Search in sources :

Example 16 with NewClassTree

use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.

the class TooManyMethodsCheck method visitNode.

@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);
}
Also used : RuleProperty(org.sonar.check.RuleProperty) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ExpressionsHelper(org.sonar.java.checks.helpers.ExpressionsHelper) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 17 with NewClassTree

use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.

the class ThreadAsRunnableArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    List<ExpressionTree> arguments;
    Symbol methodSymbol;
    if (tree.is(Kind.NEW_CLASS)) {
        NewClassTree nct = (NewClassTree) tree;
        methodSymbol = nct.constructorSymbol();
        arguments = nct.arguments();
    } else {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        methodSymbol = mit.symbol();
        arguments = mit.arguments();
    }
    if (!arguments.isEmpty() && methodSymbol.isMethodSymbol()) {
        checkArgumentsTypes(arguments, (MethodJavaSymbol) methodSymbol);
    }
}
Also used : MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 18 with NewClassTree

use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.

the class ThreadLocalWithInitialCheck method onConstructorFound.

@Override
protected void onConstructorFound(NewClassTree newClassTree) {
    ClassTree classTree = newClassTree.classBody();
    if (classTree == null) {
        return;
    }
    List<Tree> members = classTree.members();
    if (members.size() != 1) {
        return;
    }
    members.stream().filter(tree -> tree.is(Tree.Kind.METHOD)).map(t -> ((MethodTree) t)).filter(t -> "initialValue".equals(t.simpleName().name())).filter(t -> t.parameters().isEmpty()).findFirst().ifPresent(t -> reportIssue(newClassTree.identifier(), "Replace this anonymous class with a call to \"ThreadLocal.withInitial\"." + context.getJavaVersion().java8CompatibilityMessage()));
}
Also used : JavaVersionAwareVisitor(org.sonar.java.JavaVersionAwareVisitor) JavaVersion(org.sonar.plugins.java.api.JavaVersion) List(java.util.List) AbstractMethodDetection(org.sonar.java.checks.methods.AbstractMethodDetection) MethodMatcher(org.sonar.java.matcher.MethodMatcher) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Collections(java.util.Collections) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 19 with NewClassTree

use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.

the class ThreadOverridesRunCheck method visitNode.

@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.");
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 20 with NewClassTree

use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.

the class ClassWithoutHashCodeInHashStructureCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    Type type = ((NewClassTree) tree).symbolType();
    if (type instanceof ParametrizedTypeJavaType && useHashDataStructure(type)) {
        ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) type;
        Symbol.TypeSymbol symbol = ptt.substitution(ptt.typeParameters().get(0)).symbol();
        if (implementsEquals(symbol) && !implementsHashCode(symbol)) {
            reportIssue(tree, "Add a \"hashCode()\" method to \"" + symbol.name() + "\" or remove it from this hash.");
        }
    }
}
Also used : ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Aggregations

NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)33 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)14 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 Tree (org.sonar.plugins.java.api.tree.Tree)11 Symbol (org.sonar.plugins.java.api.semantic.Symbol)10 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)10 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)8 Test (org.junit.Test)7 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)6 Type (org.sonar.plugins.java.api.semantic.Type)5 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)4 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)4 List (java.util.List)3