Search in sources :

Example 6 with MemberSelectExpressionTree

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

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

the class MethodInvocationTreeImpl method firstToken.

@Override
public SyntaxToken firstToken() {
    if (typeArguments() != null && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        ExpressionTree expression = ((MemberSelectExpressionTree) methodSelect).expression();
        SyntaxToken firstToken = expression.firstToken();
        if (firstToken != null) {
            return firstToken;
        }
    }
    return super.firstToken();
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 8 with MemberSelectExpressionTree

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

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

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

the class TypeAndReferenceSolver method resolveAs.

public JavaSymbol resolveAs(Tree tree, int kind, Resolve.Env resolveEnv, boolean associateReference) {
    if (tree.is(Tree.Kind.IDENTIFIER, Tree.Kind.MEMBER_SELECT)) {
        JavaSymbol resolvedSymbol;
        IdentifierTree identifierTree;
        if (tree.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) tree;
            if (JavaKeyword.CLASS.getValue().equals(mse.identifier().name())) {
                // resolve type of expression xxx.class
                return resolveClassType(tree, resolveEnv, mse);
            }
            identifierTree = mse.identifier();
            List<AnnotationTree> identifierAnnotations = identifierTree.annotations();
            scan(identifierAnnotations);
            completeMetadata((JavaSymbol) identifierTree.symbol(), identifierAnnotations);
            Resolve.Resolution res = getSymbolOfMemberSelectExpression(mse, kind, resolveEnv);
            resolvedSymbol = res.symbol();
            JavaType resolvedType = resolve.resolveTypeSubstitution(res.type(), getType(mse.expression()));
            registerType(identifierTree, resolvedType);
            registerType(tree, resolvedType);
        } else {
            identifierTree = (IdentifierTree) tree;
            Resolve.Resolution resolution = resolve.findIdent(resolveEnv, identifierTree.name(), kind);
            resolvedSymbol = resolution.symbol();
            registerType(tree, resolution.type());
        }
        if (associateReference) {
            associateReference(identifierTree, resolvedSymbol);
        }
        return resolvedSymbol;
    }
    tree.accept(this);
    JavaType type = getType(tree);
    if (tree.is(Tree.Kind.INFERED_TYPE)) {
        type = symbols.deferedType((AbstractTypedTree) tree);
        registerType(tree, type);
    }
    if (type == null) {
        throw new IllegalStateException("Type not resolved " + tree);
    }
    return type.symbol;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Aggregations

MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)60 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)36 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)36 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 Symbol (org.sonar.plugins.java.api.semantic.Symbol)13 Test (org.junit.Test)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)8 Tree (org.sonar.plugins.java.api.tree.Tree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)5 CheckForNull (javax.annotation.CheckForNull)4 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)4 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)4 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 Resolution (org.sonar.java.resolve.Resolve.Resolution)3