@Override @CheckForNull public List<AnnotationValue> valuesForAnnotation(String fullyQualifiedNameOfAnnotation) { for (AnnotationInstance annotationInstance : annotations) { if(annotationInstance.symbol().type().is(fullyQualifiedNameOfAnnotation)) { return annotationInstance.values(); } } 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 void handleParameter(Symbol parameter, boolean springInjectionAnnotated) { Type leastSpecificType = findLeastSpecificType(parameter); if (parameter.type() != leastSpecificType && !leastSpecificType.is("java.lang.Object")) { String suggestedType = getSuggestedType(springInjectionAnnotated, leastSpecificType); reportIssue(parameter.declaration(), String.format("Use '%s' here; it is a more general type than '%s'.", suggestedType, parameter.type().name())); } }
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); }
@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 boolean isSearchedMethod(MethodSymbol symbol, @Nullable Type callSiteType) { boolean result = nameAcceptable(symbol) && parametersAcceptable(symbol); if (typeDefinition != null) { result &= typeDefinition.test(symbol.owner().type()); } if (callSite != null) { result &= callSiteType != null && callSite.test(callSiteType); } return result; }
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")); }
@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 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 public void visitInstanceOf(InstanceOfTree tree) { if (tree.type().symbolType().equals(owner.type().erasure())) { if (!isOwnerFinal() && !methodSymbol.isFinal()) { reportIssue(tree, "Compare to \"this.getClass()\" instead."); } } else { reportIssue(tree, "Remove this comparison to an unrelated class."); } super.visitInstanceOf(tree); }
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() + "\""; }
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 checkIncrementDecrement(Tree tree, IdentifierTree identifier) { Type symbolType = identifier.symbol().type(); if (symbolType.is("int") || symbolType.is("java.lang.Integer")) { reportIssueIfNotInExcludedContext(tree, "AtomicInteger"); } else if (symbolType.is("long") || symbolType.is("java.lang.Long")) { reportIssueIfNotInExcludedContext(tree, "AtomicLong"); } }
private static Type getMethodOwner(MethodInvocationTree mit) { if (mit.methodSelect().is(Kind.MEMBER_SELECT)) { return ((MemberSelectExpressionTree) mit.methodSelect()).expression().symbolType(); } return mit.symbol().owner().type(); }
@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.symbol().isMethodSymbol()) { String className = tree.symbol().owner().type().fullyQualifiedName(); checkIfDisallowed(className, tree.methodSelect()); } 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); }
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))); }); } }