Skip to content

Commit 48afd27

Browse files
committed
SONARJAVA-7236 Record a use on the symbol it resolves to
1 parent f2ecb10 commit 48afd27

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ private EnumConstantTreeImpl enumConstant(Node node, @Nullable Node separatorNod
568568
String owner = enumType.type().fullyQualifiedName();
569569
Symbol.MethodSymbol constructor = sema.declaredMethod(owner, "<init>", argumentTypes(args));
570570
name.symbol = constructor != null || !args.isEmpty() ? constructor : sema.resolve(owner + "#<init>()");
571+
if (name.symbol instanceof Symbol.MethodSymbol method && !method.isUnknown()) {
572+
recordUse(method, name);
573+
}
571574
ascribe(name, enumType.type());
572575
if (tree.symbol() instanceof JSymbol field && field.type().isUnknown()) {
573576
field.type(enumType.type());
@@ -1705,7 +1708,7 @@ private void bindConstructor(NewClassTreeImpl tree, @Nullable Node typeNode) {
17051708
Symbol resolved = sema.resolve(constructor);
17061709
if (resolved instanceof Symbol.MethodSymbol method) {
17071710
identifier.symbol = resolved;
1708-
sema.use(constructor, identifier);
1711+
recordUse(resolved, identifier);
17091712
ascribeArguments(method, tree.arguments());
17101713
return;
17111714
}
@@ -1715,6 +1718,7 @@ private void bindConstructor(NewClassTreeImpl tree, @Nullable Node typeNode) {
17151718
Symbol.MethodSymbol declared = type.isClass() ? sema.declaredMethod(owner, "<init>", argumentTypes(tree.arguments())) : null;
17161719
if (declared != null) {
17171720
identifier.symbol = declared;
1721+
recordUse(declared, identifier);
17181722
ascribeArguments(declared, tree.arguments());
17191723
} else if (!(identifier.symbol() instanceof Symbol.MethodSymbol)) {
17201724
identifier.symbol = Symbol.MethodSymbol.UNKNOWN_METHOD;
@@ -2370,7 +2374,25 @@ private void bindUse(IdentifierTreeImpl identifier, Node node) {
23702374
}
23712375
identifier.symbol = symbol;
23722376
ascribe(identifier, identifierType(symbol));
2373-
sema.use(target, identifier);
2377+
sema.use(usageId(symbol, target), identifier);
2378+
}
2379+
2380+
/** Usages are stored under the symbol's own id, which can differ from the fact key that resolved it. */
2381+
private static String usageId(Symbol symbol, String target) {
2382+
if (symbol instanceof JSymbol jSymbol && !jSymbol.isUnknown()) {
2383+
String id = jSymbol.toString();
2384+
if (id != null && !id.equals(jSymbol.name())) {
2385+
return id;
2386+
}
2387+
}
2388+
return target;
2389+
}
2390+
2391+
private void recordUse(Symbol symbol, IdentifierTree identifier) {
2392+
String id = usageId(symbol, "");
2393+
if (!id.isEmpty()) {
2394+
sema.use(id, identifier);
2395+
}
23742396
}
23752397

23762398
@Nullable

0 commit comments

Comments
 (0)