Search in sources :

Example 11 with IdentifierTree

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

the class CFG method buildContinueStatement.

private void buildContinueStatement(ContinueStatementTree tree) {
    IdentifierTree label = tree.label();
    Block targetBlock = null;
    if (label == null) {
        if (continueTargets.isEmpty()) {
            if (!ignoreBreakAndContinue) {
                throw new IllegalStateException("'continue' statement not in loop or switch statement");
            }
        } else {
            targetBlock = continueTargets.getLast();
        }
    } else {
        targetBlock = labelsContinueTarget.get(label.name());
    }
    currentBlock = createUnconditionalJump(tree, targetBlock);
    // cleanup for continue statement to a finally: continue block can't have an exit block.
    currentBlock.exitBlock = null;
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 12 with IdentifierTree

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

the class PackageUtils method packageName.

public static String packageName(@Nullable PackageDeclarationTree packageDeclarationTree, String separator) {
    if (packageDeclarationTree == null) {
        return "";
    }
    Deque<String> pieces = new LinkedList<>();
    ExpressionTree expr = packageDeclarationTree.packageName();
    while (expr.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
        pieces.push(mse.identifier().name());
        pieces.push(separator);
        expr = mse.expression();
    }
    if (expr.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree idt = (IdentifierTree) expr;
        pieces.push(idt.name());
    }
    StringBuilder sb = new StringBuilder();
    for (String piece : pieces) {
        sb.append(piece);
    }
    return sb.toString();
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LinkedList(java.util.LinkedList)

Example 13 with IdentifierTree

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

the class NewClassTreeImpl method getConstructorIdentifier.

private static IdentifierTree getConstructorIdentifier(Tree constructorSelect) {
    IdentifierTree constructorIdentifier;
    if (constructorSelect.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mset = (MemberSelectExpressionTree) constructorSelect;
        constructorIdentifier = mset.identifier();
    } else if (constructorSelect.is(Tree.Kind.IDENTIFIER)) {
        constructorIdentifier = (IdentifierTree) constructorSelect;
    } else if (constructorSelect.is(Tree.Kind.PARAMETERIZED_TYPE)) {
        constructorIdentifier = getConstructorIdentifier(((ParameterizedTypeTree) constructorSelect).type());
    } else {
        throw new IllegalStateException("Constructor select is not of the expected type " + constructorSelect);
    }
    return constructorIdentifier;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 14 with IdentifierTree

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

the class ProvidesDirectiveTreeImplTest method provides_with_modules.

@Test
public void provides_with_modules() {
    ProvidesDirectiveTree exports = providesDirective("provides org.MyInterface with com.MyInterface, MyInterface2;");
    assertThat(exports.kind()).isEqualTo(Tree.Kind.PROVIDES_DIRECTIVE);
    assertThat(exports.directiveKeyword().text()).isEqualTo("provides");
    TypeTree typeName = exports.typeName();
    assertThat(typeName.is(Tree.Kind.MEMBER_SELECT)).isTrue();
    MemberSelectExpressionTree mset = (MemberSelectExpressionTree) typeName;
    assertThat(((IdentifierTree) mset.expression()).name()).isEqualTo("org");
    assertThat(mset.identifier().name()).isEqualTo("MyInterface");
    assertThat(exports.withKeyword().text()).isEqualTo("with");
    ListTree<TypeTree> typeNames = exports.typeNames();
    assertThat(typeNames).hasSize(2);
    assertThat(((MemberSelectExpressionTree) typeNames.get(0)).identifier().name()).isEqualTo("MyInterface");
    assertThat(typeNames.separators()).hasSize(1);
    assertThat(typeNames.separators().iterator().next().text()).isEqualTo(",");
    assertThat(((IdentifierTree) typeNames.get(1)).name()).isEqualTo("MyInterface2");
    assertThat(exports.semicolonToken().text()).isEqualTo(";");
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ProvidesDirectiveTree(org.sonar.plugins.java.api.tree.ProvidesDirectiveTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 15 with IdentifierTree

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

the class SymbolTableTest method ConstructorWithInference.

@Test
public void ConstructorWithInference() throws Exception {
    Result result = Result.createFor("ConstructorWithInference");
    JavaSymbol.TypeJavaSymbol classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("A");
    List<JavaSymbol> constructors = classSymbol.members.lookup("<init>");
    JavaSymbol noParamConstructor = constructors.get(0);
    JavaSymbol parametrizedConstructor = constructors.get(1);
    JavaSymbol wildcardConstructor = constructors.get(2);
    Type aObjectType = result.symbol("aObject").type();
    Type aStringType = result.symbol("aString").type();
    IdentifierTree constStringNoArg = result.referenceTree(14, 9);
    assertThat(constStringNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constStringNoArg)).isSameAs(aStringType);
    IdentifierTree constDiamondNoArg = result.referenceTree(16, 9);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    constDiamondNoArg = result.referenceTree(17, 9);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    assertThat(result.reference(17, 15)).isSameAs(result.symbol("foo", 10));
    constDiamondNoArg = result.referenceTree(18, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    constDiamondNoArg = result.referenceTree(20, 25);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aStringType);
    constDiamondNoArg = result.referenceTree(21, 18);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    constDiamondNoArg = result.referenceTree(23, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aStringType);
    assertThat(result.reference(23, 5)).isSameAs(result.symbol("bar", 11));
    IdentifierTree constDiamondObject = result.referenceTree(25, 9);
    assertThat(constDiamondObject.symbol()).isSameAs(parametrizedConstructor);
    assertThat(getNewClassTreeType(constDiamondObject)).isSameAs(aObjectType);
    IdentifierTree constDiamondString = result.referenceTree(26, 9);
    assertThat(constDiamondString.symbol()).isSameAs(parametrizedConstructor);
    assertThat(getNewClassTreeType(constDiamondString)).isSameAs(aStringType);
    IdentifierTree constWildcardDiamond = result.referenceTree(28, 9);
    assertThat(constWildcardDiamond.symbol()).isSameAs(wildcardConstructor);
    assertThat(getNewClassTreeType(constWildcardDiamond)).isSameAs(aStringType);
    IdentifierTree returnConstDiamondNoArg = result.referenceTree(30, 16);
    assertThat(returnConstDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(returnConstDiamondNoArg)).isSameAs(aStringType);
    // inference allowing to deduce only partially the type parameters
    classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("B");
    noParamConstructor = classSymbol.members.lookup("<init>").get(0);
    ParametrizedTypeJavaType type;
    constDiamondNoArg = result.referenceTree(42, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    type = (ParametrizedTypeJavaType) getNewClassTreeType(constDiamondNoArg);
    assertThat(type.erasure()).isSameAs(classSymbol.type().erasure());
    assertThat(type.substitution(type.typeParameters().get(0)).is("java.lang.String")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(1)).is("java.lang.Object")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(2)).is("java.lang.Object")).isTrue();
    assertThat(result.reference(42, 5)).isSameAs(result.symbol("qix", 46));
    constDiamondNoArg = result.referenceTree(43, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    type = (ParametrizedTypeJavaType) getNewClassTreeType(constDiamondNoArg);
    assertThat(type.erasure()).isSameAs(classSymbol.type().erasure());
    assertThat(type.substitution(type.typeParameters().get(0)).is("java.lang.Object")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(1)).is("java.lang.String")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(2)).is("java.lang.Integer")).isTrue();
    assertThat(result.reference(43, 5)).isSameAs(result.symbol("bar", 47));
    // erasure of bounded type parameters
    classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("D");
    noParamConstructor = classSymbol.members.lookup("<init>").get(0);
    constDiamondNoArg = result.referenceTree(60, 9);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(result.symbol("dC").type());
    assertThat(result.reference(60, 15)).isSameAs(result.symbol("foo", 55));
    // enclosing expression
    classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("F");
    constructors = classSymbol.members.lookup("<init>");
    noParamConstructor = constructors.get(0);
    parametrizedConstructor = constructors.get(1);
    IdentifierTree constructorCall = result.referenceTree(75, 15);
    assertThat(constructorCall.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constructorCall)).isSameAs(result.symbol("fString").type());
    assertThat(result.reference(75, 5)).isSameAs(result.symbol("foo", 72));
    constructorCall = result.referenceTree(76, 15);
    assertThat(constructorCall.symbol()).isSameAs(parametrizedConstructor);
    assertThat(getNewClassTreeType(constructorCall)).isSameAs(result.symbol("fString").type());
    assertThat(result.reference(76, 5)).isSameAs(result.symbol("foo", 72));
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7