@Override public void visitClass(ClassTree tree) { IdentifierTree simpleName = tree.simpleName(); if (simpleName != null) { Symbol.TypeSymbol classSymbol = tree.symbol(); Collection<Symbol> members = classSymbol.memberSymbols(); for (Symbol sym : members) { if (sym.isVariableSymbol() && !staticFieldSameType(classSymbol, sym)) { //Exclude static fields of the same type. fields.add(((Symbol.VariableSymbol) sym).declaration()); } } currentClassName = simpleName.name(); } super.visitClass(tree); currentClassName = ""; fields.clear(); }
private void checkModifiers(Symbol.VariableSymbol serialVersionUidSymbol) { List<String> missingModifiers = Lists.newArrayList(); if (!serialVersionUidSymbol.isStatic()) { missingModifiers.add("static"); } if (!serialVersionUidSymbol.isFinal()) { missingModifiers.add("final"); } if (!serialVersionUidSymbol.type().is("long")) { missingModifiers.add("long"); } Tree tree = serialVersionUidSymbol.declaration(); if (tree != null && !missingModifiers.isEmpty()) { addIssue(tree, "Make this \"serialVersionUID\" field \"" + Joiner.on(' ').join(missingModifiers) + "\"."); } }
private static boolean onlySynchronizedUsages(Symbol.VariableSymbol variable) { List<IdentifierTree> usages = variable.usages(); if (usages.isEmpty()) { return false; } for (IdentifierTree usage : usages) { SynchronizedStatementTree synchronizedStatementTree = getParentSynchronizedStatement(usage); if (synchronizedStatementTree == null) { // used outside a synchronized statement return false; } else { ExpressionTree expression = synchronizedStatementTree.expression(); if (!expression.is(Tree.Kind.IDENTIFIER) || !variable.equals(((IdentifierTree) expression).symbol())) { // variable is not the expression synchronized return false; } // check other usages } } return true; }
protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) { Symbol symbol = arg.symbol(); if (isExcluded(currentlyChecking, symbol)) { return false; } boolean isLocalVar = symbol.owner().isMethodSymbol() && !((JavaSymbol.MethodJavaSymbol) symbol.owner()).getParameters().scopeSymbols().contains(symbol); if (isLocalVar) { //symbol is a local variable, check it is not a dynamic string. //Check declaration VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); ExpressionTree initializer = declaration.initializer(); if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) { return true; } //check usages by revisiting the enclosing tree. Collection<IdentifierTree> usages = symbol.usages(); LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree); Tree argEnclosingDeclarationTree = semanticModel.getTree(semanticModel.getEnv(symbol)); argEnclosingDeclarationTree.accept(visitor); return visitor.dynamicString; } //arg is not a local variable nor a constant, so it is a parameter or a field. parameterName = "\""+arg.name()+"\""; return symbol.owner().isMethodSymbol() && !firstLevel; }
protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) { Symbol symbol = arg.symbol(); if (isExcluded(currentlyChecking, symbol)) { return false; } Tree enclosingBlockTree = getSemanticModel().getTree(getSemanticModel().getEnv(methodTree)); Tree argEnclosingDeclarationTree = getSemanticModel().getTree(getSemanticModel().getEnv(symbol)); if (enclosingBlockTree.equals(argEnclosingDeclarationTree)) { //symbol is a local variable, check it is not a dynamic string. //Check declaration VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); ExpressionTree initializer = declaration.initializer(); if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) { return true; } //check usages by revisiting the enclosing tree. Collection<IdentifierTree> usages = symbol.usages(); LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree); argEnclosingDeclarationTree.accept(visitor); return visitor.dynamicString; } //arg is not a local variable nor a constant, so it is a parameter or a field. parameterName = arg.name(); return symbol.owner().isMethodSymbol() && !firstLevel; }
protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) { Symbol symbol = arg.symbol(); if (isExcluded(currentlyChecking, symbol)) { return false; } boolean isLocalVar = symbol.owner().isMethodSymbol() && !((JavaSymbol.MethodJavaSymbol) symbol.owner()).getParameters().scopeSymbols().contains(symbol); if (isLocalVar) { //symbol is a local variable, check it is not a dynamic string. //Check declaration VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); ExpressionTree initializer = declaration.initializer(); if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) { return true; } //check usages by revisiting the enclosing tree. Collection<IdentifierTree> usages = symbol.usages(); LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree); Tree argEnclosingDeclarationTree = semanticModel.getTree(semanticModel.getEnv(symbol)); argEnclosingDeclarationTree.accept(visitor); return visitor.dynamicString; } //arg is not a local variable nor a constant, so it is a parameter or a field. parameterName = "\""+arg.name()+"\""; return symbol.owner().isMethodSymbol() && !firstLevel; }
private static boolean onlySynchronizedUsages(Symbol.VariableSymbol variable) { List<IdentifierTree> usages = variable.usages(); if (usages.isEmpty()) { return false; } for (IdentifierTree usage : usages) { SynchronizedStatementTree synchronizedStatementTree = getParentSynchronizedStatement(usage); if (synchronizedStatementTree == null) { // used outside a synchronized statement return false; } else { ExpressionTree expression = synchronizedStatementTree.expression(); if (!expression.is(Tree.Kind.IDENTIFIER) || !variable.equals(((IdentifierTree) expression).symbol())) { // variable is not the expression synchronized return false; } // check other usages } } return true; }
@Override public void visitClass(ClassTree tree) { IdentifierTree simpleName = tree.simpleName(); if (simpleName != null) { Symbol.TypeSymbol classSymbol = tree.symbol(); Collection<Symbol> members = classSymbol.memberSymbols(); for (Symbol sym : members) { if (sym.isVariableSymbol() && !staticFieldSameType(classSymbol, sym)) { //Exclude static fields of the same type. fields.add(((Symbol.VariableSymbol) sym).declaration()); } } currentClassName = simpleName.name(); } super.visitClass(tree); currentClassName = ""; fields.clear(); }
@Nullable private String getRetentionValue(Object value) { String retentionValue = null; if (value instanceof Tree) { Tree tree = (Tree) value; if (tree.is(Tree.Kind.MEMBER_SELECT)) { retentionValue = ((MemberSelectExpressionTree) tree).identifier().name(); } else if (tree.is(Tree.Kind.IDENTIFIER)) { retentionValue = ((IdentifierTree) tree).name(); } } else if (value instanceof Symbol.VariableSymbol) { retentionValue = ((Symbol.VariableSymbol) value).name(); } return retentionValue; } }
private boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) { Symbol variableSymbol = identifier.symbol(); if (!variableSymbol.isVariableSymbol()) { return false; } VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration(); if (variableDefinition != null) { ExpressionTree initializer = variableDefinition.initializer(); if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) { MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration(); return method != null && isCompareToInvocation((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method); } } return false; }
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) { Symbol variableSymbol = identifier.symbol(); if (!variableSymbol.isVariableSymbol()) { return false; } VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration(); if (variableDefinition != null) { ExpressionTree initializer = variableDefinition.initializer(); if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) { MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration(); return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method); } } return false; }
private void checkModifiers(Symbol.VariableSymbol serialVersionUidSymbol) { List<String> missingModifiers = Lists.newArrayList(); if (!serialVersionUidSymbol.isStatic()) { missingModifiers.add("static"); } if (!serialVersionUidSymbol.isFinal()) { missingModifiers.add("final"); } if (!serialVersionUidSymbol.type().is("long")) { missingModifiers.add("long"); } VariableTree variableTree = serialVersionUidSymbol.declaration(); if (variableTree != null && !missingModifiers.isEmpty()) { reportIssue(variableTree.simpleName(), "Make this \"serialVersionUID\" field \"" + Joiner.on(' ').join(missingModifiers) + "\"."); } }
@Override public void visitClass(ClassTree tree) { IdentifierTree simpleName = tree.simpleName(); if (simpleName != null) { Symbol.TypeSymbol classSymbol = tree.symbol(); Collection<Symbol> members = classSymbol.memberSymbols(); for (Symbol sym : members) { if (sym.isVariableSymbol() && !staticFieldSameType(classSymbol, sym)) { //Exclude static fields of the same type. fields.add(((Symbol.VariableSymbol) sym).declaration()); } } currentClassName = simpleName.name(); } super.visitClass(tree); currentClassName = ""; fields.clear(); }
@Nullable private static String getRetentionValue(Object value) { String retentionValue = null; if (value instanceof Tree) { Tree tree = (Tree) value; if (tree.is(Tree.Kind.MEMBER_SELECT)) { retentionValue = ((MemberSelectExpressionTree) tree).identifier().name(); } else if (tree.is(Tree.Kind.IDENTIFIER)) { retentionValue = ((IdentifierTree) tree).name(); } } else if (value instanceof Symbol.VariableSymbol) { retentionValue = ((Symbol.VariableSymbol) value).name(); } return retentionValue; } }
@Nullable private static String getRetentionValue(Object value) { String retentionValue = null; if (value instanceof Tree) { Tree tree = (Tree) value; if (tree.is(Tree.Kind.MEMBER_SELECT)) { retentionValue = ((MemberSelectExpressionTree) tree).identifier().name(); } else if (tree.is(Tree.Kind.IDENTIFIER)) { retentionValue = ((IdentifierTree) tree).name(); } } else if (value instanceof Symbol.VariableSymbol) { retentionValue = ((Symbol.VariableSymbol) value).name(); } return retentionValue; } }
@CheckForNull private static ExpressionTree retrievedPropertyDefaultValue(IdentifierTree identifier) { Symbol symbol = identifier.symbol(); if (symbol.usages().size() == 1) { VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); if (declaration != null) { ExpressionTree initializer = declaration.initializer(); if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION)) { return retrievedPropertyDefaultValue((MethodInvocationTree) initializer); } } } return null; }
@Override public void leaveFile(JavaFileScannerContext context) { for (VariableSymbol var : variablesToReport) { VariableTree declaration = var.declaration(); if (declaration != null) { reportIssue(declaration.simpleName(), MESSAGE); } } for (MethodInvocationTree mit : settersToReport) { reportIssue(mit.arguments(), MESSAGE); } for (NewClassTree newClassTree : newClassToReport) { reportIssue(newClassTree, MESSAGE); } }
private void checkModifiers(Symbol.VariableSymbol serialVersionUidSymbol) { List<String> missingModifiers = Lists.newArrayList(); if (!serialVersionUidSymbol.isStatic()) { missingModifiers.add("static"); } if (!serialVersionUidSymbol.isFinal()) { missingModifiers.add("final"); } if (!serialVersionUidSymbol.type().is("long")) { missingModifiers.add("long"); } VariableTree variableTree = serialVersionUidSymbol.declaration(); if (variableTree != null && !missingModifiers.isEmpty()) { reportIssue(variableTree.simpleName(), "Make this \"serialVersionUID\" field \"" + Joiner.on(' ').join(missingModifiers) + "\"."); } }
@Override public void leaveFile(JavaFileScannerContext context) { for (VariableSymbol var : variablesToReport) { VariableTree declaration = var.declaration(); if (declaration != null) { reportIssue(declaration.simpleName(), MESSAGE); } } for (MethodInvocationTree mit : settersToReport) { reportIssue(mit.arguments(), MESSAGE); } for (NewClassTree newClassTree : newClassToReport) { reportIssue(newClassTree, MESSAGE); } }
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) { Symbol variableSymbol = identifier.symbol(); if (!variableSymbol.isVariableSymbol()) { return false; } VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration(); if (variableDefinition != null) { ExpressionTree initializer = variableDefinition.initializer(); if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) { MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration(); return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method); } } return false; }