private static Optional<Symbol> findIteratorMethod(Symbol parameter) { return parameter.type().symbol().lookupSymbols("iterator").stream() .filter(Symbol::isMethodSymbol) .filter(s -> ((Symbol.MethodSymbol) s).parameterTypes().isEmpty()) .findFirst(); }
private static Optional<Symbol> findIteratorMethod(Symbol parameter) { return parameter.type().symbol().lookupSymbols("iterator").stream() .filter(Symbol::isMethodSymbol) .filter(s -> ((Symbol.MethodSymbol) s).parameterTypes().isEmpty()) .findFirst(); }
private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) { MethodTree declaration = functionalMethod.declaration(); if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) { return Optional.empty(); } Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type(); return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream() .map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)) .filter(Objects::nonNull) .findFirst(); }
private boolean hasSameArity(Symbol.MethodSymbol methodUsed, MethodInvocationTree mit) { int formalArity = methodUsed.parameterTypes().size(); int invokedArity = mit.arguments().size(); return formalArity == invokedArity || (((JavaSymbol.MethodJavaSymbol) methodUsed).isVarArgs() && invokedArity >= formalArity - 1); }
private boolean declaresCloneMethod(Symbol.TypeSymbol classSymbol) { for (Symbol memberSymbol : classSymbol.lookupSymbols("clone")) { if (memberSymbol.isMethodSymbol()) { Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) memberSymbol; if (methodSymbol.parameterTypes().isEmpty()) { return true; } } } return false; }
private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) { MethodTree declaration = functionalMethod.declaration(); if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) { return Optional.empty(); } Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type(); return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream() .map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)) .filter(Objects::nonNull) .findFirst(); }
private static ExpressionTree getActualArgument(MethodInvocationTree mit) { int arity = mit.arguments().size(); ExpressionTree arg = mit.arguments().get(arity - 1); // Check for assert equals method with delta if (arity > 2 && (arity == 4 || ((Symbol.MethodSymbol) mit.symbol()).parameterTypes().stream().allMatch(AssertionArgumentOrderCheck::isDoubleOrFloat))) { // last arg is actually delta, take the previous last to get the actual arg. arg = mit.arguments().get(arity - 2); } return arg; }
@Override protected void onConstructorFound(NewClassTree newClassTree) { Symbol symbol = newClassTree.constructorSymbol(); if (symbol.isMethodSymbol()) { Symbol.MethodSymbol constructor = (Symbol.MethodSymbol) symbol; String signature = constructor.owner().name() + "(" + Joiner.on(',').join(constructor.parameterTypes()) + ")"; reportIssue(newClassTree.identifier(), "Remove this use of constructor \"" + signature + "\""); } }
private static Optional<String> isSetterLike(Symbol.MethodSymbol methodSymbol) { if (methodSymbol.parameterTypes().size() != 1 || isPrivateStaticOrAbstract(methodSymbol)) { return Optional.empty(); } String methodName = methodSymbol.name(); if (methodName.length() > 3 && methodName.startsWith("set") && methodSymbol.returnType().type().isVoid()) { return Optional.of(lowerCaseFirstLetter(methodName.substring(3))); } return Optional.empty(); }
private boolean isEqualsMethod(Symbol symbol) { if (symbol.isMethodSymbol()) { List<Type> parameterTypes = ((Symbol.MethodSymbol) symbol).parameterTypes(); return !parameterTypes.isEmpty() && parameterTypes.get(0).is("java.lang.Object"); } return false; } }
@Override protected void onConstructorFound(NewClassTree newClassTree) { NewClassTreeImpl newClassTreeImpl = (NewClassTreeImpl) newClassTree; IdentifierTree constructorIdentifier = newClassTreeImpl.getConstructorIdentifier(); Symbol symbol = constructorIdentifier.symbol(); if(symbol.isMethodSymbol()) { Symbol.MethodSymbol constructor = (Symbol.MethodSymbol) symbol; String signature = constructor.owner().name() + "(" + Joiner.on(',').join(constructor.parameterTypes()) + ")"; addIssue(newClassTree, "Remove this use of constructor \"" + signature + "\""); } }
private static boolean isNoArgConstructor(Symbol constructor) { return ((Symbol.MethodSymbol) constructor).parameterTypes().isEmpty(); } }
private static boolean hasNonPrivateNoArgConstructor(Type type) { Collection<Symbol> constructors = type.symbol().lookupSymbols("<init>"); for (Symbol member : constructors) { if (member.isMethodSymbol()) { Symbol.MethodSymbol method = (Symbol.MethodSymbol) member; if (method.parameterTypes().isEmpty() && !method.isPrivate()) { return true; } } } return constructors.isEmpty(); }
private boolean hasNonPrivateNoArgConstructor(Type type) { Collection<Symbol> constructors = type.symbol().lookupSymbols("<init>"); for (Symbol member : constructors) { if (member.isMethodSymbol()) { Symbol.MethodSymbol method = (Symbol.MethodSymbol) member; if (method.parameterTypes().isEmpty() && !method.isPrivate()) { return true; } } } return constructors.isEmpty(); }
private void checkMethodInvocationArgument(Arguments arguments, Symbol symbol) { if (symbol.isMethodSymbol()) { List<Type> parametersTypes = ((Symbol.MethodSymbol) symbol).parameterTypes(); if (arguments.size() == parametersTypes.size()) { int i = 0; for (Type argType : parametersTypes) { checkExpression(argType, arguments.get(i)); i++; } } } }
private boolean hasSameArity(Symbol.MethodSymbol methodUsed, MethodInvocationTree mit) { int formalArity = methodUsed.parameterTypes().size(); int invokedArity = mit.arguments().size(); return formalArity == invokedArity || (((JavaSymbol.MethodJavaSymbol) methodUsed).isVarArgs() && invokedArity >= formalArity - 1); }
private static Optional<String> isSetterLike(Symbol.MethodSymbol methodSymbol) { if (methodSymbol.parameterTypes().size() != 1 || isPrivateStaticOrAbstract(methodSymbol)) { return Optional.empty(); } String methodName = methodSymbol.name(); if (methodName.length() > 3 && methodName.startsWith("set") && methodSymbol.returnType().type().isVoid()) { return Optional.of(lowerCaseFirstLetter(methodName.substring(3))); } return Optional.empty(); }
private static boolean hasNonPrivateNoArgConstructor(Type type) { Collection<Symbol> constructors = type.symbol().lookupSymbols("<init>"); for (Symbol member : constructors) { if (member.isMethodSymbol()) { Symbol.MethodSymbol method = (Symbol.MethodSymbol) member; if (method.parameterTypes().isEmpty() && !method.isPrivate()) { return true; } } } return constructors.isEmpty(); }
@Override protected void onConstructorFound(NewClassTree newClassTree) { Symbol symbol = newClassTree.constructorSymbol(); if (symbol.isMethodSymbol()) { Symbol.MethodSymbol constructor = (Symbol.MethodSymbol) symbol; String signature = constructor.owner().name() + "(" + Joiner.on(',').join(constructor.parameterTypes()) + ")"; reportIssue(newClassTree.identifier(), "Remove this use of constructor \"" + signature + "\""); } }
private static ExpressionTree getActualArgument(MethodInvocationTree mit) { int arity = mit.arguments().size(); ExpressionTree arg = mit.arguments().get(arity - 1); // Check for assert equals method with delta if (arity > 2 && (arity == 4 || ((Symbol.MethodSymbol) mit.symbol()).parameterTypes().stream().allMatch(AssertionArgumentOrderCheck::isDoubleOrFloat))) { // last arg is actually delta, take the previous last to get the actual arg. arg = mit.arguments().get(arity - 2); } return arg; }