use of org.sonar.java.model.expression.MethodInvocationTreeImpl 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.MethodInvocationTreeImpl 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());
}
}
use of org.sonar.java.model.expression.MethodInvocationTreeImpl in project sonar-java by SonarSource.
the class TreeFactory method newIdentifierOrMethodInvocation.
public ExpressionTree newIdentifierOrMethodInvocation(Optional<TypeArgumentListTreeImpl> typeArguments, InternalSyntaxToken identifierToken, Optional<ArgumentListTreeImpl> arguments) {
IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
ExpressionTree result = identifier;
if (arguments.isPresent()) {
result = new MethodInvocationTreeImpl(identifier, typeArguments.orNull(), arguments.get());
}
return result;
}
Aggregations