/** * Return the correct tree to report on for class trees. * @param classTree class tree raising an issue. * @return simple name of class tree or identifier in parent expression for anonymous class. */ public static Tree reportOnClassTree(ClassTree classTree) { Tree reportTree = classTree.simpleName(); if(reportTree == null) { reportTree = ((NewClassTree) classTree.parent()).identifier(); } return reportTree; }
/** * Return the correct tree to report on for class trees. * @param classTree class tree raising an issue. * @return simple name of class tree or identifier in parent expression for anonymous class. */ public static Tree reportOnClassTree(ClassTree classTree) { Tree reportTree = classTree.simpleName(); if(reportTree == null) { reportTree = ((NewClassTree) classTree.parent()).identifier(); } return reportTree; }
private static boolean isBodyOfEnumConstantTree(ClassTree tree) { Tree parent = tree.parent(); return parent.is(Tree.Kind.NEW_CLASS) && parent.parent().is(Tree.Kind.ENUM_CONSTANT); }
private static boolean isBodyOfEnumConstantTree(ClassTree tree) { Tree parent = tree.parent(); return parent.is(Tree.Kind.NEW_CLASS) && parent.parent().is(Tree.Kind.ENUM_CONSTANT); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } ClassTree classTree = (ClassTree) tree; List<Tree> methods = classTree.members().stream() .filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())) .collect(Collectors.toList()); if(shouldNotReportIssue(classTree, methods)) { return; } List<JavaFileScannerContext.Location> secondary = methods.stream() .map(element -> new JavaFileScannerContext.Location("Method + 1", element)) .collect(Collectors.toList()); String classDescription; if (classTree.simpleName() == null) { classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\""; } else { classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\""; } reportIssue( ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } ClassTree classTree = (ClassTree) tree; List<Tree> methods = classTree.members().stream() .filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())) .collect(Collectors.toList()); if(shouldNotReportIssue(classTree, methods)) { return; } List<JavaFileScannerContext.Location> secondary = methods.stream() .map(element -> new JavaFileScannerContext.Location("Method + 1", element)) .collect(Collectors.toList()); String classDescription; if (classTree.simpleName() == null) { classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\""; } else { classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\""; } reportIssue( ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null); }
@Override public void visitClass(ClassTree tree) { if (!isBodyOfEnumConstantTree(tree)) { Type superClass = tree.symbol().superClass(); int dit = 0; while (superClass != null) { String fullyQualifiedName = superClass.fullyQualifiedName(); if (getPatterns().stream().anyMatch(wp -> wp.match(fullyQualifiedName))) { break; } dit++; superClass = superClass.symbol().superClass(); } if (dit > max) { Tree reportTree = tree.simpleName(); if (tree.parent().is(Tree.Kind.NEW_CLASS)) { reportTree = ((NewClassTree) tree.parent()).newKeyword(); } context.reportIssue(this, reportTree, "This class has " + dit + " parents which is greater than " + max + " authorized.", new ArrayList<>(), dit - max); } } super.visitClass(tree); }
@Override public void visitClass(ClassTree tree) { if (!isBodyOfEnumConstantTree(tree)) { Type superClass = tree.symbol().superClass(); int dit = 0; while (superClass != null) { String fullyQualifiedName = superClass.fullyQualifiedName(); if (getPatterns().stream().anyMatch(wp -> wp.match(fullyQualifiedName))) { break; } dit++; superClass = superClass.symbol().superClass(); } if (dit > max) { Tree reportTree = tree.simpleName(); if (tree.parent().is(Tree.Kind.NEW_CLASS)) { reportTree = ((NewClassTree) tree.parent()).newKeyword(); } context.reportIssue(this, reportTree, "This class has " + dit + " parents which is greater than " + max + " authorized.", new ArrayList<>(), dit - max); } } super.visitClass(tree); }
@Override public void visitNode(Tree tree) { ClassTree classTree = (ClassTree) tree; Symbol.TypeSymbol classSymbol = classTree.symbol(); if (classSymbol != null && isDirectSubtypeOfThread(classSymbol) && !overridesRunMethod(classSymbol) && !hasConstructorCallingSuperWithRunnable(classTree)) { Tree report = classTree.simpleName(); Tree parent = classTree.parent(); if(parent.is(Tree.Kind.NEW_CLASS)) { NewClassTree newClassTree = (NewClassTree) parent; if (hasRunnableArgument(newClassTree.arguments())) { // will call the super constructor setting a runnable which will be executed by the run() method return; } report = newClassTree.identifier(); } reportIssue(report, "Don't extend \"Thread\", since the \"run\" method is not overridden."); } }
@Override public void visitNode(Tree tree) { ClassTree classTree = (ClassTree) tree; Symbol.TypeSymbol classSymbol = classTree.symbol(); if (classSymbol != null && isDirectSubtypeOfThread(classSymbol) && !overridesRunMethod(classSymbol) && !hasConstructorCallingSuperWithRunnable(classTree)) { Tree report = classTree.simpleName(); Tree parent = classTree.parent(); if(parent.is(Tree.Kind.NEW_CLASS)) { NewClassTree newClassTree = (NewClassTree) parent; if (hasRunnableArgument(newClassTree.arguments())) { // will call the super constructor setting a runnable which will be executed by the run() method return; } report = newClassTree.identifier(); } reportIssue(report, "Don't extend \"Thread\", since the \"run\" method is not overridden."); } }