Search in sources :

Example 21 with MethodTree

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

the class MethodNameSameAsClassCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    super.visitClass(tree);
    IdentifierTree classSimpleName = tree.simpleName();
    if (classSimpleName == null) {
        return;
    }
    String className = classSimpleName.name();
    for (Tree member : tree.members()) {
        if (member.is(Tree.Kind.METHOD)) {
            IdentifierTree simpleName = ((MethodTree) member).simpleName();
            if (className.equals(simpleName.name())) {
                context.reportIssue(this, simpleName, "Rename this method to prevent any misunderstanding or make it a constructor.");
            }
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 22 with MethodTree

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

the class ExplodedGraphWalker method execute.

private void execute(MethodTree tree) {
    CFG cfg = CFG.build(tree);
    exitBlock = cfg.exitBlock();
    checkerDispatcher.init(tree, cfg);
    liveVariables = LiveVariables.analyze(cfg);
    explodedGraph = new ExplodedGraph();
    methodTree = tree;
    constraintManager = new ConstraintManager();
    workList = new LinkedList<>();
    // Linked hashSet is required to guarantee order of yields to be generated
    endOfExecutionPath = new LinkedHashSet<>();
    if (DEBUG_MODE_ACTIVATED) {
        LOG.debug("Exploring Exploded Graph for method " + tree.simpleName().name() + " at line " + ((JavaTree) tree).getLine());
    }
    programState = ProgramState.EMPTY_STATE;
    steps = 0;
    for (ProgramState startingState : startingStates(tree, programState)) {
        enqueue(new ProgramPoint(cfg.entry()), startingState);
    }
    while (!workList.isEmpty()) {
        steps++;
        if (steps > maxSteps()) {
            throwMaxSteps(tree);
        }
        // LIFO:
        setNode(workList.removeFirst());
        CFG.Block block = (CFG.Block) programPosition.block;
        if (block.successors().isEmpty()) {
            endOfExecutionPath.add(node);
            continue;
        }
        try {
            Tree terminator = block.terminator();
            if (programPosition.i < block.elements().size()) {
                // process block element
                visit(block.elements().get(programPosition.i), terminator);
            } else if (terminator == null) {
                // process block exit, which is unconditional jump such as goto-statement or return-statement
                handleBlockExit(programPosition);
            } else if (programPosition.i == block.elements().size()) {
                // process block exist, which is conditional jump such as if-statement
                checkerDispatcher.executeCheckPostStatement(terminator);
            } else {
                // process branch
                // process block exist, which is conditional jump such as if-statement
                checkerDispatcher.executeCheckPreStatement(terminator);
                handleBlockExit(programPosition);
            }
        } catch (TooManyNestedBooleanStatesException e) {
            throwTooManyBooleanStates(tree, e);
        } catch (RelationalSymbolicValue.TransitiveRelationExceededException e) {
            throwTooManyTransitiveRelationsException(tree, e);
        }
    }
    handleEndOfExecutionPath(false);
    checkerDispatcher.executeCheckEndOfExecution();
    // Cleanup:
    workList = null;
    node = null;
    programState = null;
    constraintManager = null;
}
Also used : ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) CFG(org.sonar.java.cfg.CFG) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) JavaTree(org.sonar.java.model.JavaTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 23 with MethodTree

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

the class SecondPass method complete.

private void complete(JavaSymbol.MethodJavaSymbol symbol) {
    MethodTree methodTree = symbol.declaration;
    Resolve.Env env = semanticModel.getEnv(symbol);
    completeTypeParameters(methodTree.typeParameters(), env);
    ImmutableList.Builder<JavaType> thrownTypes = ImmutableList.builder();
    for (TypeTree throwClause : methodTree.throwsClauses()) {
        JavaType thrownType = resolveType(env, throwClause);
        if (thrownType != null) {
            thrownTypes.add(thrownType);
        }
    }
    JavaType returnType = null;
    List<JavaType> argTypes = Lists.newArrayList();
    // no return type for constructor
    if (!CONSTRUCTOR_NAME.equals(symbol.name)) {
        returnType = resolveType(env, methodTree.returnType());
        if (returnType != null) {
            symbol.returnType = returnType.symbol;
        }
    } else if (!symbol.enclosingClass().isStatic()) {
        JavaSymbol owner = symbol.enclosingClass().owner();
        if (!owner.isPackageSymbol()) {
            // JLS8 - 8.8.1 & 8.8.9 : constructors of inner class have an implicit first arg of its directly enclosing class type
            argTypes.add(owner.enclosingClass().type);
        }
    }
    List<VariableTree> parametersTree = methodTree.parameters();
    List<JavaSymbol> scopeSymbols = symbol.parameters.scopeSymbols();
    for (int i = 0; i < parametersTree.size(); i += 1) {
        VariableTree variableTree = parametersTree.get(i);
        JavaSymbol param = scopeSymbols.get(i);
        if (variableTree.simpleName().name().equals(param.getName())) {
            param.complete();
            argTypes.add(param.getType());
        }
        if (((VariableTreeImpl) variableTree).isVararg()) {
            symbol.flags |= Flags.VARARGS;
        }
    }
    MethodJavaType methodType = new MethodJavaType(argTypes, returnType, thrownTypes.build(), (JavaSymbol.TypeJavaSymbol) symbol.owner);
    symbol.setMethodType(methodType);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ImmutableList(com.google.common.collect.ImmutableList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) TypeTree(org.sonar.plugins.java.api.tree.TypeTree)

Example 24 with MethodTree

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

the class ParameterNullnessCheck method reportIssue.

private void reportIssue(Tree syntaxNode, ExpressionTree argument, JavaSymbol.MethodJavaSymbol methodSymbol) {
    String declarationMessage = "constructor declaration";
    if (!methodSymbol.isConstructor()) {
        declarationMessage = "method '" + methodSymbol.getName() + "' declaration";
    }
    String message = String.format("Annotate the parameter with @javax.annotation.Nullable in %s, or make sure that null can not be passed as argument.", declarationMessage);
    Tree reportTree;
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        reportTree = ExpressionUtils.methodName((MethodInvocationTree) syntaxNode);
    } else {
        reportTree = ((NewClassTree) syntaxNode).identifier();
    }
    Flow.Builder secondaryBuilder = Flow.builder();
    MethodTree declarationTree = methodSymbol.declaration();
    if (declarationTree != null) {
        secondaryBuilder.add(new JavaFileScannerContext.Location(StringUtils.capitalize(declarationMessage) + ".", declarationTree.simpleName()));
    }
    secondaryBuilder.add(new JavaFileScannerContext.Location("Argument can be null.", argument));
    reportIssue(reportTree, message, Collections.singleton(secondaryBuilder.build()));
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) 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) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Flow(org.sonar.java.se.Flow)

Example 25 with MethodTree

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

the class JavaPropertiesHelperTest method firstExpression.

private ExpressionTree firstExpression(String code) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse("class A { " + code + "}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    ClassTree firstType = (ClassTree) compilationUnitTree.types().get(0);
    StatementTree firstStatement = ((MethodTree) firstType.members().get(0)).block().body().get(0);
    return ((ExpressionStatementTree) firstStatement).expression();
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) 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) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

MethodTree (org.sonar.plugins.java.api.tree.MethodTree)143 Test (org.junit.Test)59 Tree (org.sonar.plugins.java.api.tree.Tree)43 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)39 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)34 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)30 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)27 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)23 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)20 Type (org.sonar.plugins.java.api.semantic.Type)19 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)19 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)18 List (java.util.List)16 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)16 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)15 ArrayList (java.util.ArrayList)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)14 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)12