private static boolean hasUniqueConstructor(ClassTree clazzTree) { return clazzTree.symbol().memberSymbols().stream() .filter(Symbol::isMethodSymbol) .map(s -> (Symbol.MethodSymbol) s) .filter(m -> m.name().equals("<init>")) .filter(m -> m.declaration() != null) .count() == 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 void checkJunit3TestClass(IdentifierTree className, Stream<Symbol.MethodSymbol> members) { if (members.noneMatch(m -> m.name().startsWith("test"))) { reportClass(className); } }
private void checkNotStatic(MethodTree methodTree) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (methodSymbol.isStatic()) { reportIssue(methodTree.simpleName(), "The \"static\" modifier should not be applied to \"" + methodSymbol.name() + "\"."); } }
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(); }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; Symbol.MethodSymbol methodSymbol = methodTree.symbol(); boolean isMethodInJunit3 = isWithinJunit3TestCase(methodSymbol) && isSetUpOrTearDown(methodSymbol); if (isMethodInJunit3 && requiresSuperCall(methodSymbol) && !callSuperOnOverride(methodTree.block(), methodSymbol)) { addIssue(tree, String.format("Add a \"super.%s()\" call to this method.", methodSymbol.name())); } }
private static boolean haveSameParamButLast(Symbol.MethodSymbol enclosing, JavaSymbol.MethodJavaSymbol methodSymbol) { return enclosing.name().equals(methodSymbol.name()) && IntStream.range(0, enclosing.parameterTypes().size()).allMatch(i -> enclosing.parameterTypes().get(i) == methodSymbol.parameterTypes().get(i)); }
private static boolean requiresSuperCall(Symbol.MethodSymbol methodSymbol) { Type superType = methodSymbol.owner().type().symbol().superClass(); Collection<Symbol> symbols = Lists.newArrayList(); while (superType != null && !superType.is(JUNIT_FRAMEWORK_TEST_CASE) && symbols.isEmpty()) { symbols = superType.symbol().lookupSymbols(methodSymbol.name()); superType = superType.symbol().superClass(); } return !symbols.isEmpty() && !symbols.iterator().next().owner().type().is(JUNIT_FRAMEWORK_TEST_CASE); }
private void checkJunit3TestClass(IdentifierTree className, Stream<Symbol.MethodSymbol> members) { if (members.noneMatch(m -> m.name().startsWith("test"))) { reportClass(className); } }
private static boolean isIteratorNextMethod(Symbol.MethodSymbol symbol) { return "next".equals(symbol.name()) && symbol.parameterTypes().isEmpty() && isIterator(symbol.enclosingClass()); }
private static boolean isOverloaded(Symbol.MethodSymbol methodSymbol) { return ((Symbol.TypeSymbol) methodSymbol.owner()).lookupSymbols(methodSymbol.name()).size() > 1; }
private boolean nameAcceptable(MethodSymbol symbol) { Preconditions.checkState(methodName != null); return methodName.test(symbol.name()); }
private void checkNotStatic(MethodTree methodTree) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (methodSymbol.isStatic()) { reportIssue(methodTree.simpleName(), "The \"static\" modifier should not be applied to \"" + methodSymbol.name() + "\"."); } }
private static boolean callSuperOnOverride(@Nullable BlockTree block, Symbol.MethodSymbol methodSymbol) { if (block == null) { return false; } InvocationVisitor visitor = new InvocationVisitor(methodSymbol.name()); block.accept(visitor); return visitor.superCallOnOverride; }
private static boolean requiresSuperCall(Symbol.MethodSymbol methodSymbol) { Type superType = methodSymbol.owner().type().symbol().superClass(); Collection<Symbol> symbols = Lists.newArrayList(); while (superType != null && !superType.is(JUNIT_FRAMEWORK_TEST_CASE) && symbols.isEmpty()) { symbols = superType.symbol().lookupSymbols(methodSymbol.name()); superType = superType.symbol().superClass(); } return !symbols.isEmpty() && !symbols.iterator().next().owner().type().is(JUNIT_FRAMEWORK_TEST_CASE); }
private void checkReturnType(MethodTree methodTree, String requiredReturnType) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (!methodSymbol.returnType().type().is(requiredReturnType)) { reportIssue(methodTree.simpleName(), "\"" + methodSymbol.name() + "\" should return \"" + requiredReturnType + "\"."); } }
private boolean checkStaticMethod(Tree reportTree, Symbol.MethodSymbol methodSymbol, Type superClass) { for (Symbol methodWithSameName : superClass.symbol().lookupSymbols(methodSymbol.name())) { if (methodWithSameName.isMethodSymbol() && hideStaticMethod(methodSymbol, superClass, methodWithSameName)) { reportIssue(reportTree, "Rename this method or make it \"static\"."); return true; } } return false; }
private boolean isConstructorOrAfterCreatedMethod(MethodTree tree) { return tree.is(MethodTree.Kind.CONSTRUCTOR) || (implementedInitializableModel && "afterCreated".equals(tree.symbol().name())); } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; Symbol.MethodSymbol methodSymbol = methodTree.symbol(); boolean isMethodInJunit3 = isWithinJunit3TestCase(methodSymbol) && isSetUpOrTearDown(methodSymbol); if (isMethodInJunit3 && requiresSuperCall(methodSymbol) && !callSuperOnOverride(methodTree.block(), methodSymbol)) { reportIssue(methodTree.simpleName(), String.format("Add a \"super.%s()\" call to this method.", methodSymbol.name())); } }
private void checkPrivate(MethodTree methodTree) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (!methodSymbol.isPrivate()) { reportIssue(methodTree.simpleName(), "Make \"" + methodSymbol.name() + "\" \"private\"."); } }