use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class LiveVariables method processBlockElements.
private void processBlockElements(CFG.Block block, Set<Symbol> blockKill, Set<Symbol> blockGen) {
// process elements from bottom to top
Set<Tree> assignmentLHS = new HashSet<>();
for (Tree element : Lists.reverse(block.elements())) {
switch(element.kind()) {
case ASSIGNMENT:
processAssignment((AssignmentExpressionTree) element, blockKill, blockGen, assignmentLHS);
break;
case IDENTIFIER:
processIdentifier((IdentifierTree) element, blockGen, assignmentLHS);
break;
case MEMBER_SELECT:
processMemberSelect((MemberSelectExpressionTree) element, assignmentLHS, blockGen);
break;
case VARIABLE:
blockKill.add(((VariableTree) element).symbol());
blockGen.remove(((VariableTree) element).symbol());
break;
case LAMBDA_EXPRESSION:
blockGen.addAll(getUsedVariables(((LambdaExpressionTree) element).body(), cfg.methodSymbol()));
break;
case METHOD_REFERENCE:
blockGen.addAll(getUsedVariables(((MethodReferenceTree) element).expression(), cfg.methodSymbol()));
break;
case NEW_CLASS:
blockGen.addAll(getUsedVariables(((NewClassTree) element).classBody(), cfg.methodSymbol()));
break;
default:
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SymbolTableTest method recursive_type_substitution.
@Test
public void recursive_type_substitution() {
Result result = Result.createFor("Generics");
MethodTree ddt_method = (MethodTree) result.symbol("ddt_method").declaration();
VariableTree variable = (VariableTree) ddt_method.block().body().get(0);
assertThat(variable.initializer().symbolType().name()).isEqualTo("String");
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SymbolTableTest method Generics.
@Test
public void Generics() {
Result result = Result.createFor("Generics");
JavaSymbol.TypeJavaSymbol typeSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("A");
JavaSymbol symbolA1 = result.symbol("a1");
assertThat(symbolA1.type.symbol).isSameAs(typeSymbol);
JavaSymbol symbolA2 = result.symbol("a1");
assertThat(symbolA2.type.symbol).isSameAs(typeSymbol);
assertThat(symbolA2.type).isSameAs(symbolA1.type);
JavaSymbol symbolA3 = result.symbol("a1");
assertThat(symbolA3.type.symbol).isSameAs(typeSymbol);
assertThat(result.reference(12, 5)).isSameAs(result.symbol("foo", 8));
assertThat(result.reference(13, 5)).isSameAs(result.symbol("foo", 9));
// Check erasure
TypeVariableJavaType STypeVariableType = (TypeVariableJavaType) typeSymbol.typeParameters.lookup("S").get(0).type;
assertThat(STypeVariableType.erasure().getSymbol().getName()).isEqualTo("CharSequence");
JavaType arrayErasure = typeSymbol.members().lookup("arrayErasure").get(0).type;
assertThat(arrayErasure.isTagged(JavaType.ARRAY)).isTrue();
assertThat(arrayErasure.erasure().isTagged(JavaType.ARRAY)).isTrue();
assertThat(((ArrayJavaType) arrayErasure.erasure()).elementType().symbol.getName()).isEqualTo("CharSequence");
IdentifierTree tree = result.referenceTree(20, 7);
JavaType symbolType = (JavaType) tree.symbolType();
assertThat(symbolType).isInstanceOf(ParametrizedTypeJavaType.class);
ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) symbolType;
assertThat(ptt.symbol.getName()).isEqualTo("C");
assertThat(ptt.typeSubstitution.size()).isEqualTo(1);
assertThat(ptt.typeSubstitution.substitutedType(ptt.typeSubstitution.typeVariables().iterator().next()).symbol.getName()).isEqualTo("String");
JavaSymbol.MethodJavaSymbol method1 = (JavaSymbol.MethodJavaSymbol) typeSymbol.members().lookup("method1").get(0);
assertThat(((MethodJavaType) method1.type).resultType).isSameAs(STypeVariableType);
JavaSymbol.MethodJavaSymbol method2 = (JavaSymbol.MethodJavaSymbol) typeSymbol.members().lookup("method2").get(0);
TypeVariableJavaType PTypeVariableType = (TypeVariableJavaType) method2.typeParameters().lookup("P").get(0).type;
assertThat(method2.getReturnType().type).isSameAs(PTypeVariableType);
assertThat(method2.parameterTypes().get(0)).isSameAs(PTypeVariableType);
// Type parameter defined in outer class
JavaSymbol.TypeJavaSymbol classCSymbol = (JavaSymbol.TypeJavaSymbol) typeSymbol.members().lookup("C").get(0);
JavaSymbol innerClassField = classCSymbol.members().lookup("innerClassField").get(0);
assertThat(innerClassField.type).isSameAs(STypeVariableType);
// Unknown parametrized type should be tagged as unknown
MethodTree methodTree = (MethodTree) result.symbol("unknownSymbol").declaration();
VariableTree variableTree = (VariableTree) methodTree.block().body().get(0);
assertThat(variableTree.type().symbolType().isUnknown()).isTrue();
// Inner class referenced as type parameter in super class/interface
assertThat(result.reference(68, 53)).isSameAs(result.symbol("B", 69));
JavaSymbol applyMethod = result.symbol("apply");
assertThat(result.reference(83, 12)).isSameAs(applyMethod);
assertThat(result.reference(89, 61)).isSameAs(applyMethod);
// this method does not compile but is resolved whereas it should not.
assertThat(result.reference(85, 12)).isSameAs(applyMethod);
assertThat(applyMethod.usages()).hasSize(3);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ComplexityVisitorTest method lambda_complexity.
@Test
public void lambda_complexity() throws Exception {
CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A { Function f = s -> {if(s.isEmpty()) return s; return new MyClass(){ void foo(){if(a) return;} };};}");
ExpressionTree lambda = ((VariableTree) ((ClassTree) cut.types().get(0)).members().get(0)).initializer();
List<Tree> nodes = new ComplexityVisitor().getNodes(lambda);
assertThat(nodes).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class PublicApiCheckerTest method retrieveJavadoc.
@Test
public void retrieveJavadoc() {
new SubscriptionVisitor() {
@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.values());
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
checkApi(tree, variableTree.simpleName().name());
} else if (tree.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) {
MethodTree methodTree = (MethodTree) tree;
checkApi(tree, methodTree.simpleName().name());
} else if (tree.is(Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE)) {
IdentifierTree idTree = ((ClassTree) tree).simpleName();
checkApi(tree, idTree == null ? "" : idTree.name());
} else {
checkApi(tree, "");
}
}
}.scanTree(cut);
}
Aggregations