use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method local_variable_declaration.
/**
* 14.4. Local Variable Declaration Statements
*/
@Test
public void local_variable_declaration() {
BlockTree block = ((MethodTree) firstTypeMember("class T { void m() { int a = 42, b[]; final @Nullable int c = 42; } }")).block();
List<StatementTree> declarations = block.body();
assertThat(declarations).hasSize(3);
VariableTree tree = (VariableTree) declarations.get(0);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).isEmpty();
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("a");
assertThat(tree.initializer()).isNotNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(",");
assertThatChildrenIteratorHasSize(tree, 6);
tree = (VariableTree) declarations.get(1);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).isEmpty();
assertThat(tree.type()).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets((ArrayTypeTree) tree.type());
assertThat(tree.simpleName().name()).isEqualTo("b");
assertThat(tree.initializer()).isNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 4);
// TODO Test annotation
tree = (VariableTree) declarations.get(2);
assertThat(tree.is(Tree.Kind.VARIABLE)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.FINAL);
assertThat(tree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.simpleName().name()).isEqualTo("c");
assertThat(tree.initializer()).isNotNull();
assertThat(tree.endToken()).isNotNull();
assertThat(tree.endToken().text()).isEqualTo(";");
assertThatChildrenIteratorHasSize(tree, 6);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class LambdaBlockReturnVisitorTest method test.
@Test
public void test() throws Exception {
CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {\n" + " java.util.function.Consumer<String> c = s -> {\n" + " if(s.length()>0) {\n" + " return;\n" + " }\n" + " System.out.println(s);\n" + " };\n" + "\n" + " java.util.function.Function<String, String> f = s -> {\n" + " if(s.length() > 0) {\n" + " return s.replace('a', 'b');\n" + " }\n" + " return unknownSymbol;\n" + " };\n" + " }");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
List<VariableTree> vars = ((ClassTree) cut.types().get(0)).members().stream().map(m -> (VariableTree) m).collect(Collectors.toList());
LambdaBlockReturnVisitor visitor = new LambdaBlockReturnVisitor();
((LambdaExpressionTree) vars.get(0).initializer()).body().accept(visitor);
assertThat(visitor.types).isEmpty();
visitor = new LambdaBlockReturnVisitor();
((LambdaExpressionTree) vars.get(1).initializer()).body().accept(visitor);
assertThat(visitor.types).hasSize(1);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ProtectedMemberInFinalClassCheck method checkMember.
private void checkMember(Tree member) {
if (member.is(Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) member;
checkVariableCompliance(variableTree);
} else if (member.is(Kind.METHOD)) {
MethodTree methodTree = (MethodTree) member;
if (Boolean.FALSE.equals(methodTree.isOverriding())) {
checkMethodCompliance(methodTree);
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ModulusEqualityCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.EQUAL_TO)) {
BinaryExpressionTree equality = (BinaryExpressionTree) tree;
checkModulusAndIntLiteral(equality.leftOperand(), equality.rightOperand());
checkModulusAndIntLiteral(equality.rightOperand(), equality.leftOperand());
} else {
MethodTree methodTree = (MethodTree) tree;
for (VariableTree variableTree : methodTree.parameters()) {
methodParams.add(variableTree.symbol());
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method targetType.
@CheckForNull
private static Type targetType(TypeCastTree tree) {
Tree parent = skipParentheses(tree.parent());
Type target = null;
if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
Tree method = parent;
while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
method = method.parent();
}
target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
} else if (parent.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) parent;
target = variableTree.symbol().type();
} else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) parent;
if (mit.symbol().isMethodSymbol()) {
JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
int castArgIndex = mit.arguments().indexOf(tree);
target = sym.parameterTypes().get(castArgIndex);
}
} else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
target = tree.type().symbolType();
} else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
} else if (parent instanceof ExpressionTree) {
target = ((ExpressionTree) parent).symbolType();
}
return target;
}
Aggregations