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 void checkForEachIdentifier(Map<Symbol, Integer> forEachSymbols, IdentifierTree node) { Symbol symbol = node.symbol(); if (symbol.owner().isMethodSymbol()) { if (forEachSymbols.containsKey(symbol)) { addIssue(node, forEachSymbols.get(symbol)); } else { forEachSymbols.put(symbol, ((JavaTree) node).getLine()); } } }
private boolean isLocalExpression(@Nullable ExpressionTree expression) { if (expression == null) { return false; } if (expression.is(Tree.Kind.IDENTIFIER)) { final Symbol symbol = ((IdentifierTree) expression).symbol().owner(); return symbol.isMethodSymbol(); } return true; }
private static String getDefinitionPlace(Symbol symbol, Symbol knownMemberSymbol) { if (sameOwner(symbol, knownMemberSymbol)) { int declarationLine = getDeclarationLine(knownMemberSymbol); if (declarationLine == -1) { return ""; } return " defined on line " + declarationLine; } return " defined in " + (knownMemberSymbol.owner().isInterface() ? "interface" : "superclass") + " \"" + knownMemberSymbol.owner().type().fullyQualifiedName() + "\""; }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if (this.owner == tree.symbol().owner()) { return; } tree.arguments().stream().filter(ExpressionUtils::isThis).forEach(this::report); super.visitMethodInvocation(tree); }
private static boolean isStaticField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.owner().isTypeSymbol() && symbol.isStatic(); }
private static boolean isFinalField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.isFinal() && symbol.owner().isTypeSymbol(); }
private static boolean isLocalVariableNotInConstructorOrStaticMain(ExpressionTree expression) { if (expression.is(Kind.IDENTIFIER)) { Symbol symbol = ((IdentifierTree) expression).symbol().owner(); return symbol.isMethodSymbol() && !("<init>".equals(symbol.name()) || ("main".equals(symbol.name()) && symbol.isStatic())); } return false; }
private void checkConstructor(Symbol constructorClass) { if (!constructorClass.isStatic()) { Symbol methodPackage = getPackage(methodScopeOwner); Symbol constructorPackage = getPackage(constructorClass); if (Objects.equals(methodPackage, constructorPackage) && !constructorClass.owner().isPackageSymbol()) { setNonStaticReference(); } } }
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 static boolean isExpectedSetter(MethodInvocationTree mit) { return mit.arguments().size() == 1 && mit.symbol().isMethodSymbol() && CLASSES.stream().anyMatch(mit.symbol().owner().type()::isSubtypeOf) && SETTER_NAMES.contains(getIdentifier(mit).name()) && isIgnoredBuilder(mit); }
private static boolean nonLocalAssignment(Tree syntaxNode) { if (syntaxNode.is(Tree.Kind.ASSIGNMENT)) { ExpressionTree variable = ((AssignmentExpressionTree) syntaxNode).variable(); return !variable.is(Tree.Kind.IDENTIFIER) || ((IdentifierTree) variable).symbol().owner().isTypeSymbol(); } return false; }
private static boolean isGetPropertyWithDefaultValue(MethodInvocationTree mit) { Symbol symbol = mit.symbol(); if (symbol.isMethodSymbol() && symbol.owner().type().is("java.util.Properties")) { return "getProperty".equals(symbol.name()) && mit.arguments().size() == 2; } return false; } }
@Override protected void onMethodInvocationFound(MethodInvocationTree mit) { if (mit.symbol().owner().type().is(SECRET_KEY_FACTORY)) { String algorithm = ConstantUtils.resolveAsStringConstant(mit.arguments().get(0)); if (algorithm == null || !algorithm.startsWith("PBKDF2")) { return; } } reportIssue(ExpressionUtils.methodName(mit), MESSAGE); }
private static Type getMethodOwnerType(MethodInvocationTree methodSelectTree) { if (methodSelectTree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) { return ((MemberSelectExpressionTree) methodSelectTree.methodSelect()).expression().symbolType(); } else { return methodSelectTree.symbol().owner().type(); } }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if(tree.symbol().isMethodSymbol()) { String className = tree.symbol().owner().type().fullyQualifiedName(); checkIfDisallowed(className, tree.methodSelect()); } super.visitMethodInvocation(tree); }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if(tree.symbol().isMethodSymbol()) { Type type = tree.symbol().owner().type(); if (isCheckedType(type)) { reportIssue(ExpressionUtils.methodName(tree), "Remove this assertion."); } } super.visitMethodInvocation(tree); }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { if (tree.methodSelect().is(Tree.Kind.IDENTIFIER)) { Symbol symbol = ((IdentifierTree) tree.methodSelect()).symbol(); usesThisInstance |= symbol.isMethodSymbol() && !symbol.isStatic() && instanceType.isSubtypeOf(symbol.owner().type()); } super.visitMethodInvocation(tree); }