use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveConstructorSymbol.
private Resolve.Resolution resolveConstructorSymbol(IdentifierTree identifier, Type type, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeArgumentsTypes) {
Resolve.Resolution resolution = resolve.findMethod(methodEnv, (JavaType) type, "<init>", argTypes, typeArgumentsTypes);
JavaSymbol symbol = resolution.symbol();
inferArgumentTypes(argTypes, resolution);
associateReference(identifier, symbol);
return resolution;
}
use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveAs.
public JavaSymbol resolveAs(Tree tree, int kind, Resolve.Env resolveEnv, boolean associateReference) {
if (tree.is(Tree.Kind.IDENTIFIER, Tree.Kind.MEMBER_SELECT)) {
JavaSymbol resolvedSymbol;
IdentifierTree identifierTree;
if (tree.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mse = (MemberSelectExpressionTree) tree;
if (JavaKeyword.CLASS.getValue().equals(mse.identifier().name())) {
// resolve type of expression xxx.class
return resolveClassType(tree, resolveEnv, mse);
}
identifierTree = mse.identifier();
List<AnnotationTree> identifierAnnotations = identifierTree.annotations();
scan(identifierAnnotations);
completeMetadata((JavaSymbol) identifierTree.symbol(), identifierAnnotations);
Resolve.Resolution res = getSymbolOfMemberSelectExpression(mse, kind, resolveEnv);
resolvedSymbol = res.symbol();
JavaType resolvedType = resolve.resolveTypeSubstitution(res.type(), getType(mse.expression()));
registerType(identifierTree, resolvedType);
registerType(tree, resolvedType);
} else {
identifierTree = (IdentifierTree) tree;
Resolve.Resolution resolution = resolve.findIdent(resolveEnv, identifierTree.name(), kind);
resolvedSymbol = resolution.symbol();
registerType(tree, resolution.type());
}
if (associateReference) {
associateReference(identifierTree, resolvedSymbol);
}
return resolvedSymbol;
}
tree.accept(this);
JavaType type = getType(tree);
if (tree.is(Tree.Kind.INFERED_TYPE)) {
type = symbols.deferedType((AbstractTypedTree) tree);
registerType(tree, type);
}
if (type == null) {
throw new IllegalStateException("Type not resolved " + tree);
}
return type.symbol;
}
use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveMethodSymbol.
@CheckForNull
private Resolve.Resolution resolveMethodSymbol(Tree methodSelect, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes) {
Resolve.Resolution resolution;
IdentifierTree identifier;
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
resolveAs(mset.expression(), JavaSymbol.TYP | JavaSymbol.VAR);
JavaType type = getType(mset.expression());
if (type.isTagged(JavaType.DEFERRED)) {
return null;
}
identifier = mset.identifier();
resolution = resolve.findMethod(methodEnv, type, identifier.name(), argTypes, typeParamTypes);
} else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
identifier = (IdentifierTree) methodSelect;
resolution = resolve.findMethod(methodEnv, identifier.name(), argTypes, typeParamTypes);
} else {
throw new IllegalStateException("Method select in method invocation is not of the expected type " + methodSelect);
}
registerType(identifier, resolution.type());
associateReference(identifier, resolution.symbol());
return resolution;
}
use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveMethodReference.
private void resolveMethodReference(JavaSymbol.MethodJavaSymbol samMethod, MethodReferenceTreeImpl methodRefTree) {
JavaType methodRefType = (JavaType) methodRefTree.symbolType();
JavaType samReturnType = (JavaType) samMethod.returnType().type();
List<JavaType> samMethodArgs = resolve.findSamMethodArgs(methodRefType);
Resolution resolution = resolve.findMethodReference(semanticModel.getEnv(methodRefTree), samMethodArgs, methodRefTree);
JavaSymbol methodSymbol = resolution.symbol();
if (methodSymbol.isMethodSymbol()) {
IdentifierTree methodIdentifier = methodRefTree.method();
addMethodRefReference(methodIdentifier, methodSymbol);
setMethodRefType(methodRefTree, methodRefType, resolution.type());
JavaType capturedReturnType = resolve.resolveTypeSubstitution(samReturnType, methodRefType);
JavaType refinedReturnType = ((MethodJavaType) methodIdentifier.symbolType()).resultType();
if ("<init>".equals(methodSymbol.name)) {
refinedReturnType = refinedTypeForConstructor(capturedReturnType, refinedReturnType);
}
if (refinedReturnType instanceof DeferredType) {
((DeferredType) refinedReturnType).setTree((AbstractTypedTree) methodRefTree.method());
}
refineType(methodRefTree, methodRefType, capturedReturnType, refinedReturnType);
} else {
handleNewArray(methodRefTree, methodRefType, samReturnType);
}
}
use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitMethodInvocation.
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
MethodInvocationTreeImpl mit = (MethodInvocationTreeImpl) tree;
Resolve.Env methodEnv = semanticModel.getEnv(tree);
if (mit.isTypeSet() && mit.symbol().isMethodSymbol()) {
TypeSubstitution typeSubstitution = inferedSubstitution(mit);
List<JavaType> argTypes = getParameterTypes(tree.arguments());
JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
List<JavaType> formals = methodSymbol.parameterTypes().stream().map(t -> (JavaType) t).collect(Collectors.toList());
List<JavaType> inferedArgTypes = resolve.resolveTypeSubstitution(formals, typeSubstitution);
int size = inferedArgTypes.size();
IntStream.range(0, argTypes.size()).forEach(i -> {
JavaType arg = argTypes.get(i);
Type formal = inferedArgTypes.get(Math.min(i, size - 1));
if (formal != arg) {
AbstractTypedTree argTree = (AbstractTypedTree) mit.arguments().get(i);
argTree.setInferedType(formal);
argTree.accept(this);
}
});
List<JavaType> typeParamTypes = getParameterTypes(tree.typeArguments());
JavaType resultType = ((MethodJavaType) mit.symbol().type()).resultType;
// if result type is a type var defined by the method we are solving, use the target type.
if (resultType.symbol.owner == mit.symbol()) {
resultType = (JavaType) mit.symbolType();
}
inferReturnTypeFromInferedArgs(tree, methodEnv, argTypes, typeParamTypes, resultType, typeSubstitution);
return;
}
scan(tree.arguments());
scan(tree.typeArguments());
List<JavaType> argTypes = getParameterTypes(tree.arguments());
List<JavaType> typeParamTypes = getParameterTypes(tree.typeArguments());
Resolve.Resolution resolution = resolveMethodSymbol(tree.methodSelect(), methodEnv, argTypes, typeParamTypes);
JavaSymbol symbol;
JavaType returnType;
if (resolution == null) {
returnType = symbols.deferedType(mit);
symbol = Symbols.unknownSymbol;
} else {
symbol = resolution.symbol();
returnType = resolution.type();
if (symbol.isMethodSymbol()) {
MethodJavaType methodType = (MethodJavaType) resolution.type();
returnType = methodType.resultType;
}
}
mit.setSymbol(symbol);
if (returnType != null && returnType.isTagged(JavaType.DEFERRED)) {
((DeferredType) returnType).setTree(mit);
}
registerType(tree, returnType);
if (resolution != null) {
inferArgumentTypes(argTypes, resolution);
inferReturnTypeFromInferedArgs(tree, methodEnv, argTypes, typeParamTypes, returnType, new TypeSubstitution());
}
}
Aggregations