Skip to content

Commit dedca18

Browse files
committed
SONARJAVA-7236 Split the import usage check and the annotation type lookup into smaller methods
1 parent 72fe0f1 commit dedca18

4 files changed

Lines changed: 44 additions & 22 deletions

File tree

‎java-frontend/src/main/java/org/sonar/java/model/CompilerWarnings.java‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private static boolean isOwnPackage(ImportTree importTree, String name, String c
167167
return false;
168168
}
169169
String qualifier = name.endsWith(".*") ? name.substring(0, name.length() - 2) : name.substring(0, Math.max(0, name.lastIndexOf('.')));
170-
return qualifier.equals(currentPackage) && !name.endsWith(".*") || name.equals(currentPackage + ".*");
170+
return (qualifier.equals(currentPackage) && !name.endsWith(".*")) || name.equals(currentPackage + ".*");
171171
}
172172

173173
private boolean isUsed(ImportTree importTree, String name) {
@@ -176,25 +176,31 @@ private boolean isUsed(ImportTree importTree, String name) {
176176
}
177177
boolean onDemand = name.endsWith(".*");
178178
String qualifier = onDemand ? name.substring(0, name.length() - 2) : name.substring(0, Math.max(0, name.lastIndexOf('.')));
179-
String simple = name.substring(name.lastIndexOf('.') + 1);
180179
for (IdentifierTree identifier : identifiers) {
181180
Symbol symbol = identifier.symbol();
182-
if (!symbol.isUnknown() && symbol.declaration() != null) {
183-
// found in the scope of the file, the import is not consulted
184-
continue;
185-
}
186-
if (onDemand) {
187-
if (declaredIn(symbol, qualifier, importTree.isStatic())) {
188-
return true;
189-
}
190-
} else if (identifier.name().equals(simple)
191-
&& (symbol.isUnknown() || (importTree.isStatic() ? isMemberOf(symbol, qualifier) : denotesType(symbol, name)))) {
181+
// a symbol found in the scope of the file does not consult the import
182+
boolean inScope = !symbol.isUnknown() && symbol.declaration() != null;
183+
if (!inScope && uses(importTree, name, qualifier, identifier)) {
192184
return true;
193185
}
194186
}
195187
return false;
196188
}
197189

190+
private boolean uses(ImportTree importTree, String name, String qualifier, IdentifierTree identifier) {
191+
Symbol symbol = identifier.symbol();
192+
if (name.endsWith(".*")) {
193+
return declaredIn(symbol, qualifier, importTree.isStatic());
194+
}
195+
if (!identifier.name().equals(name.substring(name.lastIndexOf('.') + 1))) {
196+
return false;
197+
}
198+
if (symbol.isUnknown()) {
199+
return true;
200+
}
201+
return importTree.isStatic() ? isMemberOf(symbol, qualifier) : denotesType(symbol, name);
202+
}
203+
198204
/** A static import also brings in the members the named type inherits. */
199205
private boolean isMemberOf(Symbol symbol, String qualifier) {
200206
if (!(symbol.owner() instanceof Symbol.TypeSymbol owner)) {
@@ -221,12 +227,15 @@ private static boolean declaredIn(Symbol symbol, String qualifier, boolean isSta
221227
if (isStatic) {
222228
return symbol.owner() instanceof Symbol.TypeSymbol owner && dottedType(owner).equals(qualifier);
223229
}
224-
Symbol type = symbol.isTypeSymbol() ? symbol : ("<init>".equals(symbol.name()) ? symbol.owner() : null);
225-
return type instanceof Symbol.TypeSymbol && !type.isUnknown() && dottedType(type).startsWith(qualifier + ".");
230+
Symbol type = symbol;
231+
if ("<init>".equals(symbol.name())) {
232+
type = symbol.owner();
233+
}
234+
return type.isTypeSymbol() && !type.isUnknown() && dottedType(type).startsWith(qualifier + ".");
226235
}
227236

228237
private static String dottedType(Symbol symbol) {
229-
return ((Symbol.TypeSymbol) symbol).type().fullyQualifiedName().replace('$', '.');
238+
return symbol.type().fullyQualifiedName().replace('$', '.');
230239
}
231240

232241
private static String dotted(Tree tree) {

‎java-frontend/src/main/java/org/sonar/java/model/EffectivelyFinalVariables.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ public void visitVariable(VariableTree tree) {
6868

6969
@Override
7070
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
71-
record(tree.variable(), tree.is(Tree.Kind.ASSIGNMENT) ? 0 : 1);
71+
countAssignment(tree.variable(), tree.is(Tree.Kind.ASSIGNMENT) ? 0 : 1);
7272
super.visitAssignmentExpression(tree);
7373
}
7474

7575
@Override
7676
public void visitUnaryExpression(UnaryExpressionTree tree) {
7777
if (tree.is(Tree.Kind.PREFIX_INCREMENT, Tree.Kind.PREFIX_DECREMENT, Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.POSTFIX_DECREMENT)) {
78-
record(tree.expression(), 1);
78+
countAssignment(tree.expression(), 1);
7979
}
8080
super.visitUnaryExpression(tree);
8181
}
8282

83-
private void record(ExpressionTree target, int slot) {
83+
private void countAssignment(ExpressionTree target, int slot) {
8484
ExpressionTree expression = target;
8585
while (expression instanceof org.sonar.plugins.java.api.tree.ParenthesizedTree parenthesized) {
8686
expression = parenthesized.expression();

‎java-frontend/src/main/java/org/sonar/java/model/JVariableSymbol.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public Optional<Object> constantValue() {
7676
@Nullable
7777
private Object declaredConstant() {
7878
VariableTree declaration = declaration();
79-
if (declaration == null || declaration.initializer() == null || declaration.type() == null || !isFinal() && !owner().isInterface() && !isEnum()
80-
|| !isStatic() && !owner().isInterface()) {
79+
if (declaration == null || declaration.initializer() == null || declaration.type() == null || !isStaticFinalOrInterfaceMember()) {
8180
return null;
8281
}
8382
ExpressionTree initializer = declaration.initializer();
@@ -87,6 +86,11 @@ private Object declaredConstant() {
8786
return convert(value, declaration.type().symbolType());
8887
}
8988

89+
private boolean isStaticFinalOrInterfaceMember() {
90+
boolean constantKind = isFinal() || owner().isInterface() || isEnum();
91+
return constantKind && (isStatic() || owner().isInterface());
92+
}
93+
9094
@Nullable
9195
private static Object literalNumber(LiteralTree literal) {
9296
String text = literal.value().replace("_", "");

‎java-frontend/src/main/java/org/sonar/java/model/SemsitterTranslator.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,15 @@ private TypeTree recoverQualifiedType(TypeTree type) {
12751275
return type;
12761276
}
12771277

1278+
/** The type of an annotation: an unresolved one keeps its written, possibly qualified, name. */
1279+
private Type annotationType(TypeTree type) {
1280+
String name = type instanceof ExpressionTree expression ? dottedName(expression) : "";
1281+
if (type.symbolType().isUnknown() && !naming && !name.isEmpty()) {
1282+
return sema.recoveredType(name).type();
1283+
}
1284+
return type.symbolType();
1285+
}
1286+
12781287
/** An unresolved type name keeps its written name, as a recovered binding does, so checks can match on the simple name. */
12791288
private void recoverType(IdentifierTreeImpl identifier) {
12801289
if (identifier.symbol().isUnknown()) {
@@ -1580,7 +1589,7 @@ private ExpressionTree typeExpression(Node node) {
15801589

15811590
private NewClassTreeImpl creation(Node node) {
15821591
Node typeNode = field(node, "type");
1583-
TypeTree type = annotate(recoverQualifiedType(type(typeNode == null ? firstType(node) : typeNode)), node);
1592+
TypeTree type = annotate(recoverQualifiedType(type(Objects.requireNonNull(typeNode == null ? firstType(node) : typeNode))), node);
15841593
Node arguments = field(node, "arguments");
15851594
ArgumentListTreeImpl args = arguments == null ? ArgumentListTreeImpl.emptyList().complete(synthetic("("), synthetic(")")) : arguments(arguments);
15861595
Node body = field(node, "body");
@@ -2152,7 +2161,7 @@ private AnnotationTreeImpl annotation(Node node) {
21522161
arguments = arguments(args);
21532162
}
21542163
AnnotationTreeImpl tree = new AnnotationTreeImpl(token(required(node, "@")), type, arguments);
2155-
ascribe(tree, type.symbolType().isUnknown() && !naming && !dottedName((ExpressionTree) type).isEmpty() ? sema.recoveredType(dottedName((ExpressionTree) type)).type() : type.symbolType());
2164+
ascribe(tree, annotationType(type));
21562165
for (ExpressionTree argument : arguments) {
21572166
if (argument instanceof AssignmentExpressionTree assignment && assignment.variable() instanceof IdentifierTreeImpl element && element.symbol().isUnknown()
21582167
&& type.symbolType().isClass()) {

0 commit comments

Comments
 (0)