/
ObjectCreatedOnlyToCallGetClassCheck.java
108 lines (96 loc) · 3.93 KB
/
ObjectCreatedOnlyToCallGetClassCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.checks;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.java.checks.methods.AbstractMethodDetection;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;
@Rule(key = "S2133")
public class ObjectCreatedOnlyToCallGetClassCheck extends AbstractMethodDetection {
@Override
protected MethodMatchers getMethodInvocationMatchers() {
return MethodMatchers.create().ofAnyType().names("getClass").addWithoutParametersMatcher().build();
}
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
if (mit.methodSelect()
.is(Tree.Kind.MEMBER_SELECT)) {
ExpressionTree expressionTree = ((MemberSelectExpressionTree) mit.methodSelect()).expression();
if (expressionTree.is(Tree.Kind.NEW_CLASS, Tree.Kind.NEW_ARRAY)) {
reportIssue(expressionTree);
} else if (expressionTree.is(Tree.Kind.IDENTIFIER) && variableUsedOnlyToGetClass((IdentifierTree) expressionTree)) {
reportIssue(getInitializer((IdentifierTree) expressionTree));
}
}
}
@CheckForNull
private static ExpressionTree getInitializer(IdentifierTree tree) {
Symbol symbol = tree.symbol();
if (symbol.isVariableSymbol()) {
VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration();
if (declaration != null) {
return declaration.initializer();
}
}
return null;
}
private static boolean variableUsedOnlyToGetClass(IdentifierTree tree) {
if ("this".equals(tree.name()) || "super".equals(tree.name())) {
return false;
}
Symbol symbol = tree.symbol();
return symbol.usages().size() == 1 && hasBeenInitialized(tree);
}
private static boolean hasBeenInitialized(IdentifierTree tree) {
ExpressionTree initializer = getInitializer(tree);
return initializer != null && initializer.is(Tree.Kind.NEW_CLASS);
}
private void reportIssue(@Nullable ExpressionTree expressionTree) {
if (expressionTree != null) {
reportIssue(expressionTree, "Remove this object instantiation and use \"" + getTypeName(expressionTree) + ".class\" instead.");
}
}
private static String getTypeName(ExpressionTree tree) {
Type type = tree.symbolType();
String name = getTypeName(type);
if (name.isEmpty()) {
name = getAnonymousClassTypeName(type.symbol());
}
return name;
}
private static String getAnonymousClassTypeName(Symbol.TypeSymbol symbol) {
if (symbol.interfaces().isEmpty()) {
return getTypeName(symbol.superClass());
}
return getTypeName(symbol.interfaces().get(0));
}
private static String getTypeName(Type type) {
return type.symbol().name();
}
}