Search in sources :

Example 16 with VariableTree

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

the class BadLocalVariableNameCheck method visitCatch.

@Override
public void visitCatch(CatchTree tree) {
    VariableTree parameter = tree.parameter();
    if (parameter.simpleName().name().length() > 1) {
        scan(parameter);
    }
    scan(tree.block());
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 17 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree 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 18 with VariableTree

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

the class SecondPass method complete.

private void complete(JavaSymbol.VariableJavaSymbol symbol) {
    VariableTree variableTree = symbol.declaration;
    Resolve.Env env = semanticModel.getEnv(symbol);
    if (variableTree.is(Tree.Kind.ENUM_CONSTANT)) {
        symbol.type = env.enclosingClass.type;
    } else {
        symbol.type = resolveType(env, variableTree.type());
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 19 with VariableTree

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

the class ReassignmentFinderTest method outside_method.

@Test
public void outside_method() throws Exception {
    String code = newCode("int b;", "int foo() {", "  return b;", "}");
    ClassTree classTree = classTree(code);
    List<StatementTree> statements = ((MethodTree) classTree.members().get(1)).block().body();
    ExpressionTree variableDeclaration = ((VariableTree) (classTree.members().get(0))).initializer();
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, variableDeclaration);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Test(org.junit.Test)

Example 20 with VariableTree

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

the class DefaultJavaFileScannerContextTest method report_issue_between_two_trees.

@Test
public void report_issue_between_two_trees() {
    ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
    VariableTree firstMember = (VariableTree) tree.members().get(0);
    VariableTree secondMember = (VariableTree) tree.members().get(1);
    context.reportIssue(CHECK, firstMember.simpleName(), secondMember.equalToken(), "msg");
    assertThat(reportedMessage.getMessage()).isEqualTo("msg");
    assertThat(reportedMessage.getCost()).isNull();
    assertThat(reportedMessage.flows).isEmpty();
    assertMessagePosition(reportedMessage, 2, 6, 3, 10);
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9