private static List<Symbol> retrieveMembers(Symbol.TypeSymbol classSymbol) { List<Symbol> results = Lists.newLinkedList(); results.addAll(extractMembers(classSymbol, false)); for (Type parentInterface : classSymbol.interfaces()) { results.addAll(extractMembers(parentInterface.symbol(), true)); } Type superClass = classSymbol.superClass(); if (superClass != null) { results.addAll(extractMembers(superClass.symbol(), true)); } return results; }
@Override public void visitNode(Tree tree) { TypeTree identifier = ((NewClassTree) tree).identifier(); Symbol.TypeSymbol newClassTypeSymbol = identifier.symbolType().symbol(); if (!newClassTypeSymbol.isEnum() && hasOnlyStaticMethods(newClassTypeSymbol) && !instantiateOwnClass(identifier, newClassTypeSymbol)) { String message = "Remove this instantiation."; String name = getNewClassName(identifier); if (name != null) { message = "Remove this instantiation of \"{0}\"."; } addIssue(tree, MessageFormat.format(message, name)); } }
private static boolean superClassHasOnlyStaticMethods(Symbol.TypeSymbol newClassTypeSymbol) { Type superClass = newClassTypeSymbol.superClass(); if (superClass != null && !superClass.is("java.lang.Object")) { return hasOnlyStaticMethods(superClass.symbol()); } return true; }
private static List<Symbol> extractMembers(Symbol.TypeSymbol classSymbol, boolean ignorePrivate) { List<Symbol> results = Lists.newLinkedList(); for (Symbol symbol : classSymbol.memberSymbols()) { if ((isVariableToExtract(symbol) || isMethodToExtract(symbol)) && !(symbol.isPrivate() && ignorePrivate)) { results.add(symbol); } } return results; }
@Override public void visitClass(ClassTree tree) { Symbol.TypeSymbol symbol = tree.symbol(); if (!tree.is(Tree.Kind.CLASS)) { return; } outerClasses.push(symbol); atLeastOneReference.push(Boolean.FALSE); scan(tree.members()); Boolean oneReference = atLeastOneReference.pop(); outerClasses.pop(); if (!symbol.isStatic() && !oneReference && !outerClasses.isEmpty() && isFirstParentStatic(outerClasses)) { String named = symbol.name().isEmpty() ? "named " : ""; context.addIssue(tree, this, "Make this a " + named + "\"static\" inner class."); } }
private static boolean hasOnlyStaticMethods(Symbol.TypeSymbol newClassTypeSymbol) { Collection<MethodSymbol> methods = filterMethods(newClassTypeSymbol.memberSymbols()); if (methods.isEmpty()) { return false; } for (MethodSymbol method : methods) { if (!method.isStatic()) { return false; } } return superClassHasOnlyStaticMethods(newClassTypeSymbol); }
private boolean instantiateOwnClass(Tree identifier, Symbol.TypeSymbol newClassTypeSymbol) { Type enclosingClassType = getSemanticModel().getEnclosingClass(identifier).type(); return enclosingClassType.equals(newClassTypeSymbol.type()); }