use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrNumericBinaryExpressionTypeCalculator method fun.
@Nullable
@Override
public PsiType fun(GrOperatorExpression e) {
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(e.multiResolve(false));
if (resolveResult.isApplicable() && !PsiUtil.isDGMMethod(resolveResult.getElement())) {
return ResolveUtil.extractReturnTypeFromCandidate(resolveResult, e, new PsiType[] { e.getRightType() });
}
PsiType lType = e.getLeftType();
PsiType rType = e.getRightType();
if (TypesUtil.isNumericType(lType) && TypesUtil.isNumericType(rType)) {
return inferNumericType(lType, rType, e);
}
return ResolveUtil.extractReturnTypeFromCandidate(resolveResult, e, new PsiType[] { rType });
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrArgumentLabelImpl method multiResolve.
@NotNull
@Override
public GroovyResolveResult[] multiResolve(boolean incompleteCode) {
final ResolveResult[] results = getRealReference().multiResolve(incompleteCode);
if (results instanceof GroovyResolveResult[]) {
return (GroovyResolveResult[]) results;
} else {
final GroovyResolveResult[] results1 = new GroovyResolveResult[results.length];
for (int i = 0; i < results.length; i++) {
ResolveResult result = results[i];
final PsiElement element = result.getElement();
if (element == null) {
results1[i] = GroovyResolveResult.EMPTY_RESULT;
} else {
results1[i] = new GroovyResolveResultImpl(element, true);
}
}
return results1;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class DefaultCallExpressionTypeCalculator method calculateReturnType.
@Override
public PsiType calculateReturnType(@NotNull GrMethodCall callExpression, GroovyResolveResult[] resolveResults) {
GrExpression invoked = callExpression.getInvokedExpression();
if (invoked instanceof GrReferenceExpression) {
GrReferenceExpression refExpr = (GrReferenceExpression) invoked;
PsiManager manager = callExpression.getManager();
PsiType result = null;
for (GroovyResolveResult resolveResult : resolveResults) {
PsiType returnType = calculateReturnTypeInner(callExpression, refExpr, resolveResult);
if (returnType == null)
return null;
PsiType nonVoid = PsiType.VOID.equals(returnType) && !isCompileStatic(callExpression) ? PsiType.NULL : returnType;
PsiType normalized = nonVoid instanceof GrLiteralClassType ? nonVoid : TypesUtil.substituteAndNormalizeType(nonVoid, resolveResult.getSubstitutor(), resolveResult.getSpreadState(), callExpression);
LOG.assertTrue(normalized != null, "return type: " + returnType + "; substitutor: " + resolveResult.getSubstitutor());
if (result == null || normalized.isAssignableFrom(result)) {
result = normalized;
} else if (!result.isAssignableFrom(normalized)) {
result = TypesUtil.getLeastUpperBound(result, normalized, manager);
}
}
return result;
} else {
return extractReturnTypeFromType(invoked.getType(), false, callExpression);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrIndexPropertyImpl method inferType.
private PsiType inferType(@Nullable Boolean isSetter) {
PsiType arrayType = inferArrayType();
if (arrayType != null) {
return arrayType;
}
GrExpression selected = getInvokedExpression();
PsiType thisType = selected.getType();
if (thisType == null) {
thisType = TypesUtil.getJavaLangObject(this);
}
GrArgumentList argList = getArgumentList();
PsiType[] argTypes = PsiUtil.getArgumentTypes(argList);
if (argTypes == null)
return null;
final PsiManager manager = getManager();
final GlobalSearchScope resolveScope = getResolveScope();
if (PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, this, isSetter != null ? isSetter.booleanValue() : PsiUtil.isLValue(this))) {
return TypesUtil.boxPrimitiveType(((PsiArrayType) thisType).getComponentType(), manager, resolveScope);
}
final GroovyResolveResult[] candidates;
if (isSetter != null) {
candidates = isSetter.booleanValue() ? multiResolveSetter(false) : multiResolveGetter(false);
} else {
candidates = multiResolve(false);
}
//don't use short PsiUtil.getArgumentTypes(...) because it use incorrect 'isSetter' value
PsiType[] args = PsiUtil.getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, true, null);
final GroovyResolveResult candidate = PsiImplUtil.extractUniqueResult(candidates);
final PsiElement element = candidate.getElement();
if (element instanceof PsiNamedElement) {
final String name = ((PsiNamedElement) element).getName();
if ("putAt".equals(name) && args != null) {
args = ArrayUtil.append(args, TypeInferenceHelper.getInitializerTypeFor(this), PsiType.class);
}
}
PsiType overloadedOperatorType = ResolveUtil.extractReturnTypeFromCandidate(candidate, this, args);
PsiType componentType = extractMapValueType(thisType, args, manager, resolveScope);
if (overloadedOperatorType != null && (componentType == null || !TypesUtil.isAssignableByMethodCallConversion(overloadedOperatorType, componentType, selected))) {
return TypesUtil.boxPrimitiveType(overloadedOperatorType, manager, resolveScope);
}
return componentType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrIndexPropertyImpl method resolveImpl.
private GroovyResolveResult[] resolveImpl(boolean incompleteCode, @Nullable GrExpression upToArgument, @Nullable Boolean isSetter) {
if (isSetter == null)
isSetter = PsiUtil.isLValue(this);
GrExpression invoked = getInvokedExpression();
PsiType thisType = invoked.getType();
if (thisType == null) {
thisType = TypesUtil.getJavaLangObject(this);
}
GrArgumentList argList = getArgumentList();
//don't use short PsiUtil.getArgumentTypes(...) because it use incorrect 'isSetter' value
PsiType[] argTypes = PsiUtil.getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, true, upToArgument);
if (argTypes == null)
return GroovyResolveResult.EMPTY_ARRAY;
final GlobalSearchScope resolveScope = getResolveScope();
if (argTypes.length == 0) {
PsiType arrType = null;
if (invoked instanceof GrBuiltinTypeClassExpression) {
arrType = ((GrBuiltinTypeClassExpression) invoked).getPrimitiveType();
}
if (invoked instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) invoked).resolve();
if (resolved instanceof PsiClass) {
String qname = ((PsiClass) resolved).getQualifiedName();
if (qname != null) {
arrType = TypesUtil.createTypeByFQClassName(qname, this);
}
}
}
if (arrType != null) {
return GroovyResolveResult.EMPTY_ARRAY;
}
}
GroovyResolveResult[] candidates;
final String name = isSetter ? "putAt" : "getAt";
if (isSetter && !incompleteCode) {
argTypes = ArrayUtil.append(argTypes, TypeInferenceHelper.getInitializerTypeFor(this), PsiType.class);
}
if (PsiImplUtil.isSimpleArrayAccess(thisType, argTypes, this, isSetter)) {
return GroovyResolveResult.EMPTY_ARRAY;
}
candidates = ResolveUtil.getMethodCandidates(thisType, name, invoked, true, incompleteCode, argTypes);
//hack for remove DefaultGroovyMethods.getAt(Object, ...)
if (candidates.length == 2) {
for (int i = 0; i < candidates.length; i++) {
GroovyResolveResult candidate = candidates[i];
final PsiElement element = candidate.getElement();
if (element instanceof GrGdkMethod) {
final PsiMethod staticMethod = ((GrGdkMethod) element).getStaticMethod();
final PsiParameter param = staticMethod.getParameterList().getParameters()[0];
if (param.getType().equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
return new GroovyResolveResult[] { candidates[1 - i] };
}
}
}
}
if (candidates.length != 1) {
final GrTupleType tupleType = new GrImmediateTupleType(argTypes, JavaPsiFacade.getInstance(getProject()), resolveScope);
final GroovyResolveResult[] tupleCandidates = ResolveUtil.getMethodCandidates(thisType, name, invoked, tupleType);
if (incompleteCode) {
candidates = ArrayUtil.mergeArrays(candidates, tupleCandidates, count -> new GroovyResolveResult[count]);
} else {
candidates = tupleCandidates;
}
}
return candidates;
}
Aggregations