Search in sources :

Example 6 with TypeTree

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

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

the class JavaTreeModelTest method annotations.

@Test
public void annotations() {
    ClassTree tree = firstType("@SuppressWarnings(\"unchecked\") class T { }");
    List<AnnotationTree> annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    AnnotationTree annotation = annotations.get(0);
    assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments().separators()).isEmpty();
    assertThat(annotation.arguments()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.STRING_LITERAL)).isTrue();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@Target( ) class U {}");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).isEmpty();
    assertThat(annotation.arguments().separators()).isEmpty();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@Target({ElementType.METHOD}) class U {}");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.NEW_ARRAY)).isTrue();
    assertThat(annotation.arguments().separators()).isEmpty();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@SuppressWarnings({\"hello\",}) class U {}");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.NEW_ARRAY)).isTrue();
    NewArrayTree arg = (NewArrayTree) annotation.arguments().get(0);
    assertThat(arg.initializers()).hasSize(1);
    assertThat(arg.initializers().get(0).is(Tree.Kind.STRING_LITERAL)).isTrue();
    assertThat(arg.initializers().separators()).hasSize(1);
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    tree = firstType("@Target(value={ElementType.METHOD}, value2=\"toto\") class T { }");
    annotations = tree.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(annotation.arguments().openParenToken()).isNotNull();
    assertThat(annotation.arguments()).hasSize(2);
    assertThat(annotation.arguments().separators()).hasSize(1);
    assertThat(annotation.arguments().get(0).is(Tree.Kind.ASSIGNMENT)).isTrue();
    assertThat(annotation.arguments().closeParenToken()).isNotNull();
    assertThat(annotation.atToken()).isNotNull();
    assertThatChildrenIteratorHasSize(annotation, 3);
    VariableTree variable = (VariableTree) firstMethodFirstStatement("class T { private void meth() { @NonNullable String str;}}");
    assertThatChildrenIteratorHasSize(variable, 4);
    annotations = variable.modifiers().annotations();
    assertThat(annotations).hasSize(1);
    annotation = annotations.get(0);
    assertThat(annotation.annotationType().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(annotation.atToken()).isNotNull();
    assertThat(annotation.arguments()).isEmpty();
    assertThatChildrenIteratorHasSize(annotation, 3);
    annotations = compilationUnit("@PackageLevelAnnotation package blammy;").packageDeclaration().annotations();
    assertThat(annotations).hasSize(1);
    assertThat(annotations.get(0).atToken()).isNotNull();
    assertThat(annotation.arguments()).isEmpty();
    assertThatChildrenIteratorHasSize(annotation, 3);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { @Foo Integer foo; } }");
    assertThat(variable.modifiers().annotations()).hasSize(1);
    assertThat(variable.type().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(variable.type().annotations()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 4);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { @Foo java.lang.Integer foo; } }");
    assertThat(variable.modifiers().annotations()).hasSize(1);
    assertThat(variable.type().is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(variable.type().annotations()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 4);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { java.lang.@Foo Integer foo; } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThat(variable.type().is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(variable.type().annotations()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 4);
    assertThat(((MemberSelectExpressionTree) variable.type()).identifier().annotations()).hasSize(1);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.B.C foo = a.B.new @Foo C(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    TypeTree type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 2);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.b.C<Integer> foo = a.B.new @Foo C<Integer>(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 3);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.B.C foo = new @Foo a.B.C(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 4);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { a.b.C<Integer> foo = new @Foo a.b.C<Integer>(); } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewClassTree) variable.initializer()).identifier();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 3);
    variable = (VariableTree) firstMethodFirstStatement("class T { private void m() { int[] foo = new @Foo int[42]; } }");
    assertThat(variable.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(variable, 6);
    type = ((NewArrayTree) variable.initializer()).type();
    assertThat(type.is(Tree.Kind.PRIMITIVE_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 2);
    variable = ((TryStatementTree) firstMethodFirstStatement("class T { private void m() { try{ } catch (@Foo E1 | E2 e) {}; } }")).catches().get(0).parameter();
    assertThat(variable.modifiers()).hasSize(1);
    assertThatChildrenIteratorHasSize(variable, 3);
    type = variable.type();
    assertThat(type.is(Tree.Kind.UNION_TYPE)).isTrue();
    assertThat(type.annotations()).isEmpty();
    assertThat(((UnionTypeTree) type).typeAlternatives().separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 1);
    ClassTree classTree = firstType("class T extends @Foo a.b.C {}");
    assertThat(classTree.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(classTree, 9);
    type = classTree.superClass();
    assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 4);
    classTree = firstType("class T extends @Foo a.b.C<Integer> {}");
    assertThat(classTree.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(classTree, 9);
    type = classTree.superClass();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 3);
    classTree = (ClassTree) firstMethodFirstStatement("class MyClass<A, B, C> { void foo() { class MyOtherClass extends @Foo MyClass<A, B, C>.MyInnerClass {} } public class MyInnerClass {}}");
    assertThat(classTree.modifiers()).isEmpty();
    assertThatChildrenIteratorHasSize(classTree, 9);
    type = classTree.superClass();
    assertThat(type.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    type = (ParameterizedTypeTree) ((MemberSelectExpressionTree) type).expression();
    assertThat(type.is(Tree.Kind.PARAMETERIZED_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(0);
    assertThatChildrenIteratorHasSize(type, 2);
    TypeCastTree typeCast = (TypeCastTree) ((ReturnStatementTree) firstMethodFirstStatement("class T { private long m(int a) { return (@Foo long) a; } }")).expression();
    assertThat(typeCast.type()).isNotNull();
    assertThatChildrenIteratorHasSize(typeCast, 5);
    type = typeCast.type();
    assertThat(type.is(Tree.Kind.PRIMITIVE_TYPE)).isTrue();
    assertThat(type.annotations()).hasSize(1);
    assertThatChildrenIteratorHasSize(type, 2);
}
Also used : TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Test(org.junit.Test)

Example 8 with TypeTree

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

the class ArrayDesignatorAfterTypeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    TypeTree returnType = methodTree.returnType();
    SyntaxToken identifierToken = methodTree.simpleName().identifierToken();
    while (returnType.is(Tree.Kind.ARRAY_TYPE)) {
        ArrayTypeTree arrayTypeTree = (ArrayTypeTree) returnType;
        SyntaxToken openBracketToken = arrayTypeTree.openBracketToken();
        if (isInvalidPosition(openBracketToken, identifierToken)) {
            reportIssue(openBracketToken, "Move the array designators \"[]\" to the end of the return type.");
            break;
        }
        returnType = arrayTypeTree.type();
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Example 9 with TypeTree

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

the class LeftCurlyBraceBaseTreeVisitor method getLastTokenFromSignature.

@CheckForNull
private static SyntaxToken getLastTokenFromSignature(ClassTree classTree) {
    List<TypeTree> superInterfaces = classTree.superInterfaces();
    if (!superInterfaces.isEmpty()) {
        return getIdentifierToken(Iterables.getLast(superInterfaces));
    }
    TypeTree superClass = classTree.superClass();
    if (superClass != null) {
        return getIdentifierToken(superClass);
    }
    TypeParameters typeParameters = classTree.typeParameters();
    if (!typeParameters.isEmpty()) {
        return typeParameters.closeBracketToken();
    }
    IdentifierTree simpleName = classTree.simpleName();
    if (simpleName != null) {
        return simpleName.identifierToken();
    }
    // enum constants and new class trees are handled separately
    return null;
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull)

Example 10 with TypeTree

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

the class RawExceptionCheck method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    super.visitMethod(tree);
    if ((tree.is(Tree.Kind.CONSTRUCTOR) || isNotOverridden(tree)) && isNotMainMethod(tree)) {
        for (TypeTree throwClause : tree.throwsClauses()) {
            Type exceptionType = throwClause.symbolType();
            if (isRawException(exceptionType) && !exceptionsThrownByMethodInvocations.contains(exceptionType)) {
                reportIssue(throwClause);
            }
        }
    }
    exceptionsThrownByMethodInvocations.clear();
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Aggregations

TypeTree (org.sonar.plugins.java.api.tree.TypeTree)31 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)11 Type (org.sonar.plugins.java.api.semantic.Type)8 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)6 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)6 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 PrimitiveTypeTree (org.sonar.plugins.java.api.tree.PrimitiveTypeTree)4 UnionTypeTree (org.sonar.plugins.java.api.tree.UnionTypeTree)4 Test (org.junit.Test)3 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HashSet (java.util.HashSet)2 CheckForNull (javax.annotation.CheckForNull)2