private static Symbol.VariableSymbol findSerialVersionUid(Symbol.TypeSymbol symbol) { for (Symbol member : symbol.lookupSymbols("serialVersionUID")) { if (member.isVariableSymbol()) { return (Symbol.VariableSymbol) member; } } return null; }
@Override public void visitIdentifier(IdentifierTree tree) { Symbol symbol = tree.symbol(); if (symbol.isVariableSymbol()) { symbols.add(symbol); } super.visitIdentifier(tree); } }
private static Collection<Symbol> filterMethodsAndFields(Collection<Symbol> symbols) { List<Symbol> filtered = Lists.newArrayList(); for (Symbol symbol : symbols) { if ((symbol.isVariableSymbol() && !isThisOrSuper(symbol)) || (symbol.isMethodSymbol() && !isConstructor(symbol))) { filtered.add(symbol); } } return filtered; }
@CheckForNull private static ExpressionTree getInitializer(IdentifierTree tree) { Symbol symbol = tree.symbol(); if (symbol.isVariableSymbol()) { VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); if (declaration != null) { return declaration.initializer(); } } return null; }
@CheckForNull private ExpressionTree getInitializer(IdentifierTree tree) { Symbol symbol = tree.symbol(); if(symbol.isVariableSymbol()) { VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); if(declaration != null) { return declaration.initializer(); } } return null; }
private void onMemberSelectExpressionFound(IdentifierTree identifierTree) { Symbol symbol = identifierTree.symbol(); if (symbol.isVariableSymbol() && symbol.owner().type().is("com.google.common.base.Charsets")) { String identifier = identifierTree.name(); String aliasedIdentigier = identifier.replace("_", "-"); if (STANDARD_CHARSETS.stream().anyMatch(c -> c.name().equals(aliasedIdentigier))) { reportIssue(identifierTree, "Replace \"com.google.common.base.Charsets." + identifier + "\" with \"StandardCharsets." + identifier + "\"."); } } }
private static boolean isStaticField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.owner().isTypeSymbol() && symbol.isStatic(); }
@CheckForNull private static String nonNullFieldAnnotation(Symbol symbol) { if (symbol.isVariableSymbol() && symbol.owner().isTypeSymbol() && !isUsingNullable(symbol) && valuesForGlobalAnnotation((JavaSymbol) symbol, ORG_SPRINGFRAMEWORK_LANG_NON_NULL_FIELDS) != null) { return ORG_SPRINGFRAMEWORK_LANG_NON_NULL_FIELDS; } return null; }
private static boolean isFinalField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.isFinal() && symbol.owner().isTypeSymbol(); }
@Override public void visitIdentifier(IdentifierTree tree) { if (tree.symbol().isVariableSymbol()) { final String name = tree.symbol().name(); if (!methodParams.contains(name) && annotatedVariables.contains(name)) { context.reportIssue(javaCheck, tree, RULE_MESSAGE); } } super.visitIdentifier(tree); } }
@Override public void visitAssignmentExpression(AssignmentExpressionTree tree) { ExpressionTree variable = tree.variable(); if (variable.is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifier = (IdentifierTree) variable; Symbol reference = identifier.symbol(); if (reference.isVariableSymbol() && variables.contains(reference)) { context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\"."); } } }
@Override public void visitAssignmentExpression(AssignmentExpressionTree tree) { ExpressionTree variable = tree.variable(); if (variable.is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifier = (IdentifierTree) variable; Symbol reference = identifier.symbol(); if (reference.isVariableSymbol() && variables.contains(reference)) { context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\"."); } } }
private boolean isFinalFieldWithInitializer(@Nullable Symbol symbol) { if (symbol != null && symbol.isVariableSymbol() && symbol.owner().isTypeSymbol() && symbol.isFinal()) { VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration(); return declaration != null && declaration.initializer() != null; } return false; }
private boolean checkMembers(IdentifierTree fieldSimpleName, Symbol.TypeSymbol symbol) { for (Symbol member : symbol.memberSymbols()) { if (member.isVariableSymbol() && !member.isPrivate() && !member.isStatic() && member.name().equals(fieldSimpleName.name())) { reportIssue(fieldSimpleName, String.format("\"%s\" is the name of a field in \"%s\".", fieldSimpleName.name(), symbol.name())); return true; } } return false; }
private static boolean shouldVerify(AssignmentExpressionTree assignment) { if (assignment.expression().is(Tree.Kind.NEW_CLASS) && assignment.variable().is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifier = (IdentifierTree) assignment.variable(); boolean isMethodVariable = identifier.symbol().isVariableSymbol() && identifier.symbol().owner().isMethodSymbol(); boolean isSupportedClass = CLASSES.stream().anyMatch(identifier.symbolType()::isSubtypeOf) || CLASSES.stream().anyMatch(assignment.expression().symbolType()::isSubtypeOf); return isMethodVariable && isSupportedClass; } return false; }
private void addToUnsecuredCookies(VariableTree variableTree) { ExpressionTree initializer = variableTree.initializer(); Symbol variableTreeSymbol = variableTree.symbol(); // Ignore field variables if (initializer != null && variableTreeSymbol.isVariableSymbol() && variableTreeSymbol.owner().isMethodSymbol()) { boolean isInitializedWithConstructor = initializer.is(Tree.Kind.NEW_CLASS); boolean isMatchedType = isCookieClass(variableTreeSymbol.type()) || isCookieClass(initializer.symbolType()); if (isInitializedWithConstructor && isMatchedType && isSecureParamFalse((NewClassTree) initializer)) { unsecuredCookies.add((Symbol.VariableSymbol) variableTreeSymbol); } } }
private void addToUnsecuredCookies(VariableTree variableTree) { ExpressionTree initializer = variableTree.initializer(); Symbol variableTreeSymbol = variableTree.symbol(); // Ignore field variables if (initializer != null && variableTreeSymbol.isVariableSymbol() && variableTreeSymbol.owner().isMethodSymbol()) { boolean isInitializedWithConstructor = initializer.is(Tree.Kind.NEW_CLASS); boolean isMatchedType = isCookieClass(variableTreeSymbol.type()) || isCookieClass(initializer.symbolType()); if (isInitializedWithConstructor && isMatchedType && isSecureParamFalse((NewClassTree) initializer)) { unsecuredCookies.add((Symbol.VariableSymbol) variableTreeSymbol); } } }
private static int getDeclarationLine(Symbol symbol) { if (symbol.declaration() == null) { return -1; } if (symbol.isVariableSymbol()) { return ((Symbol.VariableSymbol) symbol).declaration().simpleName().identifierToken().line(); } return ((Symbol.MethodSymbol) symbol).declaration().simpleName().identifierToken().line(); }
private static int getDeclarationLine(Symbol symbol) { if (symbol.declaration() == null) { return -1; } if (symbol.isVariableSymbol()) { return ((Symbol.VariableSymbol) symbol).declaration().simpleName().identifierToken().line(); } return ((Symbol.MethodSymbol) symbol).declaration().simpleName().identifierToken().line(); }
private static boolean verifyCondition(ExpressionTree condition) { if(condition.is(Tree.Kind.IDENTIFIER)) { IdentifierTree identifierTree = (IdentifierTree) condition; if(identifierTree.symbol().isFinal() && identifierTree.symbol().isVariableSymbol()) { VariableTree declaration = (VariableTree) identifierTree.symbol().declaration(); return declaration == null || declaration.initializer() == null || !declaration.initializer().is(Tree.Kind.BOOLEAN_LITERAL); } } return !condition.is(Tree.Kind.BOOLEAN_LITERAL); }