@Override public void visitMethod(MethodTree tree) { ArrayList<Symbol> parameters = new ArrayList<>(); for (VariableTree variableTree : tree.parameters()) { parameters.add(variableTree.symbol()); } parametersStack.push(parameters); super.visitMethod(tree); parametersStack.pop(); }
private static void collectAutowiredFields(Tree tree, Map<Symbol, VariableTree> autowiredFields) { if (!tree.is(Tree.Kind.VARIABLE)) { return; } VariableTree variable = (VariableTree) tree; Symbol variableSymbol = variable.symbol(); if (AUTOWIRED_ANNOTATIONS.stream().anyMatch(a -> variableSymbol.metadata().isAnnotatedWith(a))) { autowiredFields.put(variableSymbol, variable); } }
private static boolean matchingParameters(List<VariableTree> parameters, Arguments arguments) { return arguments.size() == parameters.size() && IntStream.range(0, arguments.size()).allMatch(i -> { List<IdentifierTree> usages = parameters.get(i).symbol().usages(); return usages.size() == 1 && usages.get(0).equals(arguments.get(i)); }); }
private static boolean isInStaticInnerClass(VariableTree hiddenVariable, VariableTree variableTree) { Symbol hiddenVariableOwner = hiddenVariable.symbol().owner(); Symbol owner = variableTree.symbol().owner(); while (!owner.equals(hiddenVariableOwner)) { if (owner.isTypeSymbol() && owner.isStatic()) { return true; } owner = owner.owner(); } return false; }
private static boolean nullAgainstParam(ExpressionTree o1, ExpressionTree o2, LambdaExpressionTree tree) { if (o1.is(Tree.Kind.NULL_LITERAL) && o2.is(Tree.Kind.IDENTIFIER)) { List<VariableTree> parameters = tree.parameters(); return parameters.size() == 1 && parameters.get(0).symbol().equals(((IdentifierTree) o2).symbol()); } return false; }
@Override public void visitMethod(MethodTree tree) { if (tree.block() == null || tree.is(Tree.Kind.CONSTRUCTOR)) { // skip everything for abstract methods (from interfaces or abstract class) and constructors return; } tree.parameters().stream() .filter(p -> IS_ZIP_ENTRY.test(p.symbol().type())) .forEach(p -> context.reportIssue(this, p, ISSUE_MESSAGE)); super.visitMethod(tree); }
private Optional<JavaFileScannerContext.Location> flowFromCaughtException(ExplodedGraph.Edge edge) { ProgramPoint programPoint = edge.parent.programPoint; if (((CFG.Block) programPoint.block).isCatchBlock() && programPoint.i == 0) { VariableTree catchVariable = ((VariableTree) programPoint.syntaxTree()); SymbolicValue.CaughtExceptionSymbolicValue caughtSv = ((SymbolicValue.CaughtExceptionSymbolicValue) edge.child.programState.getValue(catchVariable.symbol())); Preconditions.checkNotNull(caughtSv, "Caught exception not found in program state"); Type exceptionType = caughtSv.exception().exceptionType(); return Optional.of(location(edge.parent, String.format("%s is caught.", exceptionName(exceptionType)))); } return Optional.empty(); }
private boolean shouldBeCounted(VariableTree variableTree) { Symbol symbol = variableTree.symbol(); if (symbol.isStatic() && symbol.isFinal()) { return false; } return countNonPublicFields || symbol.isPublic(); }
public void checkIfUnused(VariableTree tree) { if (hasNoAnnotation(tree)) { Symbol symbol = tree.symbol(); String name = symbol.name(); if (symbol.isPrivate() && onlyUsedInVariableAssignment(symbol) && !"serialVersionUID".equals(name) && !unknownIdentifiers.contains(name)) { reportIssue(tree.simpleName(), "Remove this unused \"" + name + "\" private field."); } } }
@Override public void visitVariable(VariableTree tree) { createSymbol(tree.simpleName(), tree.symbol().usages()); super.visitVariable(tree); }
private static boolean isDateOrCollection(VariableTree variableTree) { Type type = variableTree.symbol().type(); return type.is("java.util.Date") || (type.isSubtypeOf("java.util.Collection") && !type.isSubtypeOf("com.google.common.collect.ImmutableCollection")); }
private static Symbol resourceSymbol(Tree resource) { // java 7 try-with resource if (resource.is(Tree.Kind.VARIABLE)) { return ((VariableTree) resource).symbol(); } // java 9 try-with-resource if (resource.is(Tree.Kind.IDENTIFIER)) { return ((IdentifierTree) resource).symbol(); } return ((MemberSelectExpressionTree) resource).identifier().symbol(); }
private static boolean isMemberAutowired(Tree member) { Symbol s = null; if (member.is(Kind.VARIABLE)) { s = ((VariableTree) member).symbol(); } else if (member.is(Kind.METHOD)) { s = ((MethodTree) member).symbol(); } return s != null && !s.isStatic() && isAutowired(s); }
public void checkVariableTypeAndInitializer(VariableTree variableTree) { ExpressionTree initializer = variableTree.initializer(); if ((variableTree.symbol().owner().isMethodSymbol() && !variableTree.parent().is(Tree.Kind.LAMBDA_EXPRESSION)) || (initializer != null && (initializer.is(Tree.Kind.LAMBDA_EXPRESSION) || isAnonymousClass(initializer)))) { matchFunctionalInterface((variableTree.symbol().type())).ifPresent(reportString -> { TypeTree variableType = variableTree.type(); reportIssue(variableType, reportMessage(new InterfaceTreeAndStringPairReport(reportString, variableType))); }); } }
private void checkParameter(VariableTree parameter, Symbol overrideeParamSymbol) { Tree reportTree = parameter; if (nonNullVsNull(parameter.symbol(), overrideeParamSymbol)) { for (AnnotationTree annotationTree : parameter.modifiers().annotations()) { if(annotationTree.symbolType().is(JAVAX_ANNOTATION_NONNULL)) { reportTree = annotationTree; } } reportIssue(reportTree, "Remove this \"Nonnull\" annotation to honor the overridden method's contract."); } }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { Tree parent = mit.parent(); if (parent.is(Tree.Kind.EXPRESSION_STATEMENT) || (parent.is(Tree.Kind.VARIABLE) && ((VariableTree) parent).symbol().usages().isEmpty())) { reportIssue(parent, "Do something with the \"" + mit.symbolType().name() + "\" value returned by \"" + mit.symbol().name() + "\"."); } }
@Override public void visitVariable(VariableTree tree) { ExpressionTree initializer = tree.initializer(); boolean arrayNotInitialized = initializer != null && initializer.is(Kind.NEW_ARRAY) && ((NewArrayTree) initializer).initializers().isEmpty(); boolean isFinalOrNoSemantic = context.getSemanticModel() == null || tree.symbol().isFinal(); if (arrayNotInitialized || !isFinalOrNoSemantic) { super.visitVariable(tree); } }
@Override public void visitNode(Tree tree) { VariableTree variableTree = (VariableTree) tree; Type type = variableTree.type().symbolType(); if (ModifiersUtils.hasModifier(variableTree.modifiers(), Modifier.STATIC) && isForbiddenType(variableTree)) { if (type.isSubtypeOf(JAVA_TEXT_SIMPLE_DATE_FORMAT) && onlySynchronizedUsages((Symbol.VariableSymbol) variableTree.symbol())) { return; } IdentifierTree identifierTree = variableTree.simpleName(); reportIssue(identifierTree, String.format("Make \"%s\" an instance variable.", identifierTree.name())); } }
private void checkType(TypeTree typeTree, CatchTree catchTree) { Type type = typeTree.symbolType(); if (type.is("java.lang.Error")) { insertIssue(typeTree, type); } else if (type.is(JAVA_LANG_THROWABLE)) { GuavaCloserRethrowVisitor visitor = new GuavaCloserRethrowVisitor(catchTree.parameter().symbol()); catchTree.block().accept(visitor); if (!visitor.foundRethrow) { insertIssue(typeTree, type); } } }
private static boolean isTransientSerializableOrInjected(VariableTree member) { if (ModifiersUtils.hasModifier(member.modifiers(), Modifier.TRANSIENT) || (isSerializable(member.type()) && !isSubtypeOfCollectionApi(member.type().symbolType()))) { return true; } SymbolMetadata metadata = member.symbol().metadata(); return metadata.isAnnotatedWith("javax.inject.Inject") || metadata.isAnnotatedWith("javax.ejb.EJB"); }