diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java index da36fd38247..e50c014cd00 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java @@ -28,12 +28,14 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.sonar.java.model.ExpressionUtils; import org.sonar.java.model.JavaTree; import org.sonar.java.model.expression.IdentifierTreeImpl; import org.sonar.plugins.java.api.diagnostics.ProblemKind; import org.sonar.plugins.java.api.semantic.Symbol; import org.sonar.plugins.java.api.semantic.Type; +import org.sonar.plugins.java.api.tree.AnnotationTree; import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; import org.sonar.plugins.java.api.tree.BaseTreeVisitor; import org.sonar.plugins.java.api.tree.BinaryExpressionTree; @@ -44,15 +46,18 @@ import org.sonar.plugins.java.api.tree.IdentifierTree; import org.sonar.plugins.java.api.tree.ImportClauseTree; import org.sonar.plugins.java.api.tree.ImportTree; +import org.sonar.plugins.java.api.tree.LiteralTree; import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; import org.sonar.plugins.java.api.tree.MethodReferenceTree; import org.sonar.plugins.java.api.tree.MethodTree; import org.sonar.plugins.java.api.tree.ModuleDeclarationTree; +import org.sonar.plugins.java.api.tree.NewArrayTree; import org.sonar.plugins.java.api.tree.PackageDeclarationTree; import org.sonar.plugins.java.api.tree.SyntaxToken; import org.sonar.plugins.java.api.tree.SyntaxTrivia; import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.TryStatementTree; +import org.sonar.plugins.java.api.tree.TypeParameterTree; import org.sonar.plugins.java.api.tree.TypeTree; import org.sonar.plugins.java.api.tree.UnionTypeTree; import org.sonar.plugins.java.api.tree.VariableTree; @@ -151,6 +156,12 @@ public void visitMethodReference(MethodReferenceTree tree) { scan(tree.method().annotations()); } + @Override + public void visitTypeParameter(TypeParameterTree tree) { + scan(tree.identifier().annotations()); + super.visitTypeParameter(tree); + } + @Override public void visitTypeCast(org.sonar.plugins.java.api.tree.TypeCastTree tree) { Type cast = tree.type().symbolType(); @@ -206,14 +217,17 @@ public void visitTryStatement(TryStatementTree tree) { /** Flow analysis is not done for a unit that has unresolved names. */ private void reportUnreachableCatches(JavaTree.CompilationUnitTreeImpl unit) { - String currentPackage = unit.packageDeclaration() == null ? "" : dotted(unit.packageDeclaration().packageName()); - boolean unresolved = identifiers.stream().anyMatch(identifier -> identifier.symbol().isUnknown() && !isUnnamed(identifier)) - || unit.imports().stream().anyMatch(clause -> clause instanceof ImportTree tree && !isResolvable(tree, dotted(tree.qualifiedIdentifier()), currentPackage)); - if (!unresolved) { + if (!hasUnresolvedNames(unit)) { tries.forEach(this::reportUnreachableCatches); } } + private boolean hasUnresolvedNames(JavaTree.CompilationUnitTreeImpl unit) { + String currentPackage = unit.packageDeclaration() == null ? "" : dotted(unit.packageDeclaration().packageName()); + return identifiers.stream().anyMatch(identifier -> identifier.symbol().isUnknown() && !isUnnamed(identifier)) + || unit.imports().stream().anyMatch(clause -> clause instanceof ImportTree tree && !isResolvable(tree, dotted(tree.qualifiedIdentifier()), currentPackage)); + } + private static boolean isUnnamed(IdentifierTree identifier) { return identifier instanceof IdentifierTreeImpl impl && impl.isUnnamedVariable(); } @@ -367,7 +381,11 @@ private void reportUnusedImports(JavaTree.CompilationUnitTreeImpl unit) { // The compiler reports no unused import in a module declaration unit. return; } + if (suppressesUnused(unit)) { + return; + } Set seen = new HashSet<>(); + Set declared = declaredTypeNames(unit); String currentPackage = unit.packageDeclaration() == null ? "" : dotted(unit.packageDeclaration().packageName()); loadImportedTypes(unit); List outOfScope = identifiers.stream().filter(identifier -> !isInFileScope(identifier)).toList(); @@ -378,12 +396,38 @@ private void reportUnusedImports(JavaTree.CompilationUnitTreeImpl unit) { String name = dotted(importTree.qualifiedIdentifier()); String key = (importTree.isStatic() ? "static " : "") + name; boolean duplicate = !seen.add(key); - if (isResolvable(importTree, name, currentPackage) && (duplicate || isOwnPackage(importTree, name, currentPackage) || !isUsed(importTree, name, outOfScope))) { + boolean ownType = !importTree.isStatic() && importTree.qualifiedIdentifier() instanceof MemberSelectExpressionTree select && declared.contains(select.identifier().name()); + if (!ownType && isResolvable(importTree, name, currentPackage) && (duplicate || isOwnPackage(importTree, name, currentPackage) || !isUsed(importTree, name, outOfScope))) { add(ProblemKind.UNUSED_IMPORT, "The import " + (name.endsWith(".*") ? name.substring(0, name.length() - 2) : name) + " is never used", importTree); } } } + /** The compiler honours a suppression of unused code on the first type declaration of the unit for its imports too. */ + private static boolean suppressesUnused(JavaTree.CompilationUnitTreeImpl unit) { + return unit.types().stream().filter(ClassTree.class::isInstance).map(ClassTree.class::cast).findFirst() + .map(first -> first.modifiers().annotations().stream().anyMatch(CompilerWarnings::suppressesUnused)).orElse(false); + } + + /** The compiler never reports a single-type import of a type the unit itself declares. */ + private static Set declaredTypeNames(JavaTree.CompilationUnitTreeImpl unit) { + return unit.types().stream().filter(ClassTree.class::isInstance).map(ClassTree.class::cast) + .map(type -> type.simpleName() == null ? "" : type.simpleName().name()).collect(Collectors.toSet()); + } + + private static boolean suppressesUnused(AnnotationTree annotation) { + String name = dotted(annotation.annotationType()); + if (!"SuppressWarnings".equals(name) && !"java.lang.SuppressWarnings".equals(name)) { + return false; + } + return annotation.arguments().stream().anyMatch(argument -> { + ExpressionTree value = argument instanceof AssignmentExpressionTree assignment ? assignment.expression() : (ExpressionTree) argument; + List literals = value instanceof NewArrayTree array ? array.initializers() : List.of(value); + return literals.stream().anyMatch(literal -> literal instanceof LiteralTree string && string.is(Tree.Kind.STRING_LITERAL) + && Set.of("\"unused\"", "\"all\"").contains(string.value())); + }); + } + /** Loads the imported types first, so that an on-demand import of their package is known to exist. */ private void loadImportedTypes(JavaTree.CompilationUnitTreeImpl unit) { for (ImportClauseTree clause : unit.imports()) { @@ -470,7 +514,7 @@ private boolean uses(ImportTree importTree, String name, String qualifier, Ident if (!importTree.isStatic()) { return denotesType(symbol, dotted, identifier); } - return isMemberOf(symbol, qualifier.replace('$', '.')) || (symbol.isTypeSymbol() && dottedType(symbol).equals(dotted)); + return isMemberOf(symbol, qualifier.replace('$', '.')) || denotesType(symbol, dotted, identifier); } /** A static import also brings in the members the named type inherits. */ diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DeclarationTranslator.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DeclarationTranslator.java index af1f61a6ceb..20a0e7e68f4 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DeclarationTranslator.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/DeclarationTranslator.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import javax.annotation.Nullable; +import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl; import org.sonar.java.ast.parser.ArgumentListTreeImpl; import org.sonar.java.ast.parser.FormalParametersListTreeImpl; import org.sonar.java.ast.parser.TypeParameterListTreeImpl; @@ -83,12 +84,16 @@ ClassTreeImpl typeDeclaration(CstNode node) { Tree.Kind kind = declarationKind(node); CstNode body = requiredField(node, "body", null); CstNode name = requiredField(node, "name", null); - state.types.push(sema.resolveTypeSymbol(name.decl())); - // translated before the members so that the bounds of the type parameters are known inside the bodies + Symbol.TypeSymbol declaredSymbol = sema.resolveTypeSymbol(name.decl()); + state.types.push(declaredSymbol); + // translated before the members so that the bounds of the type parameters and the supertypes are known inside the bodies CstNode typeParameters = field(node, "type_parameters"); TypeParameterListTreeImpl typeParameterList = typeParameters == null ? null : typeTrees().typeParameters(typeParameters); List members; + Hierarchy hierarchy; try { + hierarchy = hierarchy(node); + bindSupertypes(declaredSymbol, hierarchy.superType(), hierarchy.interfaceTypes(), kind); members = membersOf(body); } finally { state.types.pop(); @@ -114,9 +119,9 @@ ClassTreeImpl typeDeclaration(CstNode node) { state.scopes.removeFirst(); } } - completeHierarchy(tree, node); + completeHierarchy(tree, node, hierarchy); bindDeclaration(tree, name, identifier); - bindSupertypes(tree, kind); + bindRecordComponents(tree, kind); return tree; } @@ -140,17 +145,25 @@ private static String declarationKeyword(CstNode node) { }; } - private void completeHierarchy(ClassTreeImpl tree, CstNode node) { + private record Hierarchy(@Nullable CstNode superclass, @Nullable TypeTree superType, @Nullable CstNode interfaces, @Nullable QualifiedIdentifierListTreeImpl interfaceTypes) { + } + + private Hierarchy hierarchy(CstNode node) { CstNode superclass = childKind(node, "superclass"); - if (superclass != null) { - tree.completeSuperclass(token(required(superclass, "extends")), typeTrees().type(firstType(superclass))); - } CstNode interfaces = childKind(node, "super_interfaces"); if (interfaces == null) { interfaces = childKind(node, "extends_interfaces"); } - if (interfaces != null) { - tree.completeInterfaces(firstKeyword(interfaces), typeTrees().separatedTypes(interfaces)); + return new Hierarchy(superclass, superclass == null ? null : typeTrees().type(firstType(superclass)), interfaces, + interfaces == null ? null : typeTrees().separatedTypes(interfaces)); + } + + private void completeHierarchy(ClassTreeImpl tree, CstNode node, Hierarchy hierarchy) { + if (hierarchy.superclass() != null) { + tree.completeSuperclass(token(required(hierarchy.superclass(), "extends")), hierarchy.superType()); + } + if (hierarchy.interfaces() != null) { + tree.completeInterfaces(firstKeyword(hierarchy.interfaces()), hierarchy.interfaceTypes()); } CstNode permits = childKind(node, "permits"); if (permits != null) { @@ -158,7 +171,7 @@ private void completeHierarchy(ClassTreeImpl tree, CstNode node) { } } - private static void bindSupertypes(ClassTreeImpl tree, Tree.Kind kind) { + private static void bindRecordComponents(ClassTreeImpl tree, Tree.Kind kind) { if (kind == Tree.Kind.RECORD && tree.symbol() instanceof JTypeSymbol recordSymbol) { // the components of a record belong to the record, not to its canonical constructor tree.recordComponents().stream() @@ -167,17 +180,19 @@ private static void bindSupertypes(ClassTreeImpl tree, Tree.Kind kind) { .map(JVariableSymbol.class::cast) .forEach(component -> component.owner(recordSymbol)); } - if (tree.symbol() instanceof JTypeSymbol declared) { - if (tree.superClass() != null - && tree.superClass().symbolType().symbol() instanceof JTypeSymbol parent && parent.id != null && !parent.isUnknown() && !parent.equals(declared)) { + } + + private static void bindSupertypes(Symbol.TypeSymbol symbol, @Nullable TypeTree superType, @Nullable QualifiedIdentifierListTreeImpl interfaces, Tree.Kind kind) { + if (symbol instanceof JTypeSymbol declared) { + if (superType != null + && superType.symbolType().symbol() instanceof JTypeSymbol parent && parent.id != null && !parent.isUnknown() && !parent.equals(declared)) { declared.superName(parent.id); } else if (kind == Tree.Kind.RECORD) { declared.superName("java.lang.Record"); } else if (kind == Tree.Kind.ENUM) { declared.superName("java.lang.Enum"); } - declared.declaredSupertypes(tree.superClass() == null ? null : tree.superClass().symbolType(), - tree.superInterfaces().stream().map(TypeTree::symbolType).toList()); + declared.declaredSupertypes(superType == null ? null : superType.symbolType(), (interfaces == null ? List.of() : interfaces).stream().map(TypeTree::symbolType).toList()); } } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/ExpressionTranslator.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/ExpressionTranslator.java index ef55e2bfb7c..1d8927f5993 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/ExpressionTranslator.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/ExpressionTranslator.java @@ -21,6 +21,7 @@ import javax.annotation.Nullable; import org.sonar.java.ast.parser.ArgumentListTreeImpl; import org.sonar.java.ast.parser.QualifiedIdentifierListTreeImpl; +import org.sonar.java.model.ExpressionUtils; import org.sonar.java.model.InternalSyntaxToken; import org.sonar.java.model.JavaTree; import org.sonar.java.model.LiteralUtils; @@ -168,6 +169,19 @@ private void ascribeGenericCall(ExpressionTree expression, Type target) { } } + /** A generic call that nothing types, such as a cast operand, infers {@code Object} for the type variables its arguments leave open. */ + private void defaultOpenTypeVariables(ExpressionTree expression) { + if (ExpressionUtils.skipParentheses(expression) instanceof MethodInvocationTreeImpl call + && call.methodSymbol() instanceof JMethodSymbol generic + && !generic.isUnknown() + && !capturesAnArgument(call.symbolType())) { + Type inferred = sema.inference().defaultedReturn(generic, call.symbolType()); + if (inferred != null) { + retargetInvocation(call, generic, inferred); + } + } + } + private static boolean capturesAnArgument(Type type) { return type.typeArguments().stream().anyMatch(argument -> argument instanceof JType captured && captured.isCapture()); } @@ -452,6 +466,7 @@ private TypeCastExpressionTreeImpl cast(CstNode node) { } } ExpressionTree value = expression(requiredField(node, "value", lastNamed(node))); + defaultOpenTypeVariables(value); InternalSyntaxToken andToken = bounds.isEmpty() ? null : token(ands.get(0)); TypeCastExpressionTreeImpl tree = new TypeCastExpressionTreeImpl(token(required(node, "(")), type, andToken, bounds, token(required(node, ")")), value); if (bounds.isEmpty()) { diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/FileImports.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/FileImports.java index 47f2c945708..052e711d4ee 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/FileImports.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/FileImports.java @@ -25,6 +25,7 @@ final class FileImports { final Map singleImports = new HashMap<>(); final List onDemandImports = new ArrayList<>(); - final Map staticImports = new HashMap<>(); + /** Every owner a name is statically imported from, in import order: the names of several owners overload each other. */ + final Map> staticImports = new HashMap<>(); final List staticOnDemandImports = new ArrayList<>(); } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Inference.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Inference.java index 7b02bc60745..b8a4f7aeda3 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Inference.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Inference.java @@ -24,8 +24,10 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.sonar.plugins.java.api.semantic.Type; +import org.sonar.plugins.java.api.tree.MethodTree; import static org.sonar.java.semsitter.SemaFacts.strings; import static org.sonar.java.semsitter.SemaFacts.text; @@ -244,6 +246,51 @@ private Map methodSubstitution(JMethodSymbol method, List ar return substitution; } + /** + * The type {@code method} returns when its call has no target type, which leaves the type variables that the + * arguments do not fix to {@code Object}, or null when none stays open. + */ + @Nullable + Type defaultedReturn(JMethodSymbol method, Type current) { + if (method.declaration() instanceof MethodTree declaration && !declaration.typeParameters().isEmpty()) { + if (current.isTypeVar() || declaration.typeParameters().stream().anyMatch(parameter -> !parameter.bounds().isEmpty())) { + return null; + } + Set own = declaration.typeParameters().stream().map(parameter -> parameter.identifier().name()).collect(Collectors.toSet()); + Type replaced = withObjectFor(own, current); + return replaced == current ? null : replaced; + } + JsonObject member = sema.substitution().lookupGenericMember(method); + JsonObject declared = sema.substitution().declaredReturn(method); + if (member == null || declared == null || !(member.get(TYPE_VARS) instanceof JsonArray names) || names.isEmpty()) { + return null; + } + if (isTypeVariable(declared) || (method.id != null && method.id.matches(".*#[^(]*<[^>(]*:.*"))) { + return null; + } + String descriptor = text(member, "desc"); + boolean open = descriptor == null ? Substitution.mentionsTypeVariable(current) : current.equals(sema.factTypes().returnTypeOf(descriptor)); + if (!open) { + return null; + } + Type object = sema.getClassType(SemaFacts.OBJECT_TYPE); + Map bound = new HashMap<>(); + names.forEach(name -> bound.put(name.getAsString(), object)); + JType result = sema.factTypes().typeFromFact(declared, bound); + return result.isUnknown() || Substitution.mentionsTypeVariable(result) ? null : result; + } + + private Type withObjectFor(Set variables, Type type) { + if (type instanceof JType variable && variable.isTypeVar() && variables.contains(variable.name())) { + return sema.getClassType(SemaFacts.OBJECT_TYPE); + } + if (type instanceof JType parameterized && parameterized.isParameterized()) { + List arguments = parameterized.args.stream().map(argument -> sema.asJType(withObjectFor(variables, argument))).toList(); + return arguments.equals(parameterized.args) ? type : JType.named(sema, parameterized.erasure().fullyQualifiedName(), arguments); + } + return type; + } + /** * The type {@code method} returns when the type its call is passed as fixes the type variables of the * method, or null when they stay open or were already inferred from arguments. diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/InvocationTranslator.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/InvocationTranslator.java index db05275499b..ef3a653c7d0 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/InvocationTranslator.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/InvocationTranslator.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; import javax.annotation.Nullable; @@ -113,7 +114,10 @@ private void bindStaticImport(IdentifierTreeImpl identifier, @Nullable Expressio boolean declaredHere = state.types.stream().filter(type -> !type.isUnknown()) .anyMatch(type -> sema.lookup().declaredMethod(type.type().fullyQualifiedName(), identifier.name(), arguments) != null); if (!declaredHere) { - Symbol.MethodSymbol imported = sema.lookup().declaredMethod(state.imports.staticImports.get(identifier.name()), identifier.name(), arguments); + List owners = state.imports.staticImports.get(identifier.name()); + Symbol.MethodSymbol imported = owners.stream().map(owner -> sema.lookup().applicableMethod(owner, identifier.name(), arguments)) + .filter(Objects::nonNull).findFirst() + .orElseGet(() -> owners.stream().map(owner -> sema.lookup().declaredMethod(owner, identifier.name(), arguments)).filter(Objects::nonNull).findFirst().orElse(null)); if (imported != null) { rebindMethod(identifier, imported, initialTarget); } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSema.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSema.java index 623fef25d16..e21b5333366 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSema.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSema.java @@ -101,6 +101,7 @@ private static final class Caches { private Function packageLoader = name -> PackagePresence.UNKNOWN; private int typeRevision; private boolean noLibraries; + private int release; private boolean typesLoaded; } @@ -319,6 +320,15 @@ void moveUse(@Nullable String from, @Nullable String to, IdentifierTree use) { } /** Without any library, a method of a type nothing declares is unknown instead of named after its written owner. */ + void release(int release) { + caches.release = release; + } + + /** The configured language level, or 0 when the project sets none. */ + int release() { + return caches.release; + } + void noLibraries(boolean noLibraries) { caches.noLibraries = noLibraries; } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSymbolMetadata.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSymbolMetadata.java index 5a92fef270c..158b36455ef 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSymbolMetadata.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSymbolMetadata.java @@ -33,6 +33,7 @@ import org.sonar.plugins.java.api.tree.AnnotationTree; import org.sonar.plugins.java.api.tree.ArrayTypeTree; import org.sonar.plugins.java.api.tree.ClassTree; +import org.sonar.plugins.java.api.tree.EnumConstantTree; import org.sonar.plugins.java.api.tree.IdentifierTree; import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; import org.sonar.plugins.java.api.tree.MethodTree; @@ -84,7 +85,7 @@ private List loadAnnotations() { List fromTree = new ArrayList<>(); TypeTree written = declaredType(declaration); boolean array = written instanceof ArrayTypeTree; - for (AnnotationTree annotation : ModifiersUtils.getAnnotations(declaration)) { + for (AnnotationTree annotation : writtenAnnotations(declaration)) { // an annotation only a type can carry, written before an array type, is on its elements if (!(array && isTypeUseOnly(annotation)) && !namesDeclaredNonAnnotation(annotation)) { fromTree.add(new TreeAnnotation(symbol.sema, annotation)); @@ -107,6 +108,11 @@ private List loadAnnotations() { return fromFacts; } + /** The annotations written on the declaration, which {@code ModifiersUtils} does not read for an enum constant. */ + private static List writtenAnnotations(Tree declaration) { + return declaration instanceof EnumConstantTree constant ? constant.modifiers().annotations() : ModifiersUtils.getAnnotations(declaration); + } + /** A class or interface declared in the sources is not an annotation, whatever the code writes after the {@code @}. */ private static boolean namesDeclaredNonAnnotation(AnnotationTree annotation) { return annotation.annotationType().symbolType().symbol().declaration() instanceof ClassTree declared && !declared.is(Tree.Kind.ANNOTATION_TYPE); diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JTypeSymbol.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JTypeSymbol.java index eba034c2790..92545ce91f8 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JTypeSymbol.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JTypeSymbol.java @@ -176,6 +176,11 @@ void typeParameterCount(int typeParameterCount) { this.typeParameterCount = typeParameterCount; } + @Nullable + String superName() { + return superName; + } + void superName(@Nullable String superName) { this.superName = superName; sema.invalidateTypeCaches(); @@ -367,7 +372,10 @@ private Type computeSuperClass() { return sema.getClassType(OBJECT); } if (namesSameClass(superTypeFact, superName)) { - return sema.factTypes().supertypeFromFact(superTypeFact); + Type fromFact = sema.factTypes().supertypeFromFact(superTypeFact); + boolean declaredFits = declaredSuperClass != null && declaredSuperClass.isParameterized() + && (fromFact.isUnknown() || declaredSuperClass.erasure().fullyQualifiedName().equals(fromFact.erasure().fullyQualifiedName())); + return declaredFits && !fromFact.isParameterized() ? declaredSuperClass : fromFact; } return withDeclaredArguments(sema.getClassType(SemaFacts.erase(superName)), declaredSuperClass == null ? List.of() : List.of(declaredSuperClass)); } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/LibraryMembers.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/LibraryMembers.java index a047c66a8df..847685eb4b1 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/LibraryMembers.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/LibraryMembers.java @@ -225,6 +225,7 @@ private JSymbol newMethod(JTypeSymbol owner, String id, @Nullable String name, @ method.returnTypeSymbol(sema.factTypes().readDescriptor(descriptor, new int[] {descriptor.indexOf(')') + 1}).symbol()); } applyMemberFlags(method, member); + method.parametrized(!strings(member, "typeVars").isEmpty()); if (owner.isInterface() && !truth(member, JSema.ABSTRACT) && !truth(member, JSema.STATIC) && !"private".equals(text(member, "vis"))) { method.addModifiers(List.of("default")); } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/MemberLookup.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/MemberLookup.java index 26a77debcc4..281db948ef9 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/MemberLookup.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/MemberLookup.java @@ -125,6 +125,17 @@ Symbol.MethodSymbol declaredMethod(String ownerFqn, String name, List argu /** [declaredMethod] where the arguments at {@code functions} are lambdas or method references, which only a functional interface takes. */ @Nullable Symbol.MethodSymbol declaredMethod(String ownerFqn, String name, List arguments, Set functions) { + return declaredMethod(ownerFqn, name, arguments, functions, true); + } + + /** The method Java would pick for {@code arguments} among those declared by {@code ownerFqn}, without guessing one that is not applicable. */ + @Nullable + Symbol.MethodSymbol applicableMethod(String ownerFqn, String name, List arguments) { + return declaredMethod(ownerFqn, name, arguments, Set.of(), false); + } + + @Nullable + private Symbol.MethodSymbol declaredMethod(String ownerFqn, String name, List arguments, Set functions, boolean guessSameArity) { if (!sema.declaresType(ownerFqn)) { return null; } @@ -140,6 +151,9 @@ Symbol.MethodSymbol declaredMethod(String ownerFqn, String name, List argu return applicable.stream().filter(method -> applicable.stream().allMatch(other -> OverloadRanking.moreSpecific(method, other))).findFirst().orElse(applicable.get(0)); } } + if (!guessSameArity) { + return null; + } List sameArity = candidates.stream() .filter(method -> method.isVarArgsMethod() ? (arguments.size() >= (method.parameterTypes().size() - 1)) : (method.parameterTypes().size() == arguments.size())) .toList(); @@ -337,6 +351,9 @@ private void collectMethods(String root, String ownerFqn, String name, Set collectMethods(root, erase(parent), name, seen, found)); + if (sema.typeSymbol(ownerFqn) instanceof JTypeSymbol declared && declared.superName() != null) { + collectMethods(root, erase(declared.superName()), name, seen, found); + } } } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java index 8bf098e429a..ff777cbfeff 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java @@ -17,6 +17,7 @@ package org.sonar.java.semsitter; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; @@ -76,7 +77,8 @@ ExpressionTree primary(CstNode node) { IdentifierTreeImpl identifier = identifier(node); bindUse(identifier, node); if (!state.naming && identifier.symbol().isUnknown() && node.unresolved()) { - String type = importedType(identifier.name()); + String type = inheritedMemberType(identifier.name()); + type = type != null ? type : importedType(identifier.name()); if (type != null) { bindType(identifier, type); } @@ -158,6 +160,36 @@ private void preferSingleImport(IdentifierTreeImpl identifier) { } } + /** A member type an enclosing type inherits, found through the type information of a compiled supertype. */ + @Nullable + private String inheritedMemberType(String name) { + for (Symbol.TypeSymbol enclosing : state.types) { + String found = enclosing.isUnknown() ? null : memberTypeOf(enclosing.type(), name, new HashSet<>()); + if (found != null) { + return found; + } + } + return null; + } + + @Nullable + private String memberTypeOf(Type type, String name, Set visited) { + String owner = type.erasure().fullyQualifiedName(); + if (type.isUnknown() || !visited.add(owner)) { + return null; + } + String candidate = owner + "$" + name; + if (sema.declaresType(candidate)) { + return candidate; + } + List supertypes = new ArrayList<>(type.symbol().interfaces()); + Type superClass = type.symbol().superClass(); + if (superClass != null) { + supertypes.add(superClass); + } + return supertypes.stream().map(supertype -> memberTypeOf(supertype, name, visited)).filter(Objects::nonNull).findFirst().orElse(null); + } + /** The type a simple name stands for when SemSitter left it unresolved: imported, in the package, or in java.lang. */ @Nullable private String importedType(String name) { @@ -178,10 +210,7 @@ private String importedType(String name) { private void unresolvedField(IdentifierTreeImpl identifier) { String name = identifier.name(); List imported = new ArrayList<>(); - String single = state.imports.staticImports.get(name); - if (single != null) { - imported.add(single); - } + imported.addAll(state.imports.staticImports.getOrDefault(name, List.of())); imported.addAll(state.imports.staticOnDemandImports); Stream members = state.types.stream().filter(type -> !type.isUnknown()) .map(type -> sema.lookup().declaredField(type.type().fullyQualifiedName(), name)); @@ -247,7 +276,7 @@ Symbol.MethodSymbol libraryMethod(@Nullable ExpressionTree qualifier, String nam } else { state.types.stream().filter(type -> !type.isUnknown()).forEach(type -> owners.add(type.type().fullyQualifiedName())); if (state.imports.staticImports.containsKey(name)) { - owners.add(state.imports.staticImports.get(name)); + owners.addAll(state.imports.staticImports.get(name)); } owners.addAll(state.imports.staticOnDemandImports); } diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterTranslator.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterTranslator.java index a772893a4a9..b7124cf7e08 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterTranslator.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterTranslator.java @@ -95,6 +95,7 @@ static JParseResult translate(Document document, Function, JsonObjec int javaVersion) { JSema sema = new JSema(); sema.noLibraries(noLibraries); + sema.release(javaVersion); sema.ingestDocument(document); sema.loadTypes(types); sema.loadPackages(packages); @@ -323,7 +324,11 @@ private JavaTree.ImportTreeImpl importDeclaration(CstNode node) { if (staticToken == null) { state.imports.singleImports.putIfAbsent(select.identifier().name(), dottedName(name)); } else { - state.imports.staticImports.putIfAbsent(select.identifier().name(), dottedName(select.expression())); + List owners = state.imports.staticImports.computeIfAbsent(select.identifier().name(), key -> new ArrayList<>()); + String owner = dottedName(select.expression()); + if (!owners.contains(owner)) { + owners.add(owner); + } } } JavaTree.ImportTreeImpl imported = new JavaTree.ImportTreeImpl(token(required(node, "import")), staticToken, optionalToken(node, MODULE), name, token(required(node, ";"))); diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemaAnnotations.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemaAnnotations.java index 03163e334f0..d00d3bfd4b7 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemaAnnotations.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemaAnnotations.java @@ -117,7 +117,9 @@ void registerParameterAnnotations(String methodId, JsonObject info) { /** Annotations and deprecation a {@code types} answer gives a library type or member. */ void libraryAnnotations(JSymbol symbol, String id, JsonObject info) { registerParameterAnnotations(id, info); - List annotations = readAnnotations(info); + List annotations = readAnnotations(info).stream() + .filter(annotation -> !deprecatedAfterRelease(id, annotation)) + .toList(); if (annotations.isEmpty()) { return; } @@ -127,6 +129,33 @@ void libraryAnnotations(JSymbol symbol, String id, JsonObject info) { } } + /** + * Whether a JDK member is annotated {@code @Deprecated(since = ...)} with a release newer than the configured language + * level: the API of that release does not know the deprecation yet. Like ECJ, levels up to 8 see the running JDK's API. + */ + private boolean deprecatedAfterRelease(String id, DeclaredAnnotation annotation) { + int release = sema.release(); + if (release <= 8 || !SemaFacts.DEPRECATED_ANNOTATION.equals(annotation.name()) || !isJdkMember(id)) { + return false; + } + for (SymbolMetadata.AnnotationValue value : annotation.values()) { + if ("since".equals(value.name()) && value.value() instanceof String since) { + String number = since.startsWith("1.") ? since.substring(2) : since; + int dot = number.indexOf('.'); + try { + return Integer.parseInt(dot < 0 ? number : number.substring(0, dot)) > release; + } catch (NumberFormatException notAVersion) { + return false; + } + } + } + return false; + } + + private static boolean isJdkMember(String id) { + return id.startsWith("java.") || id.startsWith("javax.") || id.startsWith("jdk."); + } + /** Annotations written on the outermost type of a field or return type are annotations of the member. */ List readAnnotations(JsonObject info) { List annotations = new ArrayList<>(readAnnotationList(info.get(JSema.ANNOTATIONS))); diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Substitution.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Substitution.java index 94432de18da..ccf4a7da99d 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Substitution.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/Substitution.java @@ -439,13 +439,32 @@ private boolean preservesArguments(Type from, Type to, Set visitedVars) } for (int i = 0; i < to.typeArguments().size(); i++) { Type written = to.typeArguments().get(i); - if (!(written instanceof JType type && type.isWildcard()) && !written.equals(aligned.typeArguments().get(i))) { + Type argument = aligned.typeArguments().get(i); + boolean contained = written instanceof JType type && type.isWildcard() ? wildcardContains(type, argument) : written.equals(argument); + if (!contained) { return false; } } return true; } + /** Whether {@code argument} lies within the bounds the wildcard {@code written} states. */ + private static boolean wildcardContains(JType written, Type argument) { + if (argument.isUnknown() || written.element == null || written.element.isUnknown()) { + return true; + } + JType lowerBound = written.lower; + if (lowerBound != null) { + Type ground = argument instanceof JType other && (other.isWildcard() || other.isCapture()) ? other.lower : argument; + return ground != null && (lowerBound.isUnknown() || lowerBound.isSubtypeOf(ground)); + } + if (written.element.is("java.lang.Object")) { + return true; + } + Type ground = argument instanceof JType other && (other.isWildcard() || other.isCapture()) ? (other.lower != null ? null : other.element) : argument; + return ground != null && ground.isSubtypeOf(written.element); + } + private boolean preservesVariableArguments(JType from, Type to, Set visitedVars) { if (from.isCapture() || from.isWildcard()) { JType ground = from.isCapture() ? from.element : from.groundType(); diff --git a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/TypeTranslator.java b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/TypeTranslator.java index 295a852cbd9..9eee907eaac 100644 --- a/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/TypeTranslator.java +++ b/java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/TypeTranslator.java @@ -223,6 +223,7 @@ TypeParameterListTreeImpl typeParameters(CstNode node) { private TypeParameterTree typeParameter(CstNode node) { CstNode nameNode = firstOf(node, "type_identifier", "identifier"); IdentifierTreeImpl name = names().identifier(nameNode); + name.complete(node.children().stream().filter(CstNodes::isAnnotation).map(members()::annotation).toList()); bindUse(name, nameNode); CstNode bound = childKind(node, "type_bound"); TypeParameterTreeImpl tree; diff --git a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ReplayTest.java b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ReplayTest.java index fe392cf7bb8..168620b7c63 100644 --- a/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ReplayTest.java +++ b/java-frontend-semsitter/src/test/java/org/sonar/java/semsitter/ReplayTest.java @@ -20,14 +20,19 @@ import com.sonar.sslr.api.RecognitionException; import java.io.IOException; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import java.util.Set; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.sonar.java.model.JParseResult; +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; import org.sonar.plugins.java.api.tree.CompilationUnitTree; +import org.sonar.plugins.java.api.tree.IdentifierTree; +import org.sonar.plugins.java.api.tree.MethodInvocationTree; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -96,6 +101,37 @@ void approved_semantics(String sample) throws Exception { assertThat(actual).containsExactlyElementsOf(Replays.read(approved).lines().toList()); } + @Test + void every_use_of_a_deprecated_enum_constant_is_deprecated() throws Exception { + CompilationUnitTree unit = (CompilationUnitTree) translate("DeprecatedEnumConstants").get(); + List uses = new ArrayList<>(); + unit.accept(new BaseTreeVisitor() { + @Override + public void visitIdentifier(IdentifierTree tree) { + if (tree.symbol().isVariableSymbol() && !tree.symbol().isUnknown()) { + uses.add(tree.name() + "=" + tree.symbol().metadata().isAnnotatedWith("java.lang.Deprecated")); + } + } + }); + + assertThat(uses).contains("OLD=true").doesNotContain("OLD=false", "CURRENT=true"); + } + + @Test + void a_generic_library_method_is_parametrized() throws Exception { + CompilationUnitTree unit = (CompilationUnitTree) translate("InheritedGenericCalls").get(); + List calls = new ArrayList<>(); + unit.accept(new BaseTreeVisitor() { + @Override + public void visitMethodInvocation(MethodInvocationTree tree) { + calls.add(tree.methodSymbol().name() + "=" + tree.methodSymbol().isParametrizedMethod()); + super.visitMethodInvocation(tree); + } + }); + + assertThat(calls).contains("emptyList=true", "setBlockSize=false"); + } + private static List describe(JParseResult result) { try { return TreeDescription.describe(result.get()); diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.expected.txt new file mode 100644 index 00000000000..6203f024716 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.expected.txt @@ -0,0 +1,189 @@ +COMPILATION_UNIT @1:1 +CLASS @1:1 class .CastToSuperWildcard [t package] : CastToSuperWildcard super java.lang.Object interfaces [] + details declared true usages 0 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing CastToSuperWildcard flags [package] members [, fromObject, number, raw] lookupToString 0 outermost CastToSuperWildcard annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw true declaring CastToSuperWildcard +MODIFIERS +TOKEN @1:1 +IDENTIFIER @1:7 id CastToSuperWildcard .CastToSuperWildcard [t package] : CastToSuperWildcard : CastToSuperWildcard +TOKEN @1:7 +TYPE_PARAMETERS @1:26 +TOKEN @1:26 +TYPE_PARAMETER @1:27 +IDENTIFIER @1:27 id T CastToSuperWildcard.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @1:27 +TOKEN @1:28 +LIST +LIST +TOKEN @1:30 +VARIABLE @2:3 var CastToSuperWildcard.raw [v final package] : java.lang.Class + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing CastToSuperWildcard flags [final, package] local false parameter false effectivelyFinal true constant null +MODIFIERS @2:3 +TOKEN @2:3 +PARAMETERIZED_TYPE @2:9 : java.lang.Class +IDENTIFIER @2:9 id Class java.lang.Class [t final public] : java.lang.Class : java.lang.Class +TOKEN @2:9 +TYPE_ARGUMENTS @2:14 +TOKEN @2:14 +SUPER_WILDCARD @2:15 +TOKEN @2:15 +TOKEN @2:17 +IDENTIFIER @2:23 id T CastToSuperWildcard.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @2:23 +TOKEN @2:24 +IDENTIFIER @2:26 id raw CastToSuperWildcard.raw [v final package] : java.lang.Class : java.lang.Class +TOKEN @2:26 +TOKEN @2:29 +VARIABLE @3:3 var CastToSuperWildcard.number [v final package] : java.lang.Class + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing CastToSuperWildcard flags [final, package] local false parameter false effectivelyFinal true constant null +MODIFIERS @3:3 +TOKEN @3:3 +PARAMETERIZED_TYPE @3:9 : java.lang.Class +IDENTIFIER @3:9 id Class java.lang.Class [t final public] : java.lang.Class : java.lang.Class +TOKEN @3:9 +TYPE_ARGUMENTS @3:14 +TOKEN @3:14 +EXTENDS_WILDCARD @3:15 +TOKEN @3:15 +TOKEN @3:17 +IDENTIFIER @3:25 id Number java.lang.Number [t abstract public] : java.lang.Number : java.lang.Number +TOKEN @3:25 +TOKEN @3:31 +IDENTIFIER @3:33 id number CastToSuperWildcard.number [v final package] : java.lang.Class : java.lang.Class +TOKEN @3:33 +TOKEN @3:39 +CONSTRUCTOR @5:3 method CastToSuperWildcard. [m package] : ? (java.lang.Object,?) returns void throws [] sig CastToSuperWildcard#(Ljava/lang/Object;Ljava/lang/Object;)V overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing CastToSuperWildcard flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [type, integer] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @5:3 id CastToSuperWildcard CastToSuperWildcard. [m package] : ? (java.lang.Object,?) returns void throws [] sig CastToSuperWildcard#(Ljava/lang/Object;Ljava/lang/Object;)V : void +TOKEN @5:3 +TOKEN @5:22 +VARIABLE @5:23 var .type [v package] : java.lang.Object + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing CastToSuperWildcard flags [package] local true parameter true effectivelyFinal true constant null owner CastToSuperWildcard#(Ljava/lang/Object;Ljava/lang/Object;)V [package] returns void parameters 2 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing CastToSuperWildcard type true kinds truefalsefalse other falsefalsefalsefalsetruefalse2falsefalse +MODIFIERS +IDENTIFIER @5:23 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @5:23 +IDENTIFIER @5:30 id type .type [v package] : java.lang.Object : java.lang.Object +TOKEN @5:30 +TOKEN @5:34 +VARIABLE @5:36 var .integer [v package] : java.lang.Class + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing CastToSuperWildcard flags [package] local true parameter true effectivelyFinal true constant null owner CastToSuperWildcard#(Ljava/lang/Object;Ljava/lang/Object;)V [package] returns void parameters 2 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing CastToSuperWildcard type true kinds truefalsefalse other falsefalsefalsefalsetruefalse2falsefalse +MODIFIERS +PARAMETERIZED_TYPE @5:36 : java.lang.Class +IDENTIFIER @5:36 id Class java.lang.Class [t final public] : java.lang.Class : java.lang.Class +TOKEN @5:36 +TYPE_ARGUMENTS @5:41 +TOKEN @5:41 +IDENTIFIER @5:42 id Integer java.lang.Integer [t final public] : java.lang.Integer @jdk.internal.ValueBased : java.lang.Integer +TOKEN @5:42 +TOKEN @5:49 +IDENTIFIER @5:51 id integer .integer [v package] : java.lang.Class : java.lang.Class +TOKEN @5:51 +TOKEN @5:58 +BLOCK @5:60 +TOKEN @5:60 +EXPRESSION_STATEMENT @6:5 +ASSIGNMENT @6:5 : java.lang.Class +MEMBER_SELECT @6:5 : java.lang.Class +IDENTIFIER @6:5 id this CastToSuperWildcard.this [v final] : CastToSuperWildcard : CastToSuperWildcard +TOKEN @6:5 +TOKEN @6:9 +IDENTIFIER @6:10 id raw CastToSuperWildcard.raw [v final package] : java.lang.Class : java.lang.Class +TOKEN @6:10 +TOKEN @6:14 +TYPE_CAST @6:16 : java.lang.Class +TOKEN @6:16 +PARAMETERIZED_TYPE @6:17 : java.lang.Class +IDENTIFIER @6:17 id Class java.lang.Class [t final public] : java.lang.Class : java.lang.Class +TOKEN @6:17 +TYPE_ARGUMENTS @6:22 +TOKEN @6:22 +SUPER_WILDCARD @6:23 +TOKEN @6:23 +TOKEN @6:25 +IDENTIFIER @6:31 id T CastToSuperWildcard.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @6:31 +TOKEN @6:32 +LIST +TOKEN @6:33 +METHOD_INVOCATION @6:35 call CastToSuperWildcard.fromObject [m static package] : ? (java.lang.Object) returns java.lang.Class throws [] sig CastToSuperWildcard#fromObject(Ljava/lang/Object;)Ljava/lang/Object; : java.lang.Class + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing CastToSuperWildcard flags [static, package] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [o] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +IDENTIFIER @6:35 id fromObject CastToSuperWildcard.fromObject [m static package] : ? (java.lang.Object) returns java.lang.Class throws [] sig CastToSuperWildcard#fromObject(Ljava/lang/Object;)Ljava/lang/Object; : java.lang.Class +TOKEN @6:35 +ARGUMENTS @6:45 +TOKEN @6:45 +IDENTIFIER @6:46 id type .type [v package] : java.lang.Object : java.lang.Object +TOKEN @6:46 +TOKEN @6:50 +TOKEN @6:51 +EXPRESSION_STATEMENT @7:5 +ASSIGNMENT @7:5 : java.lang.Class +MEMBER_SELECT @7:5 : java.lang.Class +IDENTIFIER @7:5 id this CastToSuperWildcard.this [v final] : CastToSuperWildcard : CastToSuperWildcard +TOKEN @7:5 +TOKEN @7:9 +IDENTIFIER @7:10 id number CastToSuperWildcard.number [v final package] : java.lang.Class : java.lang.Class +TOKEN @7:10 +TOKEN @7:17 +TYPE_CAST @7:19 : java.lang.Class +TOKEN @7:19 +PARAMETERIZED_TYPE @7:20 : java.lang.Class +IDENTIFIER @7:20 id Class java.lang.Class [t final public] : java.lang.Class : java.lang.Class +TOKEN @7:20 +TYPE_ARGUMENTS @7:25 +TOKEN @7:25 +EXTENDS_WILDCARD @7:26 +TOKEN @7:26 +TOKEN @7:28 +IDENTIFIER @7:36 id Number java.lang.Number [t abstract public] : java.lang.Number : java.lang.Number +TOKEN @7:36 +TOKEN @7:42 +LIST +TOKEN @7:43 +IDENTIFIER @7:45 id integer .integer [v package] : java.lang.Class : java.lang.Class +TOKEN @7:45 +TOKEN @7:52 +TOKEN @8:3 +METHOD @10:3 method CastToSuperWildcard.fromObject [m static package] : ? (java.lang.Object) returns java.lang.Class throws [] sig CastToSuperWildcard#fromObject(Ljava/lang/Object;)Ljava/lang/Object; overrides false + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing CastToSuperWildcard flags [static, package] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [o] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +MODIFIERS @10:3 +TOKEN @10:3 +TYPE_PARAMETERS +PARAMETERIZED_TYPE @10:10 : java.lang.Class +IDENTIFIER @10:10 id Class java.lang.Class [t final public] : java.lang.Class : java.lang.Class +TOKEN @10:10 +TYPE_ARGUMENTS @10:15 +TOKEN @10:15 +UNBOUNDED_WILDCARD @10:16 +TOKEN @10:16 +TOKEN @10:17 +IDENTIFIER @10:19 id fromObject CastToSuperWildcard.fromObject [m static package] : ? (java.lang.Object) returns java.lang.Class throws [] sig CastToSuperWildcard#fromObject(Ljava/lang/Object;)Ljava/lang/Object; : java.lang.Class +TOKEN @10:19 +TOKEN @10:29 +VARIABLE @10:30 var fromObject.o [v package] : java.lang.Object + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing CastToSuperWildcard flags [package] local true parameter true effectivelyFinal true constant null owner fromObject CastToSuperWildcard#fromObject(Ljava/lang/Object;)Ljava/lang/Object; [static, package] returns java.lang.Class parameters 1 thrown 0 overridden 0 declared true usages 1 metadata 0 enclosing CastToSuperWildcard type true kinds truefalsefalse other falsefalsefalsefalsefalsefalse1falsefalse +MODIFIERS +IDENTIFIER @10:30 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @10:30 +IDENTIFIER @10:37 id o fromObject.o [v package] : java.lang.Object : java.lang.Object +TOKEN @10:37 +TOKEN @10:38 +BLOCK @10:40 +TOKEN @10:40 +RETURN_STATEMENT @11:5 +TOKEN @11:5 +METHOD_INVOCATION @11:12 call Object.getClass [m final public] : ? @jdk.internal.vm.annotation.IntrinsicCandidate () returns java.lang.Class throws [] sig java.lang.Object#getClass()Ljava/lang/Class; : java.lang.Class + details declared false usages 1 metadata [jdk.internal.vm.annotation.IntrinsicCandidate()] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [jdk.internal.vm.annotation.IntrinsicCandidate tree false values 0 annotated true] typeArguments [] enclosing Object flags [final, public] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MEMBER_SELECT @11:12 : ? +IDENTIFIER @11:12 id o fromObject.o [v package] : java.lang.Object : java.lang.Object +TOKEN @11:12 +TOKEN @11:13 +IDENTIFIER @11:14 id getClass Object.getClass [m final public] : ? @jdk.internal.vm.annotation.IntrinsicCandidate () returns java.lang.Class throws [] sig java.lang.Object#getClass()Ljava/lang/Class; : java.lang.Class +TOKEN @11:14 +ARGUMENTS @11:22 +TOKEN @11:22 +TOKEN @11:23 +TOKEN @11:24 +TOKEN @12:3 +TOKEN @13:1 +TOKEN @14:1 +WARNING REDUNDANT_CAST TYPE_CAST @7:19 Unnecessary cast from Class to Class diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.java new file mode 100644 index 00000000000..e769c2c0aa2 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.java @@ -0,0 +1,13 @@ +class CastToSuperWildcard { + final Class raw; + final Class number; + + CastToSuperWildcard(Object type, Class integer) { + this.raw = (Class) fromObject(type); + this.number = (Class) integer; + } + + static Class fromObject(Object o) { + return o.getClass(); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.textproto.gz new file mode 100644 index 00000000000..e361a40b9fc Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastToSuperWildcard.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.expected.txt new file mode 100644 index 00000000000..f62133ee041 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.expected.txt @@ -0,0 +1,82 @@ +COMPILATION_UNIT @1:1 +ENUM @1:1 class .DeprecatedEnumConstants [t final package enum] : DeprecatedEnumConstants super java.lang.Enum interfaces [] + details declared true usages 3 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [final, package, enum] members [, CURRENT, DEFAULT, OLD, isOld, valueOf, values] lookupToString 0 outermost DeprecatedEnumConstants annotation false subtypeOf [java.lang.Object, java.io.Serializable, java.lang.Comparable] superTypes [java.io.Serializable, java.lang.Comparable, java.lang.Enum, java.lang.Object, java.lang.constant.Constable] numerical false wrapper false raw false declaring DeprecatedEnumConstants +MODIFIERS +TOKEN @1:1 +IDENTIFIER @1:6 id DeprecatedEnumConstants .DeprecatedEnumConstants [t final package enum] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @1:6 +TYPE_PARAMETERS +LIST +LIST +TOKEN @1:30 +ENUM_CONSTANT @2:3 var DeprecatedEnumConstants.OLD [v static final public enum deprecated] : DeprecatedEnumConstants @java.lang.Deprecated + details declared true usages 2 metadata [java.lang.Deprecated()] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [java.lang.Deprecated tree false values 0 annotated true] typeArguments [] enclosing DeprecatedEnumConstants flags [static, final, public, deprecated, enum] local false parameter false effectivelyFinal true constant null +MODIFIERS @2:3 +ANNOTATION @2:3 : java.lang.Deprecated +TOKEN @2:3 +IDENTIFIER @2:4 id Deprecated java.lang.Deprecated [t public interface] : java.lang.Deprecated @java.lang.annotation.Documented,java.lang.annotation.Retention,java.lang.annotation.Target : java.lang.Deprecated +TOKEN @2:4 +ARGUMENTS +NEW_CLASS @3:3 new DeprecatedEnumConstants. [m private] : ? () returns void throws [] sig DeprecatedEnumConstants#(Ljava/lang/String;I)V : ? + details declared false usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [private] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +IDENTIFIER @3:3 id OLD DeprecatedEnumConstants. [m private] : ? () returns void throws [] sig DeprecatedEnumConstants#(Ljava/lang/String;I)V : DeprecatedEnumConstants +TOKEN @3:3 +ARGUMENTS +TOKEN @3:6 +ENUM_CONSTANT @4:3 var DeprecatedEnumConstants.CURRENT [v static final public enum] : DeprecatedEnumConstants + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [static, final, public, enum] local false parameter false effectivelyFinal true constant null +MODIFIERS +NEW_CLASS @4:3 new DeprecatedEnumConstants. [m private] : ? () returns void throws [] sig DeprecatedEnumConstants#(Ljava/lang/String;I)V : ? + details declared false usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [private] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +IDENTIFIER @4:3 id CURRENT DeprecatedEnumConstants. [m private] : ? () returns void throws [] sig DeprecatedEnumConstants#(Ljava/lang/String;I)V : DeprecatedEnumConstants +TOKEN @4:3 +ARGUMENTS +TOKEN @4:10 +VARIABLE @6:3 var DeprecatedEnumConstants.DEFAULT [v static final package] : DeprecatedEnumConstants + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [static, final, package] local false parameter false effectivelyFinal true constant null +MODIFIERS @6:3 +TOKEN @6:3 +TOKEN @6:10 +IDENTIFIER @6:16 id DeprecatedEnumConstants .DeprecatedEnumConstants [t final package enum] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @6:16 +IDENTIFIER @6:40 id DEFAULT DeprecatedEnumConstants.DEFAULT [v static final package] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @6:40 +TOKEN @6:48 +IDENTIFIER @6:50 id OLD DeprecatedEnumConstants.OLD [v static final public enum deprecated] : DeprecatedEnumConstants @java.lang.Deprecated : DeprecatedEnumConstants +TOKEN @6:50 +TOKEN @6:53 +METHOD @8:3 method DeprecatedEnumConstants.isOld [m package] : ? (DeprecatedEnumConstants) returns boolean throws [] sig DeprecatedEnumConstants#isOld(LDeprecatedEnumConstants;)Z overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [package] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [other] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +PRIMITIVE_TYPE @8:3 : boolean +TOKEN @8:3 +IDENTIFIER @8:11 id isOld DeprecatedEnumConstants.isOld [m package] : ? (DeprecatedEnumConstants) returns boolean throws [] sig DeprecatedEnumConstants#isOld(LDeprecatedEnumConstants;)Z : boolean +TOKEN @8:11 +TOKEN @8:16 +VARIABLE @8:17 var isOld.other [v package] : DeprecatedEnumConstants + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedEnumConstants flags [package] local true parameter true effectivelyFinal true constant null owner isOld DeprecatedEnumConstants#isOld(LDeprecatedEnumConstants;)Z [package] returns boolean parameters 1 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing DeprecatedEnumConstants type true kinds truefalsefalse other falsefalsefalsefalsefalsefalse1falsefalse +MODIFIERS +IDENTIFIER @8:17 id DeprecatedEnumConstants .DeprecatedEnumConstants [t final package enum] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @8:17 +IDENTIFIER @8:41 id other isOld.other [v package] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @8:41 +TOKEN @8:46 +BLOCK @8:48 +TOKEN @8:48 +RETURN_STATEMENT @9:5 +TOKEN @9:5 +EQUAL_TO @9:12 : boolean +IDENTIFIER @9:12 id other isOld.other [v package] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @9:12 +TOKEN @9:18 +MEMBER_SELECT @9:21 : DeprecatedEnumConstants +IDENTIFIER @9:21 id DeprecatedEnumConstants .DeprecatedEnumConstants [t final package enum] : DeprecatedEnumConstants : DeprecatedEnumConstants +TOKEN @9:21 +TOKEN @9:44 +IDENTIFIER @9:45 id OLD DeprecatedEnumConstants.OLD [v static final public enum deprecated] : DeprecatedEnumConstants @java.lang.Deprecated : DeprecatedEnumConstants +TOKEN @9:45 +TOKEN @9:48 +TOKEN @10:3 +TOKEN @11:1 +TOKEN @12:1 diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.java new file mode 100644 index 00000000000..23bc2460fff --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.java @@ -0,0 +1,11 @@ +enum DeprecatedEnumConstants { + @Deprecated + OLD, + CURRENT; + + static final DeprecatedEnumConstants DEFAULT = OLD; + + boolean isOld(DeprecatedEnumConstants other) { + return other == DeprecatedEnumConstants.OLD; + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.textproto.gz new file mode 100644 index 00000000000..3ba39f911b7 Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/DeprecatedEnumConstants.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.expected.txt new file mode 100644 index 00000000000..9b2ee61ba72 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.expected.txt @@ -0,0 +1,242 @@ +COMPILATION_UNIT @1:1 +IMPORT @1:1 +TOKEN @1:1 +MEMBER_SELECT @1:8 : ? +MEMBER_SELECT @1:8 : ? +IDENTIFIER @1:8 id java unknown : ? +TOKEN @1:8 +TOKEN @1:12 +IDENTIFIER @1:13 id util unknown : ? +TOKEN @1:13 +TOKEN @1:17 +IDENTIFIER @1:18 id Collections unknown : ? +TOKEN @1:18 +TOKEN @1:29 +IMPORT @2:1 +TOKEN @2:1 +MEMBER_SELECT @2:8 : ? +MEMBER_SELECT @2:8 : ? +IDENTIFIER @2:8 id java unknown : ? +TOKEN @2:8 +TOKEN @2:12 +IDENTIFIER @2:13 id util unknown : ? +TOKEN @2:13 +TOKEN @2:17 +IDENTIFIER @2:18 id List unknown : ? +TOKEN @2:18 +TOKEN @2:22 +CLASS @4:1 class .GenericCallWithoutTarget [t package] : GenericCallWithoutTarget super java.lang.Object interfaces [] + details declared true usages 0 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] members [, Holder, casts, empty, holder] lookupToString 0 outermost GenericCallWithoutTarget annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring GenericCallWithoutTarget +MODIFIERS +TOKEN @4:1 +IDENTIFIER @4:7 id GenericCallWithoutTarget .GenericCallWithoutTarget [t package] : GenericCallWithoutTarget : GenericCallWithoutTarget +TOKEN @4:7 +TYPE_PARAMETERS +LIST +LIST +TOKEN @4:32 +INTERFACE @6:3 class GenericCallWithoutTarget.Holder [t static package interface] : GenericCallWithoutTarget$Holder super java.lang.Object interfaces [] + details declared true usages 2 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [static, package, interface] members [] lookupToString 0 outermost GenericCallWithoutTarget annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw true declaring Holder +MODIFIERS +TOKEN @6:3 +IDENTIFIER @6:13 id Holder GenericCallWithoutTarget.Holder [t static package interface] : GenericCallWithoutTarget$Holder : GenericCallWithoutTarget$Holder +TOKEN @6:13 +TYPE_PARAMETERS @6:19 +TOKEN @6:19 +TYPE_PARAMETER @6:20 +IDENTIFIER @6:20 id T Holder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @6:20 +TOKEN @6:21 +LIST +LIST +TOKEN @6:23 +TOKEN @7:3 +METHOD @9:3 method GenericCallWithoutTarget.holder [m static package] : ? (int) returns GenericCallWithoutTarget$Holder throws [] sig GenericCallWithoutTarget#holder(I)LGenericCallWithoutTarget$Holder; overrides false + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing GenericCallWithoutTarget flags [static, package] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [index] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +MODIFIERS @9:3 +TOKEN @9:3 +TYPE_PARAMETERS @9:10 +TOKEN @9:10 +TYPE_PARAMETER @9:11 +IDENTIFIER @9:11 id T holder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @9:11 +TOKEN @9:12 +PARAMETERIZED_TYPE @9:14 : GenericCallWithoutTarget$Holder +IDENTIFIER @9:14 id Holder GenericCallWithoutTarget.Holder [t static package interface] : GenericCallWithoutTarget$Holder : GenericCallWithoutTarget$Holder +TOKEN @9:14 +TYPE_ARGUMENTS @9:20 +TOKEN @9:20 +IDENTIFIER @9:21 id T holder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @9:21 +TOKEN @9:22 +IDENTIFIER @9:24 id holder GenericCallWithoutTarget.holder [m static package] : ? (int) returns GenericCallWithoutTarget$Holder throws [] sig GenericCallWithoutTarget#holder(I)LGenericCallWithoutTarget$Holder; : GenericCallWithoutTarget$Holder +TOKEN @9:24 +TOKEN @9:30 +VARIABLE @9:31 var holder.index [v package] : int + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] local true parameter true effectivelyFinal true constant null owner holder GenericCallWithoutTarget#holder(I)LGenericCallWithoutTarget$Holder; [static, package] returns GenericCallWithoutTarget$Holder parameters 1 thrown 0 overridden 0 declared true usages 1 metadata 0 enclosing GenericCallWithoutTarget type true kinds truefalsefalse other falsefalsefalsefalsefalsetrue1falsefalse +MODIFIERS +PRIMITIVE_TYPE @9:31 : int +TOKEN @9:31 +IDENTIFIER @9:35 id index holder.index [v package] : int : int +TOKEN @9:35 +TOKEN @9:40 +BLOCK @9:42 +TOKEN @9:42 +RETURN_STATEMENT @10:5 +TOKEN @10:5 +NULL_LITERAL @10:12 : +TOKEN @10:12 +TOKEN @10:16 +TOKEN @11:3 +METHOD @13:3 method GenericCallWithoutTarget.empty [m static package] : ? () returns java.util.List throws [] sig GenericCallWithoutTarget#empty()Ljava/util/List; overrides false + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing GenericCallWithoutTarget flags [static, package] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +MODIFIERS @13:3 +TOKEN @13:3 +TYPE_PARAMETERS @13:10 +TOKEN @13:10 +TYPE_PARAMETER @13:11 +IDENTIFIER @13:11 id T empty.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @13:11 +TOKEN @13:12 +PARAMETERIZED_TYPE @13:14 : java.util.List +IDENTIFIER @13:14 id List java.util.List [t public interface] : java.util.List : java.util.List +TOKEN @13:14 +TYPE_ARGUMENTS @13:18 +TOKEN @13:18 +IDENTIFIER @13:19 id T empty.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @13:19 +TOKEN @13:20 +IDENTIFIER @13:22 id empty GenericCallWithoutTarget.empty [m static package] : ? () returns java.util.List throws [] sig GenericCallWithoutTarget#empty()Ljava/util/List; : java.util.List +TOKEN @13:22 +TOKEN @13:27 +TOKEN @13:28 +BLOCK @13:30 +TOKEN @13:30 +RETURN_STATEMENT @14:5 +TOKEN @14:5 +NULL_LITERAL @14:12 : +TOKEN @14:12 +TOKEN @14:16 +TOKEN @15:3 +METHOD @17:3 method GenericCallWithoutTarget.casts [m package] : ? () returns void throws [] sig GenericCallWithoutTarget#casts()V overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +PRIMITIVE_TYPE @17:3 : void +TOKEN @17:3 +IDENTIFIER @17:8 id casts GenericCallWithoutTarget.casts [m package] : ? () returns void throws [] sig GenericCallWithoutTarget#casts()V : void +TOKEN @17:8 +TOKEN @17:13 +TOKEN @17:14 +BLOCK @17:16 +TOKEN @17:16 +VARIABLE @18:5 var casts.a [v package] : java.lang.Object + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] local true parameter false effectivelyFinal true constant null owner casts GenericCallWithoutTarget#casts()V [package] returns void parameters 0 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing GenericCallWithoutTarget type true kinds truefalsefalse other falsefalsefalsefalsetruefalse0falsefalse +MODIFIERS +IDENTIFIER @18:5 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @18:5 +IDENTIFIER @18:12 id a casts.a [v package] : java.lang.Object : java.lang.Object +TOKEN @18:12 +TOKEN @18:14 +TYPE_CAST @18:16 : GenericCallWithoutTarget$Holder +TOKEN @18:16 +IDENTIFIER @18:17 id Holder GenericCallWithoutTarget.Holder [t static package interface] : GenericCallWithoutTarget$Holder : GenericCallWithoutTarget$Holder +TOKEN @18:17 +LIST +TOKEN @18:23 +METHOD_INVOCATION @18:25 call GenericCallWithoutTarget.holder [m static package] : ? (int) returns GenericCallWithoutTarget$Holder throws [] sig GenericCallWithoutTarget#holder(I)LGenericCallWithoutTarget$Holder; : GenericCallWithoutTarget$Holder + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing GenericCallWithoutTarget flags [static, package] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [index] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +IDENTIFIER @18:25 id holder GenericCallWithoutTarget.holder [m static package] : ? (int) returns GenericCallWithoutTarget$Holder throws [] sig GenericCallWithoutTarget#holder(I)LGenericCallWithoutTarget$Holder; : GenericCallWithoutTarget$Holder +TOKEN @18:25 +ARGUMENTS @18:31 +TOKEN @18:31 +INT_LITERAL @18:32 : int = 0 +TOKEN @18:32 +TOKEN @18:33 +TOKEN @18:34 +VARIABLE @19:5 var casts.b [v package] : java.lang.Object + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] local true parameter false effectivelyFinal true constant null owner casts GenericCallWithoutTarget#casts()V [package] returns void parameters 0 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing GenericCallWithoutTarget type true kinds truefalsefalse other falsefalsefalsefalsetruefalse0falsefalse +MODIFIERS +IDENTIFIER @19:5 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @19:5 +IDENTIFIER @19:12 id b casts.b [v package] : java.lang.Object : java.lang.Object +TOKEN @19:12 +TOKEN @19:14 +TYPE_CAST @19:16 : java.util.List +TOKEN @19:16 +IDENTIFIER @19:17 id List java.util.List [t public interface] : java.util.List : java.util.List +TOKEN @19:17 +LIST +TOKEN @19:21 +METHOD_INVOCATION @19:23 call GenericCallWithoutTarget.empty [m static package] : ? () returns java.util.List throws [] sig GenericCallWithoutTarget#empty()Ljava/util/List; : java.util.List + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing GenericCallWithoutTarget flags [static, package] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +IDENTIFIER @19:23 id empty GenericCallWithoutTarget.empty [m static package] : ? () returns java.util.List throws [] sig GenericCallWithoutTarget#empty()Ljava/util/List; : java.util.List +TOKEN @19:23 +ARGUMENTS @19:28 +TOKEN @19:28 +TOKEN @19:29 +TOKEN @19:30 +VARIABLE @20:5 var casts.c [v package] : java.lang.Object + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] local true parameter false effectivelyFinal true constant null owner casts GenericCallWithoutTarget#casts()V [package] returns void parameters 0 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing GenericCallWithoutTarget type true kinds truefalsefalse other falsefalsefalsefalsetruefalse0falsefalse +MODIFIERS +IDENTIFIER @20:5 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @20:5 +IDENTIFIER @20:12 id c casts.c [v package] : java.lang.Object : java.lang.Object +TOKEN @20:12 +TOKEN @20:14 +TYPE_CAST @20:16 : java.util.List +TOKEN @20:16 +IDENTIFIER @20:17 id List java.util.List [t public interface] : java.util.List : java.util.List +TOKEN @20:17 +LIST +TOKEN @20:21 +METHOD_INVOCATION @20:23 call Collections.emptyList [m static final public] : ? () returns java.util.List throws [] sig java.util.Collections#emptyList()Ljava/util/List; : java.util.List + details declared false usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Collections flags [static, final, public] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [] parameterMetadata [] +MEMBER_SELECT @20:23 : ? +IDENTIFIER @20:23 id Collections java.util.Collections [t public] : java.util.Collections : java.util.Collections +TOKEN @20:23 +TOKEN @20:34 +IDENTIFIER @20:35 id emptyList Collections.emptyList [m static final public] : ? () returns java.util.List throws [] sig java.util.Collections#emptyList()Ljava/util/List; : java.util.List +TOKEN @20:35 +ARGUMENTS @20:44 +TOKEN @20:44 +TOKEN @20:45 +TOKEN @20:46 +VARIABLE @21:5 var casts.d [v package] : java.lang.Object + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing GenericCallWithoutTarget flags [package] local true parameter false effectivelyFinal true constant null owner casts GenericCallWithoutTarget#casts()V [package] returns void parameters 0 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing GenericCallWithoutTarget type true kinds truefalsefalse other falsefalsefalsefalsetruefalse0falsefalse +MODIFIERS +IDENTIFIER @21:5 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @21:5 +IDENTIFIER @21:12 id d casts.d [v package] : java.lang.Object : java.lang.Object +TOKEN @21:12 +TOKEN @21:14 +TYPE_CAST @21:16 : java.util.List +TOKEN @21:16 +IDENTIFIER @21:17 id List java.util.List [t public interface] : java.util.List : java.util.List +TOKEN @21:17 +LIST +TOKEN @21:21 +METHOD_INVOCATION @21:23 call Collections.emptyList [m static final public] : ? () returns java.util.List throws [] sig java.util.Collections#emptyList()Ljava/util/List; : java.util.List + details declared false usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Collections flags [static, final, public] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [] parameterMetadata [] +TYPE_ARGUMENTS @21:35 +TOKEN @21:35 +IDENTIFIER @21:36 id String java.lang.String [t final public] : java.lang.String : java.lang.String +TOKEN @21:36 +TOKEN @21:42 +MEMBER_SELECT @21:23 : ? +IDENTIFIER @21:23 id Collections java.util.Collections [t public] : java.util.Collections : java.util.Collections +TOKEN @21:23 +TOKEN @21:34 +IDENTIFIER @21:43 id emptyList Collections.emptyList [m static final public] : ? () returns java.util.List throws [] sig java.util.Collections#emptyList()Ljava/util/List; : java.util.List +TOKEN @21:43 +ARGUMENTS @21:52 +TOKEN @21:52 +TOKEN @21:53 +TOKEN @21:54 +TOKEN @22:3 +TOKEN @23:1 +TOKEN @24:1 +WARNING REDUNDANT_CAST TYPE_CAST @18:16 Unnecessary cast from GenericCallWithoutTarget.Holder to GenericCallWithoutTarget.Holder +WARNING REDUNDANT_CAST TYPE_CAST @19:16 Unnecessary cast from List to List +WARNING REDUNDANT_CAST TYPE_CAST @20:16 Unnecessary cast from List to List +WARNING REDUNDANT_CAST TYPE_CAST @21:16 Unnecessary cast from List to List diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.java new file mode 100644 index 00000000000..71101b3fa9e --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.java @@ -0,0 +1,23 @@ +import java.util.Collections; +import java.util.List; + +class GenericCallWithoutTarget { + + interface Holder { + } + + static Holder holder(int index) { + return null; + } + + static List empty() { + return null; + } + + void casts() { + Object a = (Holder) holder(0); + Object b = (List) empty(); + Object c = (List) Collections.emptyList(); + Object d = (List) Collections.emptyList(); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.textproto.gz new file mode 100644 index 00000000000..3f43bfa5eb2 Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericCallWithoutTarget.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.expected.txt new file mode 100644 index 00000000000..dcb83e9222e --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.expected.txt @@ -0,0 +1,65 @@ +COMPILATION_UNIT @1:1 +PACKAGE @1:1 +TOKEN @1:1 +IDENTIFIER @1:9 id scopes .scopes [p package] : ? : ? +TOKEN @1:9 +TOKEN @1:15 +IMPORT @3:1 +TOKEN @3:1 +MEMBER_SELECT @3:8 : ? +MEMBER_SELECT @3:8 : ? +IDENTIFIER @3:8 id java unknown : ? +TOKEN @3:8 +TOKEN @3:12 +IDENTIFIER @3:13 id util unknown : ? +TOKEN @3:13 +TOKEN @3:17 +IDENTIFIER @3:18 id List unknown : ? +TOKEN @3:18 +TOKEN @3:22 +IMPORT @4:1 +TOKEN @4:1 +MEMBER_SELECT @4:8 : ? +IDENTIFIER @4:8 id scopes unknown : ? +TOKEN @4:8 +TOKEN @4:14 +IDENTIFIER @4:15 id ImportOfDeclaredType unknown : ? +TOKEN @4:15 +TOKEN @4:35 +CLASS @6:1 class scopes.ImportOfDeclaredType [t package] : scopes.ImportOfDeclaredType super java.lang.Object interfaces [] + details declared true usages 2 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing ImportOfDeclaredType flags [package] members [, self] lookupToString 0 outermost ImportOfDeclaredType annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring ImportOfDeclaredType +MODIFIERS +TOKEN @6:1 +IDENTIFIER @6:7 id ImportOfDeclaredType scopes.ImportOfDeclaredType [t package] : scopes.ImportOfDeclaredType : scopes.ImportOfDeclaredType +TOKEN @6:7 +TYPE_PARAMETERS +LIST +LIST +TOKEN @6:28 +METHOD @8:3 method ImportOfDeclaredType.self [m package] : ? () returns scopes.ImportOfDeclaredType throws [] sig scopes.ImportOfDeclaredType#self()Lscopes/ImportOfDeclaredType; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing ImportOfDeclaredType flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @8:3 id ImportOfDeclaredType scopes.ImportOfDeclaredType [t package] : scopes.ImportOfDeclaredType : scopes.ImportOfDeclaredType +TOKEN @8:3 +IDENTIFIER @8:24 id self ImportOfDeclaredType.self [m package] : ? () returns scopes.ImportOfDeclaredType throws [] sig scopes.ImportOfDeclaredType#self()Lscopes/ImportOfDeclaredType; : scopes.ImportOfDeclaredType +TOKEN @8:24 +TOKEN @8:28 +TOKEN @8:29 +BLOCK @8:31 +TOKEN @8:31 +RETURN_STATEMENT @9:5 +TOKEN @9:5 +NEW_CLASS @9:12 new ImportOfDeclaredType. [m package] : ? () returns void throws [] sig scopes.ImportOfDeclaredType#()V : scopes.ImportOfDeclaredType + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing ImportOfDeclaredType flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +TOKEN @9:12 +IDENTIFIER @9:16 id ImportOfDeclaredType ImportOfDeclaredType. [m package] : ? () returns void throws [] sig scopes.ImportOfDeclaredType#()V : scopes.ImportOfDeclaredType +TOKEN @9:16 +ARGUMENTS @9:36 +TOKEN @9:36 +TOKEN @9:37 +TOKEN @9:38 +TOKEN @10:3 +TOKEN @11:1 +TOKEN @12:1 +WARNING UNUSED_IMPORT IMPORT @3:1 The import java.util.List is never used diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.java new file mode 100644 index 00000000000..d3fda7ac3ba --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.java @@ -0,0 +1,11 @@ +package scopes; + +import java.util.List; +import scopes.ImportOfDeclaredType; + +class ImportOfDeclaredType { + + ImportOfDeclaredType self() { + return new ImportOfDeclaredType(); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.textproto.gz new file mode 100644 index 00000000000..82b2c5393c1 Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/ImportOfDeclaredType.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.expected.txt new file mode 100644 index 00000000000..f93e14189b5 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.expected.txt @@ -0,0 +1,268 @@ +COMPILATION_UNIT @1:1 +IMPORT @1:1 +TOKEN @1:1 +MEMBER_SELECT @1:8 : ? +MEMBER_SELECT @1:8 : ? +IDENTIFIER @1:8 id java unknown : ? +TOKEN @1:8 +TOKEN @1:12 +IDENTIFIER @1:13 id util unknown : ? +TOKEN @1:13 +TOKEN @1:17 +IDENTIFIER @1:18 id Collections unknown : ? +TOKEN @1:18 +TOKEN @1:29 +IMPORT @2:1 +TOKEN @2:1 +MEMBER_SELECT @2:8 : ? +MEMBER_SELECT @2:8 : ? +IDENTIFIER @2:8 id java unknown : ? +TOKEN @2:8 +TOKEN @2:12 +IDENTIFIER @2:13 id util unknown : ? +TOKEN @2:13 +TOKEN @2:17 +IDENTIFIER @2:18 id List unknown : ? +TOKEN @2:18 +TOKEN @2:22 +CLASS @4:1 class .Codec [t abstract package] : Codec super java.lang.Object interfaces [] + details declared true usages 1 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing Codec flags [abstract, package] members [, AbstractBuilder] lookupToString 0 outermost Codec annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring Codec +MODIFIERS @4:1 +TOKEN @4:1 +TOKEN @4:10 +IDENTIFIER @4:16 id Codec .Codec [t abstract package] : Codec : Codec +TOKEN @4:16 +TYPE_PARAMETERS +LIST +LIST +TOKEN @4:22 +CLASS @5:3 class Codec.AbstractBuilder [t static abstract package] : Codec$AbstractBuilder super java.lang.Object interfaces [] + details declared true usages 2 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing Codec flags [static, abstract, package] members [, asThis, block, setBlockSize] lookupToString 0 outermost Codec annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw true declaring AbstractBuilder +MODIFIERS @5:3 +TOKEN @5:3 +TOKEN @5:12 +TOKEN @5:19 +IDENTIFIER @5:25 id AbstractBuilder Codec.AbstractBuilder [t static abstract package] : Codec$AbstractBuilder : Codec$AbstractBuilder +TOKEN @5:25 +TYPE_PARAMETERS @5:40 +TOKEN @5:40 +TYPE_PARAMETER @5:41 +IDENTIFIER @5:41 id T AbstractBuilder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @5:41 +TOKEN @5:42 +TYPE_PARAMETER @5:44 +IDENTIFIER @5:44 id B AbstractBuilder.B [t package] : B erasure Codec$AbstractBuilder : B erasure Codec$AbstractBuilder +TOKEN @5:44 +TOKEN @5:46 +LIST @5:54 +PARAMETERIZED_TYPE @5:54 : Codec$AbstractBuilder +IDENTIFIER @5:54 id AbstractBuilder Codec.AbstractBuilder [t static abstract package] : Codec$AbstractBuilder : Codec$AbstractBuilder +TOKEN @5:54 +TYPE_ARGUMENTS @5:69 +TOKEN @5:69 +IDENTIFIER @5:70 id T AbstractBuilder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @5:70 +TOKEN @5:71 +IDENTIFIER @5:73 id B AbstractBuilder.B [t package] : B erasure Codec$AbstractBuilder : B erasure Codec$AbstractBuilder +TOKEN @5:73 +TOKEN @5:74 +TOKEN @5:75 +LIST +LIST +TOKEN @5:77 +VARIABLE @6:5 var AbstractBuilder.block [v private] : int + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [private] local false parameter false effectivelyFinal false constant null +MODIFIERS @6:5 +TOKEN @6:5 +PRIMITIVE_TYPE @6:13 : int +TOKEN @6:13 +IDENTIFIER @6:17 id block AbstractBuilder.block [v private] : int : int +TOKEN @6:17 +TOKEN @6:22 +METHOD @8:5 method AbstractBuilder.setBlockSize [m package] : ? (int) returns Codec$AbstractBuilder:: throws [] sig Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; overrides false + details declared true usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [size] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @8:5 id B AbstractBuilder.B [t package] : B erasure Codec$AbstractBuilder : B erasure Codec$AbstractBuilder +TOKEN @8:5 +IDENTIFIER @8:7 id setBlockSize AbstractBuilder.setBlockSize [m package] : ? (int) returns Codec$AbstractBuilder:: throws [] sig Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; : Codec$AbstractBuilder:: +TOKEN @8:7 +TOKEN @8:19 +VARIABLE @8:20 var setBlockSize.size [v package] : int + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [package] local true parameter true effectivelyFinal true constant null owner setBlockSize Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; [package] returns Codec$AbstractBuilder:: parameters 1 thrown 0 overridden 0 declared true usages 2 metadata 0 enclosing AbstractBuilder type true kinds truefalsefalse other falsefalsefalsefalsetruefalse1falsefalse +MODIFIERS +PRIMITIVE_TYPE @8:20 : int +TOKEN @8:20 +IDENTIFIER @8:24 id size setBlockSize.size [v package] : int : int +TOKEN @8:24 +TOKEN @8:28 +BLOCK @8:30 +TOKEN @8:30 +EXPRESSION_STATEMENT @9:7 +ASSIGNMENT @9:7 : int +MEMBER_SELECT @9:7 : int +IDENTIFIER @9:7 id this AbstractBuilder.this [v final] : Codec$AbstractBuilder : Codec$AbstractBuilder +TOKEN @9:7 +TOKEN @9:11 +IDENTIFIER @9:12 id block AbstractBuilder.block [v private] : int : int +TOKEN @9:12 +TOKEN @9:18 +IDENTIFIER @9:20 id size setBlockSize.size [v package] : int : int +TOKEN @9:20 +TOKEN @9:24 +RETURN_STATEMENT @10:7 +TOKEN @10:7 +METHOD_INVOCATION @10:14 call AbstractBuilder.asThis [m package] : ? () returns Codec$AbstractBuilder:: throws [] sig Codec$AbstractBuilder#asThis()LCodec$AbstractBuilder::; : Codec$AbstractBuilder:: + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +IDENTIFIER @10:14 id asThis AbstractBuilder.asThis [m package] : ? () returns Codec$AbstractBuilder:: throws [] sig Codec$AbstractBuilder#asThis()LCodec$AbstractBuilder::; : Codec$AbstractBuilder:: +TOKEN @10:14 +ARGUMENTS @10:20 +TOKEN @10:20 +TOKEN @10:21 +TOKEN @10:22 +TOKEN @11:5 +METHOD @13:5 method AbstractBuilder.asThis [m package] : ? () returns Codec$AbstractBuilder:: throws [] sig Codec$AbstractBuilder#asThis()LCodec$AbstractBuilder::; overrides false + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @13:5 id B AbstractBuilder.B [t package] : B erasure Codec$AbstractBuilder : B erasure Codec$AbstractBuilder +TOKEN @13:5 +IDENTIFIER @13:7 id asThis AbstractBuilder.asThis [m package] : ? () returns Codec$AbstractBuilder:: throws [] sig Codec$AbstractBuilder#asThis()LCodec$AbstractBuilder::; : Codec$AbstractBuilder:: +TOKEN @13:7 +TOKEN @13:13 +TOKEN @13:14 +BLOCK @13:16 +TOKEN @13:16 +RETURN_STATEMENT @14:7 +TOKEN @14:7 +TYPE_CAST @14:14 : B erasure Codec$AbstractBuilder +TOKEN @14:14 +IDENTIFIER @14:15 id B AbstractBuilder.B [t package] : B erasure Codec$AbstractBuilder : B erasure Codec$AbstractBuilder +TOKEN @14:15 +LIST +TOKEN @14:16 +IDENTIFIER @14:18 id this AbstractBuilder.this [v final] : Codec$AbstractBuilder : Codec$AbstractBuilder +TOKEN @14:18 +TOKEN @14:22 +TOKEN @15:5 +TOKEN @16:3 +TOKEN @17:1 +CLASS @19:1 class .Base16 [t package] : Base16 super Codec interfaces [] + details declared true usages 1 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing Base16 flags [package] members [, Builder, none] lookupToString 0 outermost Base16 annotation false subtypeOf [java.lang.Object] superTypes [Codec, java.lang.Object] numerical false wrapper false raw false declaring Base16 +MODIFIERS +TOKEN @19:1 +IDENTIFIER @19:7 id Base16 .Base16 [t package] : Base16 : Base16 +TOKEN @19:7 +TYPE_PARAMETERS +TOKEN @19:14 +IDENTIFIER @19:22 id Codec .Codec [t abstract package] : Codec : Codec +TOKEN @19:22 +LIST +LIST +TOKEN @19:28 +CLASS @20:3 class Base16.Builder [t static package] : Base16$Builder super Codec$AbstractBuilder interfaces [] + details declared true usages 2 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing Base16 flags [static, package] members [, lowerCase] lookupToString 0 outermost Base16 annotation false subtypeOf [java.lang.Object] superTypes [Codec$AbstractBuilder, java.lang.Object] numerical false wrapper false raw false declaring Builder +MODIFIERS @20:3 +TOKEN @20:3 +TOKEN @20:10 +IDENTIFIER @20:16 id Builder Base16.Builder [t static package] : Base16$Builder : Base16$Builder +TOKEN @20:16 +TYPE_PARAMETERS +TOKEN @20:24 +PARAMETERIZED_TYPE @20:32 : Codec$AbstractBuilder +IDENTIFIER @20:32 id AbstractBuilder Codec.AbstractBuilder [t static abstract package] : Codec$AbstractBuilder : Codec$AbstractBuilder +TOKEN @20:32 +TYPE_ARGUMENTS @20:47 +TOKEN @20:47 +IDENTIFIER @20:48 id Base16 .Base16 [t package] : Base16 : Base16 +TOKEN @20:48 +TOKEN @20:54 +IDENTIFIER @20:56 id Builder Base16.Builder [t static package] : Base16$Builder : Base16$Builder +TOKEN @20:56 +TOKEN @20:63 +LIST +LIST +TOKEN @20:65 +CONSTRUCTOR @21:5 method Builder. [m package] : ? () returns void throws [] sig Base16$Builder#()V overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Builder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @21:5 id Builder Builder. [m package] : ? () returns void throws [] sig Base16$Builder#()V : void +TOKEN @21:5 +TOKEN @21:12 +TOKEN @21:13 +BLOCK @21:15 +TOKEN @21:15 +EXPRESSION_STATEMENT @22:7 +METHOD_INVOCATION @22:7 call AbstractBuilder.setBlockSize [m package] : ? (int) returns Base16$Builder throws [] sig Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; : Base16$Builder + details declared true usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [size] parameterMetadata [] +IDENTIFIER @22:7 id setBlockSize AbstractBuilder.setBlockSize [m package] : ? (int) returns Base16$Builder throws [] sig Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; : Base16$Builder +TOKEN @22:7 +ARGUMENTS @22:19 +TOKEN @22:19 +INT_LITERAL @22:20 : int = 2 +TOKEN @22:20 +TOKEN @22:21 +TOKEN @22:22 +TOKEN @23:5 +METHOD @25:5 method Builder.lowerCase [m package] : ? () returns Base16$Builder throws [] sig Base16$Builder#lowerCase()LBase16$Builder; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Builder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @25:5 id Builder Base16.Builder [t static package] : Base16$Builder : Base16$Builder +TOKEN @25:5 +IDENTIFIER @25:13 id lowerCase Builder.lowerCase [m package] : ? () returns Base16$Builder throws [] sig Base16$Builder#lowerCase()LBase16$Builder; : Base16$Builder +TOKEN @25:13 +TOKEN @25:22 +TOKEN @25:23 +BLOCK @25:25 +TOKEN @25:25 +RETURN_STATEMENT @26:7 +TOKEN @26:7 +METHOD_INVOCATION @26:14 call AbstractBuilder.setBlockSize [m package] : ? (int) returns Base16$Builder throws [] sig Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; : Base16$Builder + details declared true usages 2 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing AbstractBuilder flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [size] parameterMetadata [] +IDENTIFIER @26:14 id setBlockSize AbstractBuilder.setBlockSize [m package] : ? (int) returns Base16$Builder throws [] sig Codec$AbstractBuilder#setBlockSize(I)LCodec$AbstractBuilder::; : Base16$Builder +TOKEN @26:14 +ARGUMENTS @26:26 +TOKEN @26:26 +INT_LITERAL @26:27 : int = 1 +TOKEN @26:27 +TOKEN @26:28 +TOKEN @26:29 +TOKEN @27:5 +TOKEN @28:3 +METHOD @30:3 method Base16.none [m package] : ? () returns java.util.List throws [] sig Base16#none()Ljava/util/List; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing Base16 flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [[] nullability NO_ANNOTATION/VARIABLE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups []] +MODIFIERS +TYPE_PARAMETERS +PARAMETERIZED_TYPE @30:3 : java.util.List +IDENTIFIER @30:3 id List java.util.List [t public interface] : java.util.List : java.util.List +TOKEN @30:3 +TYPE_ARGUMENTS @30:7 +TOKEN @30:7 +IDENTIFIER @30:8 id String java.lang.String [t final public] : java.lang.String : java.lang.String +TOKEN @30:8 +TOKEN @30:14 +IDENTIFIER @30:16 id none Base16.none [m package] : ? () returns java.util.List throws [] sig Base16#none()Ljava/util/List; : java.util.List +TOKEN @30:16 +TOKEN @30:20 +TOKEN @30:21 +BLOCK @30:23 +TOKEN @30:23 +RETURN_STATEMENT @31:5 +TOKEN @31:5 +METHOD_INVOCATION @31:12 call Collections.emptyList [m static final public] : ? () returns java.util.List throws [] sig java.util.Collections#emptyList()Ljava/util/List; : java.util.List + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Collections flags [static, final, public] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [] parameterMetadata [] +MEMBER_SELECT @31:12 : ? +IDENTIFIER @31:12 id Collections java.util.Collections [t public] : java.util.Collections : java.util.Collections +TOKEN @31:12 +TOKEN @31:23 +IDENTIFIER @31:24 id emptyList Collections.emptyList [m static final public] : ? () returns java.util.List throws [] sig java.util.Collections#emptyList()Ljava/util/List; : java.util.List +TOKEN @31:24 +ARGUMENTS @31:33 +TOKEN @31:33 +TOKEN @31:34 +TOKEN @31:35 +TOKEN @32:3 +TOKEN @33:1 +TOKEN @34:1 diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.java new file mode 100644 index 00000000000..f89e47e59df --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.java @@ -0,0 +1,33 @@ +import java.util.Collections; +import java.util.List; + +abstract class Codec { + abstract static class AbstractBuilder> { + private int block; + + B setBlockSize(int size) { + this.block = size; + return asThis(); + } + + B asThis() { + return (B) this; + } + } +} + +class Base16 extends Codec { + static class Builder extends AbstractBuilder { + Builder() { + setBlockSize(2); + } + + Builder lowerCase() { + return setBlockSize(1); + } + } + + List none() { + return Collections.emptyList(); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.textproto.gz new file mode 100644 index 00000000000..ef1411e6ebd Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/InheritedGenericCalls.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.expected.txt new file mode 100644 index 00000000000..4583065466c --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.expected.txt @@ -0,0 +1,61 @@ +COMPILATION_UNIT @1:1 +PACKAGE @1:1 +TOKEN @1:1 +IDENTIFIER @1:9 id scopes .scopes [p package] : ? : ? +TOKEN @1:9 +TOKEN @1:15 +IMPORT @3:1 +TOKEN @3:1 +MEMBER_SELECT @3:8 : ? +MEMBER_SELECT @3:8 : ? +IDENTIFIER @3:8 id java unknown : ? +TOKEN @3:8 +TOKEN @3:12 +IDENTIFIER @3:13 id util unknown : ? +TOKEN @3:13 +TOKEN @3:17 +IDENTIFIER @3:18 id List unknown : ? +TOKEN @3:18 +TOKEN @3:22 +IMPORT @4:1 +TOKEN @4:1 +MEMBER_SELECT @4:8 : ? +MEMBER_SELECT @4:8 : ? +IDENTIFIER @4:8 id java unknown : ? +TOKEN @4:8 +TOKEN @4:12 +IDENTIFIER @4:13 id util unknown : ? +TOKEN @4:13 +TOKEN @4:17 +IDENTIFIER @4:18 id Map unknown : ? +TOKEN @4:18 +TOKEN @4:21 +CLASS @6:1 class scopes.SuppressedUnusedImports [t final package] : scopes.SuppressedUnusedImports @java.lang.SuppressWarnings super java.lang.Object interfaces [] + details declared true usages 0 metadata [java.lang.SuppressWarnings(value={unchecked,unused})] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [java.lang.SuppressWarnings tree true values 1 annotated true] typeArguments [] enclosing SuppressedUnusedImports flags [final, package] members [] lookupToString 0 outermost SuppressedUnusedImports annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring SuppressedUnusedImports +MODIFIERS @6:1 +ANNOTATION @6:1 : java.lang.SuppressWarnings +TOKEN @6:1 +IDENTIFIER @6:2 id SuppressWarnings java.lang.SuppressWarnings [t public interface] : java.lang.SuppressWarnings @java.lang.annotation.Retention : java.lang.SuppressWarnings +TOKEN @6:2 +ARGUMENTS @6:18 +TOKEN @6:18 +NEW_ARRAY @6:19 : java.lang.String[] +TOKEN @6:19 +LIST @6:20 +STRING_LITERAL @6:20 : java.lang.String = unchecked +TOKEN @6:20 +TOKEN @6:31 +STRING_LITERAL @6:33 : java.lang.String = unused +TOKEN @6:33 +TOKEN @6:41 +TOKEN @6:42 +TOKEN @7:1 +TOKEN @7:7 +IDENTIFIER @7:13 id SuppressedUnusedImports scopes.SuppressedUnusedImports [t final package] : scopes.SuppressedUnusedImports @java.lang.SuppressWarnings : scopes.SuppressedUnusedImports +TOKEN @7:13 +TYPE_PARAMETERS +LIST +LIST +TOKEN @7:37 +TOKEN @8:1 +TOKEN @9:1 diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.java new file mode 100644 index 00000000000..08fc1b998c8 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.java @@ -0,0 +1,8 @@ +package scopes; + +import java.util.List; +import java.util.Map; + +@SuppressWarnings({"unchecked", "unused"}) +final class SuppressedUnusedImports { +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.textproto.gz new file mode 100644 index 00000000000..6fef70d2721 Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/SuppressedUnusedImports.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.expected.txt new file mode 100644 index 00000000000..cd22b588edf --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.expected.txt @@ -0,0 +1,346 @@ +COMPILATION_UNIT @1:1 +PACKAGE @1:1 +TOKEN @1:1 +IDENTIFIER @1:9 id scopes .scopes [p package] : ? : ? +TOKEN @1:9 +TOKEN @1:15 +IMPORT @3:1 +TOKEN @3:1 +TOKEN @3:8 +MEMBER_SELECT @3:15 : ? +MEMBER_SELECT @3:15 : ? +MEMBER_SELECT @3:15 : ? +IDENTIFIER @3:15 id java unknown : ? +TOKEN @3:15 +TOKEN @3:19 +IDENTIFIER @3:20 id util unknown : ? +TOKEN @3:20 +TOKEN @3:24 +IDENTIFIER @3:25 id AbstractMap unknown : ? +TOKEN @3:25 +TOKEN @3:36 +IDENTIFIER @3:37 id SimpleEntry unknown : ? +TOKEN @3:37 +TOKEN @3:48 +IMPORT @4:1 +TOKEN @4:1 +TOKEN @4:8 +MEMBER_SELECT @4:15 : ? +MEMBER_SELECT @4:15 : ? +MEMBER_SELECT @4:15 : ? +IDENTIFIER @4:15 id java unknown : ? +TOKEN @4:15 +TOKEN @4:19 +IDENTIFIER @4:20 id util unknown : ? +TOKEN @4:20 +TOKEN @4:24 +IDENTIFIER @4:25 id Arrays unknown : ? +TOKEN @4:25 +TOKEN @4:31 +IDENTIFIER @4:32 id sort unknown : ? +TOKEN @4:32 +TOKEN @4:36 +IMPORT @5:1 +TOKEN @5:1 +TOKEN @5:8 +MEMBER_SELECT @5:15 : ? +MEMBER_SELECT @5:15 : ? +MEMBER_SELECT @5:15 : ? +IDENTIFIER @5:15 id java unknown : ? +TOKEN @5:15 +TOKEN @5:19 +IDENTIFIER @5:20 id util unknown : ? +TOKEN @5:20 +TOKEN @5:24 +IDENTIFIER @5:25 id Collections unknown : ? +TOKEN @5:25 +TOKEN @5:36 +IDENTIFIER @5:37 id sort unknown : ? +TOKEN @5:37 +TOKEN @5:41 +IMPORT @6:1 +TOKEN @6:1 +TOKEN @6:8 +MEMBER_SELECT @6:15 : ? +MEMBER_SELECT @6:15 : ? +MEMBER_SELECT @6:15 : ? +IDENTIFIER @6:15 id java unknown : ? +TOKEN @6:15 +TOKEN @6:19 +IDENTIFIER @6:20 id util unknown : ? +TOKEN @6:20 +TOKEN @6:24 +IDENTIFIER @6:25 id Map unknown : ? +TOKEN @6:25 +TOKEN @6:28 +IDENTIFIER @6:29 id entry unknown : ? +TOKEN @6:29 +TOKEN @6:34 +IMPORT @8:1 +TOKEN @8:1 +MEMBER_SELECT @8:8 : ? +MEMBER_SELECT @8:8 : ? +MEMBER_SELECT @8:8 : ? +IDENTIFIER @8:8 id java unknown : ? +TOKEN @8:8 +TOKEN @8:12 +IDENTIFIER @8:13 id lang unknown : ? +TOKEN @8:13 +TOKEN @8:17 +IDENTIFIER @8:18 id annotation unknown : ? +TOKEN @8:18 +TOKEN @8:28 +IDENTIFIER @8:29 id ElementType unknown : ? +TOKEN @8:29 +TOKEN @8:40 +IMPORT @9:1 +TOKEN @9:1 +MEMBER_SELECT @9:8 : ? +MEMBER_SELECT @9:8 : ? +MEMBER_SELECT @9:8 : ? +IDENTIFIER @9:8 id java unknown : ? +TOKEN @9:8 +TOKEN @9:12 +IDENTIFIER @9:13 id lang unknown : ? +TOKEN @9:13 +TOKEN @9:17 +IDENTIFIER @9:18 id annotation unknown : ? +TOKEN @9:18 +TOKEN @9:28 +IDENTIFIER @9:29 id Target unknown : ? +TOKEN @9:29 +TOKEN @9:35 +IMPORT @10:1 +TOKEN @10:1 +MEMBER_SELECT @10:8 : ? +MEMBER_SELECT @10:8 : ? +MEMBER_SELECT @10:8 : ? +IDENTIFIER @10:8 id java unknown : ? +TOKEN @10:8 +TOKEN @10:12 +IDENTIFIER @10:13 id util unknown : ? +TOKEN @10:13 +TOKEN @10:17 +IDENTIFIER @10:18 id AbstractMap unknown : ? +TOKEN @10:18 +TOKEN @10:29 +IDENTIFIER @10:30 id SimpleImmutableEntry unknown : ? +TOKEN @10:30 +TOKEN @10:50 +IMPORT @11:1 +TOKEN @11:1 +MEMBER_SELECT @11:8 : ? +MEMBER_SELECT @11:8 : ? +IDENTIFIER @11:8 id java unknown : ? +TOKEN @11:8 +TOKEN @11:12 +IDENTIFIER @11:13 id util unknown : ? +TOKEN @11:13 +TOKEN @11:17 +IDENTIFIER @11:18 id Comparator unknown : ? +TOKEN @11:18 +TOKEN @11:28 +IMPORT @12:1 +TOKEN @12:1 +MEMBER_SELECT @12:8 : ? +MEMBER_SELECT @12:8 : ? +IDENTIFIER @12:8 id java unknown : ? +TOKEN @12:8 +TOKEN @12:12 +IDENTIFIER @12:13 id util unknown : ? +TOKEN @12:13 +TOKEN @12:17 +IDENTIFIER @12:18 id List unknown : ? +TOKEN @12:18 +TOKEN @12:22 +CLASS @14:1 class scopes.UnusedImportScopes [t package] : scopes.UnusedImportScopes super java.lang.Object interfaces [] + details declared true usages 0 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing UnusedImportScopes flags [package] members [, Holder, Marked, more, order, pairs] lookupToString 0 outermost UnusedImportScopes annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring UnusedImportScopes +MODIFIERS +TOKEN @14:1 +IDENTIFIER @14:7 id UnusedImportScopes scopes.UnusedImportScopes [t package] : scopes.UnusedImportScopes : scopes.UnusedImportScopes +TOKEN @14:7 +TYPE_PARAMETERS +LIST +LIST +TOKEN @14:26 +ANNOTATION_TYPE @16:3 class UnusedImportScopes.Marked [t static package interface] : scopes.UnusedImportScopes$Marked @java.lang.annotation.Target super java.lang.Object interfaces [java.lang.annotation.Annotation] + details declared true usages 1 metadata [java.lang.annotation.Target(value={ElementType.TYPE_PARAMETER})] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [java.lang.annotation.Target tree true values 1 annotated true] typeArguments [] enclosing UnusedImportScopes flags [static, package, interface] members [] lookupToString 0 outermost UnusedImportScopes annotation true subtypeOf [java.lang.Object, java.lang.annotation.Annotation] superTypes [java.lang.Object, java.lang.annotation.Annotation] numerical false wrapper false raw false declaring Marked +MODIFIERS @16:3 +ANNOTATION @16:3 : java.lang.annotation.Target +TOKEN @16:3 +IDENTIFIER @16:4 id Target java.lang.annotation.Target [t public interface] : java.lang.annotation.Target @java.lang.annotation.Documented,java.lang.annotation.Retention,java.lang.annotation.Target : java.lang.annotation.Target +TOKEN @16:4 +ARGUMENTS @16:10 +TOKEN @16:10 +MEMBER_SELECT @16:11 : java.lang.annotation.ElementType +IDENTIFIER @16:11 id ElementType java.lang.annotation.ElementType [t final public enum] : java.lang.annotation.ElementType : java.lang.annotation.ElementType +TOKEN @16:11 +TOKEN @16:22 +IDENTIFIER @16:23 id TYPE_PARAMETER ElementType.TYPE_PARAMETER [v static final public enum] : java.lang.annotation.ElementType : java.lang.annotation.ElementType +TOKEN @16:23 +TOKEN @16:37 +TOKEN @17:3 +TOKEN @17:4 +IDENTIFIER @17:14 id Marked UnusedImportScopes.Marked [t static package interface] : scopes.UnusedImportScopes$Marked @java.lang.annotation.Target : scopes.UnusedImportScopes$Marked +TOKEN @17:14 +TYPE_PARAMETERS +LIST +LIST +TOKEN @17:21 +TOKEN @18:3 +INTERFACE @20:3 class UnusedImportScopes.Holder [t static package interface] : scopes.UnusedImportScopes$Holder super java.lang.Object interfaces [] + details declared true usages 0 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing UnusedImportScopes flags [static, package, interface] members [get] lookupToString 0 outermost UnusedImportScopes annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw true declaring Holder +MODIFIERS +TOKEN @20:3 +IDENTIFIER @20:13 id Holder UnusedImportScopes.Holder [t static package interface] : scopes.UnusedImportScopes$Holder : scopes.UnusedImportScopes$Holder +TOKEN @20:13 +TYPE_PARAMETERS @20:19 +TOKEN @20:19 +TYPE_PARAMETER @20:20 +IDENTIFIER @20:20 id T Holder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +ANNOTATION @20:20 : scopes.UnusedImportScopes$Marked +TOKEN @20:20 +IDENTIFIER @20:21 id Marked UnusedImportScopes.Marked [t static package interface] : scopes.UnusedImportScopes$Marked @java.lang.annotation.Target : scopes.UnusedImportScopes$Marked +TOKEN @20:21 +ARGUMENTS +TOKEN @20:28 +TOKEN @20:29 +LIST +LIST +TOKEN @20:31 +METHOD @21:5 method Holder.get [m abstract public] : ? () returns scopes.UnusedImportScopes$Holder:: throws [] sig scopes.UnusedImportScopes$Holder#get()Lscopes/UnusedImportScopes$Holder::; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Holder flags [abstract, public] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @21:5 id T Holder.T [t package] : T erasure java.lang.Object : T erasure java.lang.Object +TOKEN @21:5 +IDENTIFIER @21:7 id get Holder.get [m abstract public] : ? () returns scopes.UnusedImportScopes$Holder:: throws [] sig scopes.UnusedImportScopes$Holder#get()Lscopes/UnusedImportScopes$Holder::; : scopes.UnusedImportScopes$Holder:: +TOKEN @21:7 +TOKEN @21:10 +TOKEN @21:11 +TOKEN @21:12 +TOKEN @22:3 +METHOD @24:3 method UnusedImportScopes.order [m package] : ? (?,?) returns void throws [] sig scopes.UnusedImportScopes#order(Ljava/lang/Object;Ljava/lang/Object;)V overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing UnusedImportScopes flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [list, comparator] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +PRIMITIVE_TYPE @24:3 : void +TOKEN @24:3 +IDENTIFIER @24:8 id order UnusedImportScopes.order [m package] : ? (?,?) returns void throws [] sig scopes.UnusedImportScopes#order(Ljava/lang/Object;Ljava/lang/Object;)V : void +TOKEN @24:8 +TOKEN @24:13 +VARIABLE @24:14 var order.list [v package] : java.util.List + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing UnusedImportScopes flags [package] local true parameter true effectivelyFinal true constant null owner order scopes.UnusedImportScopes#order(Ljava/lang/Object;Ljava/lang/Object;)V [package] returns void parameters 2 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing UnusedImportScopes type true kinds truefalsefalse other falsefalsefalsefalsetruefalse2falsefalse +MODIFIERS +PARAMETERIZED_TYPE @24:14 : java.util.List +IDENTIFIER @24:14 id List java.util.List [t public interface] : java.util.List : java.util.List +TOKEN @24:14 +TYPE_ARGUMENTS @24:18 +TOKEN @24:18 +IDENTIFIER @24:19 id String java.lang.String [t final public] : java.lang.String : java.lang.String +TOKEN @24:19 +TOKEN @24:25 +IDENTIFIER @24:27 id list order.list [v package] : java.util.List : java.util.List +TOKEN @24:27 +TOKEN @24:31 +VARIABLE @24:33 var order.comparator [v package] : java.util.Comparator + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [[]] enclosing UnusedImportScopes flags [package] local true parameter true effectivelyFinal true constant null owner order scopes.UnusedImportScopes#order(Ljava/lang/Object;Ljava/lang/Object;)V [package] returns void parameters 2 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing UnusedImportScopes type true kinds truefalsefalse other falsefalsefalsefalsetruefalse2falsefalse +MODIFIERS +PARAMETERIZED_TYPE @24:33 : java.util.Comparator +IDENTIFIER @24:33 id Comparator java.util.Comparator [t public interface] : java.util.Comparator @java.lang.FunctionalInterface : java.util.Comparator +TOKEN @24:33 +TYPE_ARGUMENTS @24:43 +TOKEN @24:43 +IDENTIFIER @24:44 id String java.lang.String [t final public] : java.lang.String : java.lang.String +TOKEN @24:44 +TOKEN @24:50 +IDENTIFIER @24:52 id comparator order.comparator [v package] : java.util.Comparator : java.util.Comparator +TOKEN @24:52 +TOKEN @24:62 +BLOCK @24:64 +TOKEN @24:64 +EXPRESSION_STATEMENT @25:5 +METHOD_INVOCATION @25:5 call Collections.sort [m static public] : ? (java.util.List,java.util.Comparator) returns void throws [] sig java.util.Collections#sort(Ljava/util/List;Ljava/util/Comparator;)V : void + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Collections flags [static, public] overridden [] overridable false generic true default false synchronized false varargs false native false parameters [arg0, arg1] parameterMetadata [] +IDENTIFIER @25:5 id sort Collections.sort [m static public] : ? (java.util.List,java.util.Comparator) returns void throws [] sig java.util.Collections#sort(Ljava/util/List;Ljava/util/Comparator;)V : void +TOKEN @25:5 +ARGUMENTS @25:9 +TOKEN @25:9 +IDENTIFIER @25:10 id list order.list [v package] : java.util.List : java.util.List +TOKEN @25:10 +TOKEN @25:14 +IDENTIFIER @25:16 id comparator order.comparator [v package] : java.util.Comparator : java.util.Comparator +TOKEN @25:16 +TOKEN @25:26 +TOKEN @25:27 +TOKEN @26:3 +METHOD @28:3 method UnusedImportScopes.pairs [m package] : ? () returns java.lang.Object throws [] sig scopes.UnusedImportScopes#pairs()Ljava/lang/Object; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing UnusedImportScopes flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @28:3 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @28:3 +IDENTIFIER @28:10 id pairs UnusedImportScopes.pairs [m package] : ? () returns java.lang.Object throws [] sig scopes.UnusedImportScopes#pairs()Ljava/lang/Object; : java.lang.Object +TOKEN @28:10 +TOKEN @28:15 +TOKEN @28:16 +BLOCK @28:18 +TOKEN @28:18 +RETURN_STATEMENT @29:5 +TOKEN @29:5 +NEW_CLASS @29:12 new SimpleEntry. [m public] : ? (java.lang.Integer,java.lang.Integer) returns void throws [] sig java.util.AbstractMap$SimpleEntry#(Ljava/lang/Object;Ljava/lang/Object;)V : java.util.AbstractMap$SimpleEntry + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing SimpleEntry flags [public] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [arg0, arg1] parameterMetadata [] +TOKEN @29:12 +PARAMETERIZED_TYPE @29:16 : java.util.AbstractMap$SimpleEntry +IDENTIFIER @29:16 id SimpleEntry SimpleEntry. [m public] : ? (java.lang.Integer,java.lang.Integer) returns void throws [] sig java.util.AbstractMap$SimpleEntry#(Ljava/lang/Object;Ljava/lang/Object;)V : java.util.AbstractMap$SimpleEntry +TOKEN @29:16 +TYPE_ARGUMENTS @29:27 +TOKEN @29:27 +TOKEN @29:28 +ARGUMENTS @29:29 +TOKEN @29:29 +INT_LITERAL @29:30 : int = 1 +TOKEN @29:30 +TOKEN @29:31 +INT_LITERAL @29:33 : int = 2 +TOKEN @29:33 +TOKEN @29:34 +TOKEN @29:35 +TOKEN @30:3 +METHOD @32:3 method UnusedImportScopes.more [m package] : ? () returns java.lang.Object throws [] sig scopes.UnusedImportScopes#more()Ljava/lang/Object; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing UnusedImportScopes flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @32:3 id Object java.lang.Object [t public] : java.lang.Object : java.lang.Object +TOKEN @32:3 +IDENTIFIER @32:10 id more UnusedImportScopes.more [m package] : ? () returns java.lang.Object throws [] sig scopes.UnusedImportScopes#more()Ljava/lang/Object; : java.lang.Object +TOKEN @32:10 +TOKEN @32:14 +TOKEN @32:15 +BLOCK @32:17 +TOKEN @32:17 +RETURN_STATEMENT @33:5 +TOKEN @33:5 +NEW_CLASS @33:12 new SimpleImmutableEntry. [m public] : ? (java.lang.Integer,java.lang.Integer) returns void throws [] sig java.util.AbstractMap$SimpleImmutableEntry#(Ljava/lang/Object;Ljava/lang/Object;)V : java.util.AbstractMap$SimpleImmutableEntry + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing SimpleImmutableEntry flags [public] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [arg0, arg1] parameterMetadata [] +TOKEN @33:12 +PARAMETERIZED_TYPE @33:16 : java.util.AbstractMap$SimpleImmutableEntry +IDENTIFIER @33:16 id SimpleImmutableEntry SimpleImmutableEntry. [m public] : ? (java.lang.Integer,java.lang.Integer) returns void throws [] sig java.util.AbstractMap$SimpleImmutableEntry#(Ljava/lang/Object;Ljava/lang/Object;)V : java.util.AbstractMap$SimpleImmutableEntry +TOKEN @33:16 +TYPE_ARGUMENTS @33:36 +TOKEN @33:36 +TOKEN @33:37 +ARGUMENTS @33:38 +TOKEN @33:38 +INT_LITERAL @33:39 : int = 1 +TOKEN @33:39 +TOKEN @33:40 +INT_LITERAL @33:42 : int = 2 +TOKEN @33:42 +TOKEN @33:43 +TOKEN @33:44 +TOKEN @34:3 +TOKEN @35:1 +TOKEN @36:1 +WARNING UNUSED_IMPORT IMPORT @4:1 The import java.util.Arrays.sort is never used +WARNING UNUSED_IMPORT IMPORT @6:1 The import java.util.Map.entry is never used diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.java new file mode 100644 index 00000000000..a5046906d8d --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.java @@ -0,0 +1,35 @@ +package scopes; + +import static java.util.AbstractMap.SimpleEntry; +import static java.util.Arrays.sort; +import static java.util.Collections.sort; +import static java.util.Map.entry; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import java.util.AbstractMap.SimpleImmutableEntry; +import java.util.Comparator; +import java.util.List; + +class UnusedImportScopes { + + @Target(ElementType.TYPE_PARAMETER) + @interface Marked { + } + + interface Holder<@Marked T> { + T get(); + } + + void order(List list, Comparator comparator) { + sort(list, comparator); + } + + Object pairs() { + return new SimpleEntry<>(1, 2); + } + + Object more() { + return new SimpleImmutableEntry<>(1, 2); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.textproto.gz new file mode 100644 index 00000000000..84d1ebd51b5 Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/UnusedImportScopes.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.expected.txt new file mode 100644 index 00000000000..bca6a55298f --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.expected.txt @@ -0,0 +1,128 @@ +COMPILATION_UNIT @1:1 +IMPORT @1:1 +TOKEN @1:1 +MEMBER_SELECT @1:8 : ? +MEMBER_SELECT @1:8 : ? +IDENTIFIER @1:8 id java unknown : ? +TOKEN @1:8 +TOKEN @1:12 +IDENTIFIER @1:13 id net unknown : ? +TOKEN @1:13 +TOKEN @1:16 +IDENTIFIER @1:17 id URL unknown : ? +TOKEN @1:17 +TOKEN @1:20 +IMPORT @2:1 +TOKEN @2:1 +MEMBER_SELECT @2:8 : ? +MEMBER_SELECT @2:8 : ? +IDENTIFIER @2:8 id java unknown : ? +TOKEN @2:8 +TOKEN @2:12 +IDENTIFIER @2:13 id util unknown : ? +TOKEN @2:13 +TOKEN @2:17 +IDENTIFIER @2:18 id Locale unknown : ? +TOKEN @2:18 +TOKEN @2:24 +CLASS @4:1 class .DeprecatedSinceLaterRelease [t package] : DeprecatedSinceLaterRelease super java.lang.Object interfaces [] + details declared true usages 0 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedSinceLaterRelease flags [package] members [, boxed, locale, url] lookupToString 0 outermost DeprecatedSinceLaterRelease annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring DeprecatedSinceLaterRelease +MODIFIERS +TOKEN @4:1 +IDENTIFIER @4:7 id DeprecatedSinceLaterRelease .DeprecatedSinceLaterRelease [t package] : DeprecatedSinceLaterRelease : DeprecatedSinceLaterRelease +TOKEN @4:7 +TYPE_PARAMETERS +LIST +LIST +TOKEN @4:35 +METHOD @5:3 method DeprecatedSinceLaterRelease.url [m package] : ? () returns java.net.URL throws [java.lang.Exception] sig DeprecatedSinceLaterRelease#url()Ljava/net/URL; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedSinceLaterRelease flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @5:3 id URL java.net.URL [t final public] : java.net.URL @jdk.internal.vm.annotation.AOTSafeClassInitializer : java.net.URL +TOKEN @5:3 +IDENTIFIER @5:7 id url DeprecatedSinceLaterRelease.url [m package] : ? () returns java.net.URL throws [java.lang.Exception] sig DeprecatedSinceLaterRelease#url()Ljava/net/URL; : java.net.URL +TOKEN @5:7 +TOKEN @5:10 +TOKEN @5:11 +TOKEN @5:13 +LIST @5:20 +IDENTIFIER @5:20 id Exception java.lang.Exception [t public] : java.lang.Exception : java.lang.Exception +TOKEN @5:20 +BLOCK @5:30 +TOKEN @5:30 +RETURN_STATEMENT @6:5 +TOKEN @6:5 +NEW_CLASS @6:12 new URL. [m public] : ? (java.lang.String) returns void throws [java.net.MalformedURLException] sig java.net.URL#(Ljava/lang/String;)V : java.net.URL + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing URL flags [public] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [arg0] parameterMetadata [] +TOKEN @6:12 +IDENTIFIER @6:16 id URL URL. [m public] : ? (java.lang.String) returns void throws [java.net.MalformedURLException] sig java.net.URL#(Ljava/lang/String;)V : java.net.URL +TOKEN @6:16 +ARGUMENTS @6:19 +TOKEN @6:19 +STRING_LITERAL @6:20 : java.lang.String = http://localhost +TOKEN @6:20 +TOKEN @6:38 +TOKEN @6:39 +TOKEN @7:3 +METHOD @9:3 method DeprecatedSinceLaterRelease.locale [m package] : ? (java.lang.String) returns java.util.Locale throws [] sig DeprecatedSinceLaterRelease#locale(Ljava/lang/String;)Ljava/util/Locale; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedSinceLaterRelease flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [language] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @9:3 id Locale java.util.Locale [t final public] : java.util.Locale : java.util.Locale +TOKEN @9:3 +IDENTIFIER @9:10 id locale DeprecatedSinceLaterRelease.locale [m package] : ? (java.lang.String) returns java.util.Locale throws [] sig DeprecatedSinceLaterRelease#locale(Ljava/lang/String;)Ljava/util/Locale; : java.util.Locale +TOKEN @9:10 +TOKEN @9:16 +VARIABLE @9:17 var locale.language [v package] : java.lang.String + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedSinceLaterRelease flags [package] local true parameter true effectivelyFinal true constant null owner locale DeprecatedSinceLaterRelease#locale(Ljava/lang/String;)Ljava/util/Locale; [package] returns java.util.Locale parameters 1 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing DeprecatedSinceLaterRelease type true kinds truefalsefalse other falsefalsefalsefalsetruefalse1falsefalse +MODIFIERS +IDENTIFIER @9:17 id String java.lang.String [t final public] : java.lang.String : java.lang.String +TOKEN @9:17 +IDENTIFIER @9:24 id language locale.language [v package] : java.lang.String : java.lang.String +TOKEN @9:24 +TOKEN @9:32 +BLOCK @9:34 +TOKEN @9:34 +RETURN_STATEMENT @10:5 +TOKEN @10:5 +NEW_CLASS @10:12 new Locale. [m public] : ? (java.lang.String) returns void throws [] sig java.util.Locale#(Ljava/lang/String;)V : java.util.Locale + details declared false usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing Locale flags [public] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [arg0] parameterMetadata [] +TOKEN @10:12 +IDENTIFIER @10:16 id Locale Locale. [m public] : ? (java.lang.String) returns void throws [] sig java.util.Locale#(Ljava/lang/String;)V : java.util.Locale +TOKEN @10:16 +ARGUMENTS @10:22 +TOKEN @10:22 +IDENTIFIER @10:23 id language locale.language [v package] : java.lang.String : java.lang.String +TOKEN @10:23 +TOKEN @10:31 +TOKEN @10:32 +TOKEN @11:3 +METHOD @13:3 method DeprecatedSinceLaterRelease.boxed [m package] : ? () returns java.lang.Integer throws [] sig DeprecatedSinceLaterRelease#boxed()Ljava/lang/Integer; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedSinceLaterRelease flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @13:3 id Integer java.lang.Integer [t final public] : java.lang.Integer @jdk.internal.ValueBased : java.lang.Integer +TOKEN @13:3 +IDENTIFIER @13:11 id boxed DeprecatedSinceLaterRelease.boxed [m package] : ? () returns java.lang.Integer throws [] sig DeprecatedSinceLaterRelease#boxed()Ljava/lang/Integer; : java.lang.Integer +TOKEN @13:11 +TOKEN @13:16 +TOKEN @13:17 +BLOCK @13:19 +TOKEN @13:19 +RETURN_STATEMENT @14:5 +TOKEN @14:5 +NEW_CLASS @14:12 new Integer. [m public deprecated] : ? @java.lang.Deprecated (int) returns void throws [] sig java.lang.Integer#(I)V : java.lang.Integer + details declared false usages 1 metadata [java.lang.Deprecated(since=9,forRemoval=true)] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [java.lang.Deprecated tree false values 2 annotated true] typeArguments [] enclosing Integer flags [public, deprecated] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [arg0] parameterMetadata [] +TOKEN @14:12 +IDENTIFIER @14:16 id Integer Integer. [m public deprecated] : ? @java.lang.Deprecated (int) returns void throws [] sig java.lang.Integer#(I)V : java.lang.Integer +TOKEN @14:16 +ARGUMENTS @14:23 +TOKEN @14:23 +INT_LITERAL @14:24 : int = 1 +TOKEN @14:24 +TOKEN @14:25 +TOKEN @14:26 +TOKEN @15:3 +TOKEN @16:1 +TOKEN @17:1 diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.java new file mode 100644 index 00000000000..166c073a7c5 --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.java @@ -0,0 +1,16 @@ +import java.net.URL; +import java.util.Locale; + +class DeprecatedSinceLaterRelease { + URL url() throws Exception { + return new URL("http://localhost"); + } + + Locale locale(String language) { + return new Locale(language); + } + + Integer boxed() { + return new Integer(1); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.textproto.gz new file mode 100644 index 00000000000..ef4f88dd63b Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java17/DeprecatedSinceLaterRelease.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.expected.txt b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.expected.txt new file mode 100644 index 00000000000..860fa80a7fd --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.expected.txt @@ -0,0 +1,128 @@ +COMPILATION_UNIT @1:1 +IMPORT @1:1 +TOKEN @1:1 +MEMBER_SELECT @1:8 : ? +MEMBER_SELECT @1:8 : ? +IDENTIFIER @1:8 id java unknown : ? +TOKEN @1:8 +TOKEN @1:12 +IDENTIFIER @1:13 id net unknown : ? +TOKEN @1:13 +TOKEN @1:16 +IDENTIFIER @1:17 id URL unknown : ? +TOKEN @1:17 +TOKEN @1:20 +IMPORT @2:1 +TOKEN @2:1 +MEMBER_SELECT @2:8 : ? +MEMBER_SELECT @2:8 : ? +IDENTIFIER @2:8 id java unknown : ? +TOKEN @2:8 +TOKEN @2:12 +IDENTIFIER @2:13 id util unknown : ? +TOKEN @2:13 +TOKEN @2:17 +IDENTIFIER @2:18 id Locale unknown : ? +TOKEN @2:18 +TOKEN @2:24 +CLASS @4:1 class .DeprecatedAtLevelEight [t package] : DeprecatedAtLevelEight super java.lang.Object interfaces [] + details declared true usages 0 metadata [] nullability UNKNOWN/UNKNOWN nonNull false nullable false strongNullable false strict truetruetrue meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedAtLevelEight flags [package] members [, boxed, locale, url] lookupToString 0 outermost DeprecatedAtLevelEight annotation false subtypeOf [java.lang.Object] superTypes [java.lang.Object] numerical false wrapper false raw false declaring DeprecatedAtLevelEight +MODIFIERS +TOKEN @4:1 +IDENTIFIER @4:7 id DeprecatedAtLevelEight .DeprecatedAtLevelEight [t package] : DeprecatedAtLevelEight : DeprecatedAtLevelEight +TOKEN @4:7 +TYPE_PARAMETERS +LIST +LIST +TOKEN @4:30 +METHOD @5:3 method DeprecatedAtLevelEight.url [m package] : ? () returns java.net.URL throws [java.lang.Exception] sig DeprecatedAtLevelEight#url()Ljava/net/URL; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedAtLevelEight flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @5:3 id URL java.net.URL [t final public] : java.net.URL @jdk.internal.vm.annotation.AOTSafeClassInitializer : java.net.URL +TOKEN @5:3 +IDENTIFIER @5:7 id url DeprecatedAtLevelEight.url [m package] : ? () returns java.net.URL throws [java.lang.Exception] sig DeprecatedAtLevelEight#url()Ljava/net/URL; : java.net.URL +TOKEN @5:7 +TOKEN @5:10 +TOKEN @5:11 +TOKEN @5:13 +LIST @5:20 +IDENTIFIER @5:20 id Exception java.lang.Exception [t public] : java.lang.Exception : java.lang.Exception +TOKEN @5:20 +BLOCK @5:30 +TOKEN @5:30 +RETURN_STATEMENT @6:5 +TOKEN @6:5 +NEW_CLASS @6:12 new URL. [m public deprecated] : ? @java.lang.Deprecated (java.lang.String) returns void throws [java.net.MalformedURLException] sig java.net.URL#(Ljava/lang/String;)V : java.net.URL + details declared false usages 1 metadata [java.lang.Deprecated(since=20)] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [java.lang.Deprecated tree false values 1 annotated true] typeArguments [] enclosing URL flags [public, deprecated] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [arg0] parameterMetadata [] +TOKEN @6:12 +IDENTIFIER @6:16 id URL URL. [m public deprecated] : ? @java.lang.Deprecated (java.lang.String) returns void throws [java.net.MalformedURLException] sig java.net.URL#(Ljava/lang/String;)V : java.net.URL +TOKEN @6:16 +ARGUMENTS @6:19 +TOKEN @6:19 +STRING_LITERAL @6:20 : java.lang.String = http://localhost +TOKEN @6:20 +TOKEN @6:38 +TOKEN @6:39 +TOKEN @7:3 +METHOD @9:3 method DeprecatedAtLevelEight.locale [m package] : ? (java.lang.String) returns java.util.Locale throws [] sig DeprecatedAtLevelEight#locale(Ljava/lang/String;)Ljava/util/Locale; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedAtLevelEight flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [language] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @9:3 id Locale java.util.Locale [t final public] : java.util.Locale : java.util.Locale +TOKEN @9:3 +IDENTIFIER @9:10 id locale DeprecatedAtLevelEight.locale [m package] : ? (java.lang.String) returns java.util.Locale throws [] sig DeprecatedAtLevelEight#locale(Ljava/lang/String;)Ljava/util/Locale; : java.util.Locale +TOKEN @9:10 +TOKEN @9:16 +VARIABLE @9:17 var locale.language [v package] : java.lang.String + details declared true usages 1 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedAtLevelEight flags [package] local true parameter true effectivelyFinal true constant null owner locale DeprecatedAtLevelEight#locale(Ljava/lang/String;)Ljava/util/Locale; [package] returns java.util.Locale parameters 1 thrown 0 overridden 0 declared true usages 0 metadata 0 enclosing DeprecatedAtLevelEight type true kinds truefalsefalse other falsefalsefalsefalsetruefalse1falsefalse +MODIFIERS +IDENTIFIER @9:17 id String java.lang.String [t final public] : java.lang.String : java.lang.String +TOKEN @9:17 +IDENTIFIER @9:24 id language locale.language [v package] : java.lang.String : java.lang.String +TOKEN @9:24 +TOKEN @9:32 +BLOCK @9:34 +TOKEN @9:34 +RETURN_STATEMENT @10:5 +TOKEN @10:5 +NEW_CLASS @10:12 new Locale. [m public deprecated] : ? @java.lang.Deprecated (java.lang.String) returns void throws [] sig java.util.Locale#(Ljava/lang/String;)V : java.util.Locale + details declared false usages 1 metadata [java.lang.Deprecated(since=19)] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [java.lang.Deprecated tree false values 1 annotated true] typeArguments [] enclosing Locale flags [public, deprecated] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [arg0] parameterMetadata [] +TOKEN @10:12 +IDENTIFIER @10:16 id Locale Locale. [m public deprecated] : ? @java.lang.Deprecated (java.lang.String) returns void throws [] sig java.util.Locale#(Ljava/lang/String;)V : java.util.Locale +TOKEN @10:16 +ARGUMENTS @10:22 +TOKEN @10:22 +IDENTIFIER @10:23 id language locale.language [v package] : java.lang.String : java.lang.String +TOKEN @10:23 +TOKEN @10:31 +TOKEN @10:32 +TOKEN @11:3 +METHOD @13:3 method DeprecatedAtLevelEight.boxed [m package] : ? () returns java.lang.Integer throws [] sig DeprecatedAtLevelEight#boxed()Ljava/lang/Integer; overrides false + details declared true usages 0 metadata [] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [] typeArguments [] enclosing DeprecatedAtLevelEight flags [package] overridden [] overridable true generic false default false synchronized false varargs false native false parameters [] parameterMetadata [] +MODIFIERS +TYPE_PARAMETERS +IDENTIFIER @13:3 id Integer java.lang.Integer [t final public] : java.lang.Integer @jdk.internal.ValueBased : java.lang.Integer +TOKEN @13:3 +IDENTIFIER @13:11 id boxed DeprecatedAtLevelEight.boxed [m package] : ? () returns java.lang.Integer throws [] sig DeprecatedAtLevelEight#boxed()Ljava/lang/Integer; : java.lang.Integer +TOKEN @13:11 +TOKEN @13:16 +TOKEN @13:17 +BLOCK @13:19 +TOKEN @13:19 +RETURN_STATEMENT @14:5 +TOKEN @14:5 +NEW_CLASS @14:12 new Integer. [m public deprecated] : ? @java.lang.Deprecated (int) returns void throws [] sig java.lang.Integer#(I)V : java.lang.Integer + details declared false usages 1 metadata [java.lang.Deprecated(since=9,forRemoval=true)] nullability NO_ANNOTATION/PACKAGE nonNull false nullable false strongNullable false strict falsefalsefalse meta false source null tree false lookups [java.lang.Deprecated tree false values 2 annotated true] typeArguments [] enclosing Integer flags [public, deprecated] overridden [] overridable false generic false default false synchronized false varargs false native false parameters [arg0] parameterMetadata [] +TOKEN @14:12 +IDENTIFIER @14:16 id Integer Integer. [m public deprecated] : ? @java.lang.Deprecated (int) returns void throws [] sig java.lang.Integer#(I)V : java.lang.Integer +TOKEN @14:16 +ARGUMENTS @14:23 +TOKEN @14:23 +INT_LITERAL @14:24 : int = 1 +TOKEN @14:24 +TOKEN @14:25 +TOKEN @14:26 +TOKEN @15:3 +TOKEN @16:1 +TOKEN @17:1 diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.java b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.java new file mode 100644 index 00000000000..6be23a977cc --- /dev/null +++ b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.java @@ -0,0 +1,16 @@ +import java.net.URL; +import java.util.Locale; + +class DeprecatedAtLevelEight { + URL url() throws Exception { + return new URL("http://localhost"); + } + + Locale locale(String language) { + return new Locale(language); + } + + Integer boxed() { + return new Integer(1); + } +} diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.textproto.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.textproto.gz new file mode 100644 index 00000000000..339f87f66b5 Binary files /dev/null and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/java8/DeprecatedAtLevelEight.textproto.gz differ diff --git a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/types.json.gz b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/types.json.gz index 7c9749e3ae7..4883eaaa812 100644 Binary files a/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/types.json.gz and b/java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/types.json.gz differ