use of org.sonar.java.model.expression.NewClassTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method applySelectors.
private static ExpressionTree applySelectors(ExpressionTree primary, Optional<List<Tuple<Optional<InternalSyntaxToken>, ExpressionTree>>> selectors) {
ExpressionTree result = primary;
if (selectors.isPresent()) {
for (Tuple<Optional<InternalSyntaxToken>, ExpressionTree> tuple : selectors.get()) {
Optional<InternalSyntaxToken> dotTokenOptional = tuple.first();
ExpressionTree selector = tuple.second();
if (dotTokenOptional.isPresent()) {
InternalSyntaxToken dotToken = dotTokenOptional.get();
if (selector.is(Kind.IDENTIFIER)) {
IdentifierTreeImpl identifier = (IdentifierTreeImpl) selector;
result = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
} else {
MethodInvocationTreeImpl methodInvocation = (MethodInvocationTreeImpl) selector;
IdentifierTreeImpl identifier = (IdentifierTreeImpl) methodInvocation.methodSelect();
MemberSelectExpressionTreeImpl memberSelect = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
result = new MethodInvocationTreeImpl(memberSelect, methodInvocation.typeArguments(), (ArgumentListTreeImpl) methodInvocation.arguments());
}
} else if (selector.is(Kind.NEW_CLASS)) {
NewClassTreeImpl newClass = (NewClassTreeImpl) selector;
result = newClass.completeWithEnclosingExpression(result);
} else if (selector.is(Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTreeImpl arrayAccess = (ArrayAccessExpressionTreeImpl) selector;
result = arrayAccess.complete(result);
} else if (selector.is(Kind.MEMBER_SELECT)) {
MemberSelectExpressionTreeImpl memberSelect = (MemberSelectExpressionTreeImpl) selector;
result = memberSelect.completeWithExpression(result);
} else {
throw new IllegalStateException();
}
}
}
return result;
}
use of org.sonar.java.model.expression.NewClassTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method newEnumConstant.
public EnumConstantTreeImpl newEnumConstant(Optional<List<AnnotationTreeImpl>> annotations, InternalSyntaxToken identifierToken, Optional<ArgumentListTreeImpl> arguments, Optional<ClassTreeImpl> classBody, Optional<InternalSyntaxToken> commaToken) {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
ArgumentListTreeImpl defaultArguments = new ArgumentListTreeImpl(ImmutableList.<ExpressionTree>of(), ImmutableList.<SyntaxToken>of());
NewClassTreeImpl newClass = new NewClassTreeImpl(arguments.or(defaultArguments), classBody.orNull());
newClass.completeWithIdentifier(identifier);
return new EnumConstantTreeImpl(modifiers((Optional<List<ModifierTree>>) (Optional<?>) annotations), identifier, newClass, commaToken.orNull());
}
use of org.sonar.java.model.expression.NewClassTreeImpl in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitNewClass.
@Override
public void visitNewClass(NewClassTree tree) {
NewClassTreeImpl newClassTreeImpl = (NewClassTreeImpl) tree;
if (newClassTreeImpl.isTypeSet()) {
return;
}
List<JavaType> typeArgumentsTypes = ImmutableList.of();
if (tree.typeArguments() != null) {
resolveAs((List<Tree>) tree.typeArguments(), JavaSymbol.TYP);
typeArgumentsTypes = tree.typeArguments().stream().map(this::getType).collect(Collectors.toList());
}
resolveAs((List<ExpressionTree>) tree.arguments(), JavaSymbol.VAR);
List<JavaType> parameterTypes = getParameterTypes(tree.arguments());
Resolve.Env newClassEnv = semanticModel.getEnv(tree);
ExpressionTree enclosingExpression = tree.enclosingExpression();
TypeTree typeTree = tree.identifier();
IdentifierTree constructorIdentifier = newClassTreeImpl.getConstructorIdentifier();
JavaType identifierType = resolveIdentifierType(newClassEnv, enclosingExpression, typeTree, constructorIdentifier.name());
JavaSymbol.TypeJavaSymbol constructorIdentifierSymbol = (JavaSymbol.TypeJavaSymbol) identifierType.symbol();
constructorIdentifierSymbol.addUsage(constructorIdentifier);
parameterTypes = addImplicitOuterClassParameter(parameterTypes, constructorIdentifierSymbol);
Resolution resolution = resolveConstructorSymbol(constructorIdentifier, identifierType, newClassEnv, parameterTypes, typeArgumentsTypes);
JavaType constructedType = identifierType;
if (resolution.symbol().isMethodSymbol()) {
constructedType = ((MethodJavaType) resolution.type()).resultType;
if (constructedType.isTagged(JavaType.DEFERRED)) {
Tree parent = newClassTreeImpl.parent();
if (parent.is(Tree.Kind.MEMBER_SELECT)) {
constructedType = resolve.parametrizedTypeWithErasure((ParametrizedTypeJavaType) identifierType);
} else {
// will be resolved by type inference
((DeferredType) constructedType).setTree(newClassTreeImpl);
}
} else if (identifierType.symbol().isInterface()) {
// constructor of interface always resolve to 'Object' no-arg constructor
registerType(typeTree, identifierType);
} else {
registerType(typeTree, resolution.type());
}
}
ClassTree classBody = tree.classBody();
if (classBody != null) {
constructedType = getAnonymousClassType(identifierType, constructedType, classBody);
}
registerType(tree, constructedType);
}
use of org.sonar.java.model.expression.NewClassTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method newExpression.
public ExpressionTree newExpression(InternalSyntaxToken newToken, Optional<List<AnnotationTreeImpl>> annotations, ExpressionTree partial) {
TypeTree typeTree;
if (partial.is(Tree.Kind.NEW_CLASS)) {
NewClassTreeImpl newClassTree = (NewClassTreeImpl) partial;
newClassTree.completeWithNewKeyword(newToken);
typeTree = newClassTree.identifier();
} else {
NewArrayTreeImpl newArrayTree = (NewArrayTreeImpl) partial;
newArrayTree.completeWithNewKeyword(newToken);
typeTree = newArrayTree.type();
}
completeTypeTreeWithAnnotations(typeTree, annotations);
return partial;
}
Aggregations