Search in sources :

Example 26 with NewClassTree

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

the class LeftCurlyBraceBaseTreeVisitor method visitNewClass.

@Override
public void visitNewClass(NewClassTree tree) {
    ClassTree classBody = tree.classBody();
    if (classBody != null && tree.arguments().closeParenToken() != null) {
        checkTokens(tree.arguments().closeParenToken(), classBody.openBraceToken());
    }
    super.visitNewClass(tree);
}
Also used : NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 27 with NewClassTree

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

the class LeftCurlyBraceBaseTreeVisitor method visitEnumConstant.

@Override
public void visitEnumConstant(EnumConstantTree tree) {
    NewClassTree initializer = tree.initializer();
    ClassTree classBody = initializer.classBody();
    if (classBody != null) {
        SyntaxToken openBraceToken = classBody.openBraceToken();
        if (initializer.arguments().closeParenToken() != null) {
            checkTokens(initializer.arguments().closeParenToken(), openBraceToken);
        } else {
            checkTokens(tree.simpleName().identifierToken(), openBraceToken);
        }
    }
    super.visitEnumConstant(tree);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 28 with NewClassTree

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

the class LiveVariables method processBlockElements.

private void processBlockElements(CFG.Block block, Set<Symbol> blockKill, Set<Symbol> blockGen) {
    // process elements from bottom to top
    Set<Tree> assignmentLHS = new HashSet<>();
    for (Tree element : Lists.reverse(block.elements())) {
        switch(element.kind()) {
            case ASSIGNMENT:
                processAssignment((AssignmentExpressionTree) element, blockKill, blockGen, assignmentLHS);
                break;
            case IDENTIFIER:
                processIdentifier((IdentifierTree) element, blockGen, assignmentLHS);
                break;
            case MEMBER_SELECT:
                processMemberSelect((MemberSelectExpressionTree) element, assignmentLHS, blockGen);
                break;
            case VARIABLE:
                blockKill.add(((VariableTree) element).symbol());
                blockGen.remove(((VariableTree) element).symbol());
                break;
            case LAMBDA_EXPRESSION:
                blockGen.addAll(getUsedVariables(((LambdaExpressionTree) element).body(), cfg.methodSymbol()));
                break;
            case METHOD_REFERENCE:
                blockGen.addAll(getUsedVariables(((MethodReferenceTree) element).expression(), cfg.methodSymbol()));
                break;
            case NEW_CLASS:
                blockGen.addAll(getUsedVariables(((NewClassTree) element).classBody(), cfg.methodSymbol()));
                break;
            default:
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) HashSet(java.util.HashSet)

Example 29 with NewClassTree

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

the class SymbolTableTest method anonymousClassWithDiamondOperator.

/**
 * JLS9 - ยง15.9.3
 * @since Java 9
 */
@Test
public void anonymousClassWithDiamondOperator() {
    Result result = Result.createForJavaFile("src/test/files/resolve/Java9DiamondOperatorAnonymousClass");
    NewClassTree nct;
    ClassJavaType symbolType;
    ClassJavaType superType;
    ParametrizedTypeJavaType ptjt;
    // ArrayList<>(), not anonymous but with diamond
    nct = parentNewClassTree(result.referenceTree(7, 16));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.is("java.util.ArrayList")).isTrue();
    assertThat(symbolType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) symbolType;
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
    // B<Integer>(), no diamond
    nct = parentNewClassTree(result.referenceTree(11, 16));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.isSubtypeOf("B")).isTrue();
    superType = symbolType.getSuperType();
    assertThat(superType.is("B")).isTrue();
    assertThat(superType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) superType;
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
    // B<>(Integer), diamond with inference from B<U> to B<Integer>
    nct = parentNewClassTree(result.referenceTree(15, 16));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.isSubtypeOf("B")).isTrue();
    superType = symbolType.getSuperType();
    assertThat(superType.is("B")).isTrue();
    assertThat(superType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) superType;
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
    // B<>(T), diamond with inference from B<U> to B<T>
    nct = parentNewClassTree(result.referenceTree(19, 16));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.isSubtypeOf("B")).isTrue();
    superType = symbolType.getSuperType();
    assertThat(superType.is("B")).isTrue();
    assertThat(superType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) superType;
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).symbol().name()).isEqualTo("T");
    // C<>(Integer, String[]), diamond with inference from C<U,V> to C<Integer, String>
    nct = parentNewClassTree(result.referenceTree(23, 16));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.isSubtypeOf("C")).isTrue();
    superType = symbolType.getSuperType();
    assertThat(superType.is("C")).isTrue();
    assertThat(superType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) superType;
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(1).is("java.lang.String")).isTrue();
    // D<>(), diamond with inference on interface from D<X> to D<Integer>
    nct = parentNewClassTree(result.referenceTree(27, 16));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.isSubtypeOf("D")).isTrue();
    superType = (ClassJavaType) symbolType.interfaces.get(0);
    assertThat(superType.is("D")).isTrue();
    assertThat(superType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) superType;
    // FIXME SONARJAVA-1706 : requires inference from target type
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).symbol().name()).isEqualTo("X");
    // should be true
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isFalse();
    // B<>(), diamond with inference on class from B<U> to B<Integer>
    nct = parentNewClassTree(result.referenceTree(34, 13));
    symbolType = (ClassJavaType) nct.symbolType();
    assertThat(symbolType.isSubtypeOf("B")).isTrue();
    superType = symbolType.getSuperType();
    assertThat(superType.is("B")).isTrue();
    assertThat(superType.isParameterized()).isTrue();
    ptjt = (ParametrizedTypeJavaType) superType;
    // FIXME SONARJAVA-1706 : requires inference from target type
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).symbol().name()).isEqualTo("U");
    // should be true
    assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isFalse();
}
Also used : NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Test(org.junit.Test)

Example 30 with NewClassTree

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

the class TypeAndReferenceSolverTest method parametrized_constructor.

@Test
public void parametrized_constructor() {
    CompilationUnitTree compilationUnit = treeOf("class A { <T> A(T t) {} void test() { new A(\"hello\"); } }");
    MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
    NewClassTree newClassTree = (NewClassTree) ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
    Type identifierType = newClassTree.identifier().symbolType();
    assertThat(identifierType).isInstanceOf(MethodJavaType.class);
    assertThat(((MethodJavaType) identifierType).argTypes.get(0).is("java.lang.String")).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Test(org.junit.Test)

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