use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.
the class GppClosureParameterTypeProvider method getClosureParameterType.
@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
final PsiElement parent = closure.getParent();
if (parent instanceof GrNamedArgument) {
final Pair<PsiMethod, PsiSubstitutor> pair = getOverriddenMethod((GrNamedArgument) parent);
if (pair != null) {
final PsiParameter[] parameters = pair.first.getParameterList().getParameters();
if (parameters.length > index) {
return pair.second.substitute(parameters[index].getType());
}
return null;
}
}
if (parent instanceof GrListOrMap) {
final GrListOrMap list = (GrListOrMap) parent;
if (!list.isMap()) {
final PsiType listType = list.getType();
final int argIndex = Arrays.asList(list.getInitializers()).indexOf(closure);
assert argIndex >= 0;
if (listType instanceof GrTupleType) {
for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
if (!(type instanceof PsiClassType))
continue;
final GroovyResolveResult[] candidates = PsiUtil.getConstructorCandidates((PsiClassType) type, ((GrTupleType) listType).getComponentTypes(), closure);
for (GroovyResolveResult resolveResult : candidates) {
final PsiElement method = resolveResult.getElement();
if (!(method instanceof PsiMethod) || !((PsiMethod) method).isConstructor())
continue;
final PsiParameter[] parameters = ((PsiMethod) method).getParameterList().getParameters();
if (parameters.length <= argIndex)
continue;
final PsiType toCastTo = resolveResult.getSubstitutor().substitute(parameters[argIndex].getType());
final PsiType suggestion = getSingleMethodParameterType(toCastTo, index, closure);
if (suggestion != null)
return suggestion;
}
}
}
return null;
}
}
for (PsiType constraint : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
final PsiType suggestion = getSingleMethodParameterType(constraint, index, closure);
if (suggestion != null) {
return suggestion;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.
the class TypeInferenceHelper method getInitializerTypeFor.
@Nullable
public static PsiType getInitializerTypeFor(PsiElement element) {
final PsiElement parent = skipParentheses(element.getParent(), true);
if (parent instanceof GrAssignmentExpression) {
if (element instanceof GrIndexProperty) {
final GrExpression rvalue = ((GrAssignmentExpression) parent).getRValue();
//don't try to infer assignment type in case of index property because of infinite recursion (example: a[2]+=4)
return rvalue != null ? rvalue.getType() : null;
}
return ((GrAssignmentExpression) parent).getType();
}
if (parent instanceof GrTupleExpression) {
GrTupleExpression list = (GrTupleExpression) parent;
if (list.getParent() instanceof GrAssignmentExpression) {
// multiple assignment
final GrExpression rValue = ((GrAssignmentExpression) list.getParent()).getRValue();
int idx = list.indexOf(element);
if (idx >= 0 && rValue != null) {
PsiType rType = rValue.getType();
if (rType instanceof GrTupleType) {
PsiType[] componentTypes = ((GrTupleType) rType).getComponentTypes();
if (idx < componentTypes.length)
return componentTypes[idx];
return null;
}
return PsiUtil.extractIterableTypeParameter(rType, false);
}
}
}
if (parent instanceof GrUnaryExpression && TokenSets.POSTFIX_UNARY_OP_SET.contains(((GrUnaryExpression) parent).getOperationTokenType())) {
return ((GrUnaryExpression) parent).getType();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType 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;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.
the class ClosureParameterEnhancer method findTypeForIteration.
@Contract("null,_ -> null")
@Nullable
public static PsiType findTypeForIteration(@Nullable PsiType type, @NotNull PsiElement context) {
final PsiManager manager = context.getManager();
final GlobalSearchScope resolveScope = context.getResolveScope();
if (type instanceof PsiArrayType) {
return TypesUtil.boxPrimitiveType(((PsiArrayType) type).getComponentType(), manager, resolveScope);
}
if (type instanceof GrTupleType) {
PsiType[] types = ((GrTupleType) type).getParameters();
return types.length == 1 ? types[0] : null;
}
if (type instanceof GrRangeType) {
return ((GrRangeType) type).getIterationType();
}
PsiType fromIterator = findTypeFromIteratorMethod(type, context);
if (fromIterator != null) {
return fromIterator;
}
PsiType extracted = PsiUtil.extractIterableTypeParameter(type, true);
if (extracted != null) {
return extracted;
}
if (TypesUtil.isClassType(type, JAVA_LANG_STRING) || TypesUtil.isClassType(type, JAVA_IO_FILE)) {
return PsiType.getJavaLangString(manager, resolveScope);
}
if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
return getEntryForMap(type, manager.getProject(), resolveScope);
}
return type;
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType in project intellij-community by JetBrains.
the class GdkMethodUtil method categoryIteration.
public static boolean categoryIteration(GrClosableBlock place, final PsiScopeProcessor processor, ResolveState state) {
if (!ResolveUtil.shouldProcessMethods(processor.getHint(ElementClassHint.KEY)) && !(processor instanceof GroovyResolverProcessor && ((GroovyResolverProcessor) processor).isPropertyResolve())) {
return true;
}
final GrMethodCall call = checkMethodCall(place, USE);
if (call == null)
return true;
final GrClosableBlock[] closures = call.getClosureArguments();
GrExpression[] args = call.getExpressionArguments();
if (!(placeEqualsSingleClosureArg(place, closures) || placeEqualsLastArg(place, args)))
return true;
if (!(call.resolveMethod() instanceof GrGdkMethod))
return true;
state = state.put(ClassHint.RESOLVE_CONTEXT, call);
if ((args.length == 1 || args.length == 2 && placeEqualsLastArg(place, args))) {
PsiType type = args[0].getType();
if (type instanceof GrTupleType) {
return processTypesFromTuple((GrTupleType) type, processor, state, place);
}
}
return processTypesFomArgs(args, processor, state, place);
}
Aggregations