@Override public void visitMethodInvocation(MethodInvocationTree tree) { Symbol symbol = tree.symbol(); if (!symbol.isUnknown() && symbol.owner().type().isSubtypeOf("java.lang.Thread") && "interrupt".equals(symbol.name()) && tree.arguments().isEmpty()) { threadInterrupted = true; return; } super.visitMethodInvocation(tree); }
@Override public void visitNode(Tree tree) { TypeSymbol classSymbol = ((ClassTree) tree).symbol(); Set<Symbol> fieldsReadOnAnotherInstance = FieldsReadOnAnotherInstanceVisitor.getFrom(tree); classSymbol.memberSymbols().stream() .filter(PrivateFieldUsedLocallyCheck::isPrivateField) .filter(s -> !(s.isFinal() && s.isStatic())) .filter(s -> !hasAnnotation(s)) .filter(s -> !s.usages().isEmpty()) .filter(s -> !fieldsReadOnAnotherInstance.contains(s)) .forEach(s -> checkPrivateField(s, classSymbol)); }
private static boolean isMethodFromCollection(Symbol methodSymbol) { if (!methodSymbol.isMethodSymbol()) { return false; } Type ownerType = methodSymbol.owner().type(); return !ownerType.is("java.util.Collection") && ownerType.isSubtypeOf("java.util.Collection"); }
private static boolean isFinalField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.isFinal() && symbol.owner().isTypeSymbol(); }
private static boolean isStaticField(Symbol symbol) { return symbol.isVariableSymbol() && symbol.owner().isTypeSymbol() && symbol.isStatic(); }
private static boolean isTypeInDefaultPackage(Symbol symbol) { if (!symbol.isTypeSymbol()) { return false; } Symbol parent = symbol.owner(); while (!parent.isPackageSymbol()) { parent = parent.owner(); } return parent.name().isEmpty(); }
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 boolean isIntermediateOperation(MethodInvocationTree mit) { if (BASE_STREAM_INTERMEDIATE_OPERATIONS.anyMatch(mit)) { return true; } Symbol method = mit.symbol(); return method.isMethodSymbol() && !method.isStatic() && STREAM_TYPES.contains(method.owner().type().fullyQualifiedName()) && STREAM_TYPES.contains(((Symbol.MethodSymbol) method).returnType().type().fullyQualifiedName()); }
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; } }
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 static boolean hasDefaultConstructor(Collection<Symbol> members) { for (Symbol member : members) { if ("<init>".equals(member.name()) && member.declaration() == null) { return true; } } return false; }
private void checkSymbol(Symbol symbol) { for (IdentifierTree usageIdentifier : symbol.usages()) { Tree parent = usageIdentifier.parent(); if (parent.is(Kind.MEMBER_SELECT) && ((MemberSelectExpressionTree) parent).expression().is(Kind.MEMBER_SELECT)) { reportIssue(parent, String.format(MESSAGE, symbol.name())); } } }
@Override public void visitMethodInvocation(MethodInvocationTree tree) { Symbol method = tree.symbol(); if ("next".equals(method.name()) && tree.arguments().isEmpty() && isIteratorMethod(method) && (hasNextOwner == method.owner() || hasNextOwner.type().isSubtypeOf(method.owner().type()))) { reportIssue(ExpressionUtils.methodName(tree), "Refactor the implementation of this \"Iterator.hasNext()\" method to not call \"Iterator.next()\"."); } super.visitMethodInvocation(tree); }
private void checkMainMethodArgsUsage(MethodTree mainMethod) { VariableTree commandLineParameters = mainMethod.parameters().get(0); List<IdentifierTree> argsUsages = commandLineParameters.symbol().usages(); if (!argsUsages.isEmpty()) { List<JavaFileScannerContext.Location> secondaries = argsUsages.stream().map(usage -> new JavaFileScannerContext.Location("", usage)).collect(Collectors.toList()); reportIssue(commandLineParameters, MESSAGE, secondaries, null); } }
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 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 isStrutsActionParameter(VariableTree variableTree) { Type superClass = variableTree.symbol().enclosingClass().superClass(); return superClass != null && superClass.isSubtypeOf(STRUTS_ACTION_SUPERCLASS) && EXCLUDED_STRUTS_ACTION_PARAMETER_TYPES.contains(variableTree.symbol().type().fullyQualifiedName()); }
private boolean isInherited(Symbol symbol) { Type methodOwnerType = symbol.owner().type().erasure(); Type innerType = classSymbol.type().erasure(); return !symbol.isStatic() && innerType.isSubtypeOf(methodOwnerType) && !classSymbol.owner().type().equals(methodOwnerType) && !innerType.equals(methodOwnerType); }