use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class LeftCurlyBraceBaseTreeVisitor method visitNewClass.
@Override
public void visitNewClass(NewClassTree tree) {
ClassTree classBody = tree.classBody();
if (classBody != null && tree.arguments().closeParenToken() != null) {
checkTokens(tree.arguments().closeParenToken(), classBody.openBraceToken());
}
super.visitNewClass(tree);
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class LeftCurlyBraceBaseTreeVisitor method visitEnumConstant.
@Override
public void visitEnumConstant(EnumConstantTree tree) {
NewClassTree initializer = tree.initializer();
ClassTree classBody = initializer.classBody();
if (classBody != null) {
SyntaxToken openBraceToken = classBody.openBraceToken();
if (initializer.arguments().closeParenToken() != null) {
checkTokens(initializer.arguments().closeParenToken(), openBraceToken);
} else {
checkTokens(tree.simpleName().identifierToken(), openBraceToken);
}
}
super.visitEnumConstant(tree);
}
use of org.sonar.plugins.java.api.tree.NewClassTree 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.NewClassTree in project sonar-java by SonarSource.
the class SymbolTableTest method anonymousClassWithDiamondOperator.
/**
* JLS9 - ยง15.9.3
* @since Java 9
*/
@Test
public void anonymousClassWithDiamondOperator() {
Result result = Result.createForJavaFile("src/test/files/resolve/Java9DiamondOperatorAnonymousClass");
NewClassTree nct;
ClassJavaType symbolType;
ClassJavaType superType;
ParametrizedTypeJavaType ptjt;
// ArrayList<>(), not anonymous but with diamond
nct = parentNewClassTree(result.referenceTree(7, 16));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.is("java.util.ArrayList")).isTrue();
assertThat(symbolType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) symbolType;
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
// B<Integer>(), no diamond
nct = parentNewClassTree(result.referenceTree(11, 16));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.isSubtypeOf("B")).isTrue();
superType = symbolType.getSuperType();
assertThat(superType.is("B")).isTrue();
assertThat(superType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) superType;
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
// B<>(Integer), diamond with inference from B<U> to B<Integer>
nct = parentNewClassTree(result.referenceTree(15, 16));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.isSubtypeOf("B")).isTrue();
superType = symbolType.getSuperType();
assertThat(superType.is("B")).isTrue();
assertThat(superType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) superType;
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
// B<>(T), diamond with inference from B<U> to B<T>
nct = parentNewClassTree(result.referenceTree(19, 16));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.isSubtypeOf("B")).isTrue();
superType = symbolType.getSuperType();
assertThat(superType.is("B")).isTrue();
assertThat(superType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) superType;
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).symbol().name()).isEqualTo("T");
// C<>(Integer, String[]), diamond with inference from C<U,V> to C<Integer, String>
nct = parentNewClassTree(result.referenceTree(23, 16));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.isSubtypeOf("C")).isTrue();
superType = symbolType.getSuperType();
assertThat(superType.is("C")).isTrue();
assertThat(superType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) superType;
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isTrue();
assertThat(ptjt.typeSubstitution.substitutedTypes().get(1).is("java.lang.String")).isTrue();
// D<>(), diamond with inference on interface from D<X> to D<Integer>
nct = parentNewClassTree(result.referenceTree(27, 16));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.isSubtypeOf("D")).isTrue();
superType = (ClassJavaType) symbolType.interfaces.get(0);
assertThat(superType.is("D")).isTrue();
assertThat(superType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) superType;
// FIXME SONARJAVA-1706 : requires inference from target type
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).symbol().name()).isEqualTo("X");
// should be true
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isFalse();
// B<>(), diamond with inference on class from B<U> to B<Integer>
nct = parentNewClassTree(result.referenceTree(34, 13));
symbolType = (ClassJavaType) nct.symbolType();
assertThat(symbolType.isSubtypeOf("B")).isTrue();
superType = symbolType.getSuperType();
assertThat(superType.is("B")).isTrue();
assertThat(superType.isParameterized()).isTrue();
ptjt = (ParametrizedTypeJavaType) superType;
// FIXME SONARJAVA-1706 : requires inference from target type
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).symbol().name()).isEqualTo("U");
// should be true
assertThat(ptjt.typeSubstitution.substitutedTypes().get(0).is("java.lang.Integer")).isFalse();
}
use of org.sonar.plugins.java.api.tree.NewClassTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method parametrized_constructor.
@Test
public void parametrized_constructor() {
CompilationUnitTree compilationUnit = treeOf("class A { <T> A(T t) {} void test() { new A(\"hello\"); } }");
MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
NewClassTree newClassTree = (NewClassTree) ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
Type identifierType = newClassTree.identifier().symbolType();
assertThat(identifierType).isInstanceOf(MethodJavaType.class);
assertThat(((MethodJavaType) identifierType).argTypes.get(0).is("java.lang.String")).isTrue();
}
Aggregations