private void checkJunit4AndAboveTestClass(IdentifierTree className, Symbol.TypeSymbol symbol, Stream<Symbol.MethodSymbol> members) { if (symbol.name().endsWith("Test") && !runWithCucumberOrSuiteOrTheoriesRunner(symbol) && members.noneMatch(this::isTestMethod)) { reportClass(className); } }
private void checkJunit4AndAboveTestClass(IdentifierTree className, Symbol.TypeSymbol symbol, Stream<Symbol.MethodSymbol> members) { if (symbol.name().endsWith("Test") && !runWithCucumberOrSuiteOrTheoriesRunner(symbol) && members.noneMatch(this::isTestMethod)) { reportClass(className); } }
@Override public void visitNode(Tree tree) { if (hasSemantic()) { ClassTree classTree = (ClassTree) tree; Symbol.TypeSymbol symbol = classTree.symbol(); String className = symbol.name(); if (endsWithException(className) && !isSubtypeOfException(symbol) && !hasUnknownSuperType(symbol)) { String suffix = className.substring(className.length() - "exception".length()); reportIssue(classTree.simpleName(), "Rename this class to remove \"" + suffix + "\" or correct its inheritance."); } } }
private void checkRedundancy(TypeTree currentInterface, List<Type> superInterfacesTypes, Set<ClassJavaType> superTypes) { Type interfaceType = currentInterface.symbolType(); for (ClassJavaType superType : superTypes) { TypeSymbol superTypeSymbol = superType.symbol(); if (superTypeSymbol.interfaces().contains(interfaceType)) { String typeOfParentMsg = "implemented by a super class"; if (superTypeSymbol.isInterface() && superInterfacesTypes.contains(superType)) { typeOfParentMsg = "already extended by \"" + superTypeSymbol.name() + "\""; } reportIssue(currentInterface, "\"" + interfaceType.name() + "\" is " + typeOfParentMsg + "; there is no need to implement it here."); break; } } }
private static String getTypeName(Type type) { return type.symbol().name(); }
private void checkRedundancy(TypeTree currentInterface, List<Type> superInterfacesTypes, Set<ClassJavaType> superTypes) { Type interfaceType = currentInterface.symbolType(); for (ClassJavaType superType : superTypes) { TypeSymbol superTypeSymbol = superType.symbol(); if (superTypeSymbol.interfaces().contains(interfaceType)) { String typeOfParentMsg = "implemented by a super class"; if (superTypeSymbol.isInterface() && superInterfacesTypes.contains(superType)) { typeOfParentMsg = "already extended by \"" + superTypeSymbol.name() + "\""; } reportIssue(currentInterface, "\"" + interfaceType.name() + "\" is " + typeOfParentMsg + "; there is no need to implement it here."); break; } } }
private void checkSuperType(ClassTree tree, @Nullable Type superType) { if (superType != null && superType.symbol().name().equals(tree.symbol().name())) { String classOrInterface = tree.is(Tree.Kind.CLASS) ? "class" : "interface"; reportIssue(tree.simpleName(), "Rename this " + classOrInterface + "."); } }
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; }
@Override public void visitNode(Tree tree) { if(!hasSemantic()) { return; } ClassTree classTree = (ClassTree) tree; Symbol.TypeSymbol classSymbol = classTree.symbol(); if(classSymbol.isPrivate() && classSymbol.usages().isEmpty()) { reportIssue(classTree.simpleName(), "Remove this unused private \""+ classSymbol.name()+"\" class."); } } }
private void reportIssueOnMethod(@Nullable MethodTree declaration, Symbol.TypeSymbol classSymbol) { if (declaration != null) { String message = "Move this method into "; if (classSymbol.name().isEmpty()) { message += "the anonymous class declared at line " + ((JavaTree) classSymbol.declaration()).getLine()+"."; } else { message += "\"" + classSymbol.name() + "\"."; } reportIssue(declaration.simpleName(), message); } }
@Override public void visitIdentifier(IdentifierTree tree) { Type type = tree.symbolType(); if (type instanceof MethodJavaType) { type = ((MethodJavaType) type).resultType(); } if (!sameErasure(type) && type.isSubtypeOf(classType.erasure())) { reportIssue(tree, String.format("Remove this reference to \"%s\".", type.symbol().name())); } }
private static String getMethodRef(Symbol symbol) { return symbol.enclosingClass().name() + "." + symbol.name(); }
@Override public void visitNode(Tree tree) { if(!hasSemantic()) { return; } ClassTree classTree = (ClassTree) tree; Symbol.TypeSymbol classSymbol = classTree.symbol(); if(classSymbol.isPrivate() && classSymbol.usages().isEmpty()) { reportIssue(classTree.simpleName(), "Remove this unused private \""+ classSymbol.name()+"\" class."); } } }
private void checkSuperType(ClassTree tree, @Nullable Type superType) { if (superType != null && superType.symbol().name().equals(tree.symbol().name())) { String classOrInterface = tree.is(Tree.Kind.CLASS) ? "class" : "interface"; reportIssue(tree.simpleName(), "Rename this " + classOrInterface + "."); } }
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; }
@Override public void visitIdentifier(IdentifierTree tree) { Type type = tree.symbolType(); if (type instanceof MethodJavaType) { type = ((MethodJavaType) type).resultType(); } if (!sameErasure(type) && type.isSubtypeOf(classType.erasure())) { reportIssue(tree, String.format("Remove this reference to \"%s\".", type.symbol().name())); } }
@Override public void visitNode(Tree tree) { if (hasSemantic()) { ClassTree classTree = (ClassTree) tree; Symbol.TypeSymbol symbol = classTree.symbol(); String className = symbol.name(); if (endsWithException(className) && !isSubtypeOfException(symbol) && !hasUnknownSuperType(symbol)) { String suffix = className.substring(className.length() - "exception".length()); reportIssue(classTree.simpleName(), "Rename this class to remove \"" + suffix + "\" or correct its inheritance."); } } }
private void checkSuperTypeDeprecation(@Nullable TypeTree superTypeTree, boolean isInterface) { if (superTypeTree != null) { Type symbolType = superTypeTree.symbolType(); if (symbolType.isClass() && symbolType.symbol().isDeprecated()) { addIssue(superTypeTree, "\"" + symbolType.symbol().name() + "\"" + " is deprecated, " + (isInterface ? "implement" : "extend") + " the suggested replacement instead."); } } }
private void reportIssueOnMethod(@Nullable MethodTree declaration, Symbol.TypeSymbol classSymbol) { if (declaration != null) { String message = "Move this method into "; if (classSymbol.name().isEmpty()) { message += "the anonymous class declared at line " + ((JavaTree) classSymbol.declaration()).getLine()+"."; } else { message += "\"" + classSymbol.name() + "\"."; } reportIssue(declaration.simpleName(), message); } }