use of org.sonar.java.resolve.Resolve.Resolution 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.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveIdentifierType.
private JavaType resolveIdentifierType(Resolve.Env newClassEnv, @Nullable ExpressionTree enclosingExpression, TypeTree typeTree, String typeName) {
if (enclosingExpression != null) {
resolveAs(enclosingExpression, JavaSymbol.VAR);
Resolution idType = resolve.findIdentInType(newClassEnv, (JavaSymbol.TypeJavaSymbol) enclosingExpression.symbolType().symbol(), typeName, JavaSymbol.TYP);
JavaType type = idType.type();
if (typeTree.is(Tree.Kind.PARAMETERIZED_TYPE)) {
TypeArguments typeArguments = ((ParameterizedTypeTree) typeTree).typeArguments();
scan(typeArguments);
type = parametrizedTypeWithTypeArguments(type.symbol, typeArguments);
}
registerType(typeTree, type);
} else {
resolveAs(typeTree, JavaSymbol.TYP, newClassEnv, false);
}
return (JavaType) typeTree.symbolType();
}
use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method inferReturnTypeFromInferedArgs.
private void inferReturnTypeFromInferedArgs(MethodInvocationTree tree, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes, JavaType returnType, TypeSubstitution typeSubstitution) {
List<JavaType> parameterTypes = getParameterTypes(tree.arguments());
if (!parameterTypes.equals(argTypes)) {
IdentifierTree identifier = null;
Resolution resolution = null;
Tree methodSelect = tree.methodSelect();
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
JavaType type = getType(mset.expression());
if (type.isTagged(JavaType.DEFERRED)) {
throw new IllegalStateException("type of arg should not be defered anymore ??");
}
identifier = mset.identifier();
resolution = resolve.findMethod(methodEnv, type, identifier.name(), parameterTypes, typeParamTypes);
} else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
identifier = (IdentifierTree) methodSelect;
resolution = resolve.findMethod(methodEnv, identifier.name(), parameterTypes, typeParamTypes);
}
if (resolution != null && returnType != resolution.type() && resolution.symbol().isMethodSymbol()) {
MethodJavaType methodType = (MethodJavaType) resolution.type();
if (!methodType.resultType.isTagged(JavaType.DEFERRED)) {
registerType(tree, resolve.applySubstitution(methodType.resultType, typeSubstitution));
// update type associated to identifier as it may have been inferred deeper when re-resolving method with new parameter types
registerType(identifier, methodType);
}
}
} else {
registerType(tree, resolve.applySubstitution(returnType, typeSubstitution));
}
}
Aggregations