use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class StaticMethodCheck method visitIdentifier.
@Override
public void visitIdentifier(IdentifierTree tree) {
super.visitIdentifier(tree);
if ("class".equals(tree.name()) || methodReferences.isEmpty()) {
return;
}
if (parentIs(tree, Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree parent = (MemberSelectExpressionTree) tree.parent();
// New class may use member select to denote an inner class
if (tree.equals(parent.identifier()) && !parentIs(parent, Tree.Kind.NEW_CLASS) && !refToEnclosingClass(tree)) {
return;
}
}
visitTerminalIdentifier(tree);
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method explicit_constructor_invocation.
/**
* 8.8.7.1. Explicit Constructor Invocations
*/
@Test
public void explicit_constructor_invocation() {
// TODO test NonWildTypeArguments
MethodInvocationTree tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { this(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>this(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
MemberSelectExpressionTree methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
assertThatChildrenIteratorHasSize(methodSelect, 3);
assertThat(methodSelect.identifier().name()).isEqualTo("super");
assertThat(methodSelect.operatorToken()).isNotNull();
assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.<T>super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
assertThatChildrenIteratorHasSize(methodSelect, 3);
assertThat(methodSelect.identifier().name()).isEqualTo("super");
assertThat(methodSelect.operatorToken()).isNotNull();
assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
assertThat(tree.arguments()).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method field_access_expression.
/**
* 15.11. Field Access Expressions
*/
@Test
public void field_access_expression() {
MemberSelectExpressionTree tree;
tree = (MemberSelectExpressionTree) expressionOfReturnStatement("class T { int m() { return super.identifier; } }");
assertThat(tree.is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.identifier()).isNotNull();
assertThat(tree.operatorToken()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 3);
tree = (MemberSelectExpressionTree) expressionOfReturnStatement("class T { int m() { return ClassName.super.identifier; } }");
assertThat(tree.is(Tree.Kind.MEMBER_SELECT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.identifier()).isNotNull();
assertThat(tree.operatorToken()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 3);
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method lambda_expressions.
@Test
public void lambda_expressions() {
String code = "class T { public void meth(){IntStream.range(1,12).map(x->x*x).map((int a)-> {return a*a;});}}";
ExpressionTree expressionTree = ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
// parsing not broken by lambda
assertThat(expressionTree).isNotNull();
MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
LambdaExpressionTree tree = (LambdaExpressionTree) mit.arguments().get(0);
assertThat(tree.openParenToken()).isNotNull();
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.closeParenToken()).isNotNull();
assertThat(tree.arrowToken()).isNotNull();
assertThat(tree.body().is(Tree.Kind.BLOCK)).isTrue();
tree = (LambdaExpressionTree) ((MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression()).arguments().get(0);
assertThat(tree.openParenToken()).isNull();
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.closeParenToken()).isNull();
assertThat(tree.arrowToken()).isNotNull();
assertThat(tree.body().is(Tree.Kind.MULTIPLY)).isTrue();
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class OpensDirectiveTreeImplTest method opens_with_modules.
@Test
public void opens_with_modules() {
OpensDirectiveTree exports = moduleDirective("opens org.foo to com.module1, module2;");
assertThat(exports.kind()).isEqualTo(Tree.Kind.OPENS_DIRECTIVE);
assertThat(exports.directiveKeyword().text()).isEqualTo("opens");
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(";");
}
Aggregations