use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class ExpressionUtils method methodName.
/**
* Retrieve the identifier corresponding to the method name associated to the method invocation
*/
public static IdentifierTree methodName(MethodInvocationTree mit) {
ExpressionTree methodSelect = mit.methodSelect();
IdentifierTree id;
if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
id = (IdentifierTree) methodSelect;
} else {
id = ((MemberSelectExpressionTree) methodSelect).identifier();
}
return id;
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class ExportsDirectiveTreeImplTest method exports_with_modules.
@Test
public void exports_with_modules() {
ExportsDirectiveTree exports = exportsDirective("exports org.foo to com.module1, module2;");
assertThat(exports.kind()).isEqualTo(Tree.Kind.EXPORTS_DIRECTIVE);
assertThat(exports.directiveKeyword().text()).isEqualTo("exports");
ExpressionTree packageName = exports.packageName();
assertThat(packageName.is(Tree.Kind.MEMBER_SELECT)).isTrue();
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) packageName;
assertThat(((IdentifierTree) mset.expression()).name()).isEqualTo("org");
assertThat(mset.identifier().name()).isEqualTo("foo");
assertThat(exports.toKeyword().text()).isEqualTo("to");
ListTree<ModuleNameTree> moduleNames = exports.moduleNames();
assertThat(moduleNames).hasSize(2);
assertThat(moduleNames.get(0).stream().map(IdentifierTree::name)).containsExactly("com", "module1");
assertThat(moduleNames.separators()).hasSize(1);
assertThat(moduleNames.separators().iterator().next().text()).isEqualTo(",");
assertThat(moduleNames.get(1).stream().map(IdentifierTree::name)).containsExactly("module2");
assertThat(exports.semicolonToken().text()).isEqualTo(";");
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class SyntaxTreeDebug method memberSelectString.
private static String memberSelectString(MemberSelectExpressionTree expression) {
StringBuilder buffer = new StringBuilder();
ExpressionTree target = expression.expression();
switch(target.kind()) {
case IDENTIFIER:
buffer.append(identifierString((IdentifierTree) target));
break;
case METHOD_INVOCATION:
buffer.append(methodInvocationString((MethodInvocationTree) target));
break;
case VARIABLE:
buffer.append(variableString((VariableTree) target));
break;
case INT_LITERAL:
buffer.append(literalString((LiteralTree) target));
break;
default:
break;
}
buffer.append('.');
buffer.append(identifierString(expression.identifier()));
return buffer.toString();
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class SyntaxTreeDebug method methodInvocationString.
private static String methodInvocationString(MethodInvocationTree method) {
StringBuilder buffer = new StringBuilder();
ExpressionTree methodSelect = method.methodSelect();
if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
buffer.append(identifierString((IdentifierTree) methodSelect));
} else if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
buffer.append(memberSelectString((MemberSelectExpressionTree) methodSelect));
}
buffer.append('(');
buffer.append(toString(method.arguments()));
buffer.append(')');
return buffer.toString();
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class KeySetInsteadOfEntrySetCheck method getOwnerSymbol.
@CheckForNull
private static Symbol getOwnerSymbol(MethodInvocationTree tree) {
ExpressionTree expressionTree = tree.methodSelect();
// direct invocation: symbol is implicitly this
if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
return tree.symbol().owner();
}
expressionTree = ((MemberSelectExpressionTree) expressionTree).expression();
if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
return ((IdentifierTree) expressionTree).symbol();
} else {
return getFieldAccessedUsingSuperOrThis(expressionTree);
}
}
Aggregations