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();
}
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();
}
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;
}
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(";");
}
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;
}
Aggregations