Search in sources :

Example 86 with ClassTree

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

the class MethodIdenticalImplementationsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    List<MethodWithUsedVariables> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD)).map(MethodTree.class::cast).filter(methodTree -> isDuplicateCandidate(methodTree, classTree)).map(MethodWithUsedVariables::new).collect(Collectors.toList());
    if (methods.size() <= 1) {
        return;
    }
    Set<MethodTree> reported = new HashSet<>();
    for (int i = 0; i < methods.size(); i++) {
        MethodWithUsedVariables methodWithVariables = methods.get(i);
        MethodTree method = methodWithVariables.method;
        SyntaxToken methodIdentifier = method.simpleName().identifierToken();
        List<StatementTree> methodBody = method.block().body();
        methods.stream().skip(i + 1L).filter(otherMethodWithVariables -> !reported.contains(otherMethodWithVariables.method)).filter(otherMethodWithVariables -> !methodIdentifier.text().equals(otherMethodWithVariables.method.simpleName().name())).filter(otherMethodWithVariables -> SyntacticEquivalence.areEquivalent(methodBody, otherMethodWithVariables.method.block().body())).filter(methodWithVariables::isUsingSameVariablesWithSameTypes).forEach(otherMethodWithVariables -> {
            MethodTree otherMethod = otherMethodWithVariables.method;
            reportIssue(otherMethod.simpleName(), String.format(ISSUE_MSG, methodIdentifier.text(), methodIdentifier.line()), Collections.singletonList(new JavaFileScannerContext.Location("original implementation", methodIdentifier)), null);
            reported.add(otherMethod);
        });
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) AccessorsUtils(org.sonar.java.ast.visitors.AccessorsUtils) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Set(java.util.Set) HashMap(java.util.HashMap) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) SyntacticEquivalence(org.sonar.java.model.SyntacticEquivalence) Map(java.util.Map) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) HashSet(java.util.HashSet)

Example 87 with ClassTree

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

the class InnerStaticClassesCheck method visitClass.

@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 && couldBeDeclaredStatic(symbol)) {
        Tree reportTree = tree.simpleName();
        if (reportTree == null) {
            // Ignore issues on anonymous classes
            return;
        }
        String message = "Make this a \"static\" inner class.";
        if (symbol.owner().isMethodSymbol()) {
            message = "Make this local class a \"static\" inner class.";
        }
        context.reportIssue(this, reportTree, message);
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 88 with ClassTree

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

the class NestedEnumStaticCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    ModifierKeywordTree staticKeyword = ModifiersUtils.getModifier(classTree.modifiers(), Modifier.STATIC);
    if (staticKeyword != null) {
        List<JavaFileScannerContext.Location> secondary = Collections.singletonList(new JavaFileScannerContext.Location("", classTree.declarationKeyword()));
        reportIssue(staticKeyword, "Remove this redundant \"static\" qualifier.", secondary, null);
    }
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 89 with ClassTree

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

the class PrivateFieldUsedLocallyCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TypeSymbol classSymbol = ((ClassTree) tree).symbol();
    Set<Symbol> fieldsReadOnAnotherInstance = FieldsReadOnAnotherInstanceVisitor.getFrom(tree);
    classSymbol.memberSymbols().stream().filter(PrivateFieldUsedLocallyCheck::isPrivateField).filter(s -> !(s.isFinal() && s.isStatic())).filter(s -> !hasAnnotation(s)).filter(s -> !s.usages().isEmpty()).filter(s -> !fieldsReadOnAnotherInstance.contains(s)).forEach(s -> checkPrivateField(s, classSymbol));
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) HashSet(java.util.HashSet) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) CFG(org.sonar.java.cfg.CFG) ImmutableList(com.google.common.collect.ImmutableList) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ProgramState.isField(org.sonar.java.se.ProgramState.isField) LiveVariables(org.sonar.java.cfg.LiveVariables) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 90 with ClassTree

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

the class CFGTest method build_partial_cfg.

private void build_partial_cfg(String breakOrContinue) {
    String methodCode = "void meth(){ try {fun(); } catch ( Exception e) {e.printStackTrace(); " + breakOrContinue + "; } }";
    CompilationUnitTree cut = (CompilationUnitTree) parser.parse("class A {" + methodCode + "}");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
    List<StatementTree> body = methodTree.block().body();
    CFG cfg = CFG.buildCFG(body, true);
    cfg.setMethodSymbol(methodTree.symbol());
    assertThat(cfg.blocks()).hasSize(5);
    assertThat(cfg.methodSymbol()).isSameAs(methodTree.symbol());
    try {
        CFG.buildCFG(body, false);
        fail("IllegalStateException should have been thrown");
    } catch (IllegalStateException iae) {
        assertThat(iae).hasMessage("'" + breakOrContinue + "' statement not in loop or switch statement");
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

ClassTree (org.sonar.plugins.java.api.tree.ClassTree)116 Tree (org.sonar.plugins.java.api.tree.Tree)53 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)47 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)45 Test (org.junit.Test)41 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)37 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)32 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)23 List (java.util.List)19 Type (org.sonar.plugins.java.api.semantic.Type)18 File (java.io.File)14 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)13 Rule (org.sonar.check.Rule)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)12 Collectors (java.util.stream.Collectors)10 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)10 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)10 ImmutableList (com.google.common.collect.ImmutableList)9 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)9