use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GdkMethodUtil method getClosureMixins.
private static Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>> getClosureMixins(final GrStatement statement) {
if (!(statement instanceof GrAssignmentExpression))
return null;
final GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
return CachedValuesManager.getCachedValue(statement, new CachedValueProvider<Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>>>() {
@Nullable
@Override
public Result<Trinity<PsiClassType, GrReferenceExpression, List<GrMethod>>> compute() {
Pair<PsiClassType, GrReferenceExpression> original = getTypeToMixIn(assignment);
if (original == null)
return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
final Pair<GrSignature, String> signatures = getTypeToMix(assignment);
if (signatures == null)
return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
final String name = signatures.second;
final List<GrMethod> methods = ContainerUtil.newArrayList();
final PsiClass closure = JavaPsiFacade.getInstance(statement.getProject()).findClass(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, statement.getResolveScope());
if (closure == null)
return Result.create(null, PsiModificationTracker.MODIFICATION_COUNT);
signatures.first.accept(new GrSignatureVisitor() {
@Override
public void visitClosureSignature(GrClosureSignature signature) {
super.visitClosureSignature(signature);
GrMethod method = createMethod(signature, name, assignment, closure);
methods.add(method);
}
});
return Result.create(Trinity.create(original.first, original.second, methods), PsiModificationTracker.MODIFICATION_COUNT);
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class PsiUtil method isApplicableConcrete.
public static GrClosureSignatureUtil.ApplicabilityResult isApplicableConcrete(@Nullable PsiType[] argumentTypes, PsiMethod method, PsiSubstitutor substitutor, PsiElement place, final boolean eraseParameterTypes) {
if (argumentTypes == null)
return GrClosureSignatureUtil.ApplicabilityResult.canBeApplicable;
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor, eraseParameterTypes);
//check for default constructor
if (method.isConstructor()) {
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length == 0 && argumentTypes.length == 1) {
return InheritanceUtil.isInheritor(argumentTypes[0], CommonClassNames.JAVA_UTIL_MAP) ? GrClosureSignatureUtil.ApplicabilityResult.applicable : GrClosureSignatureUtil.ApplicabilityResult.inapplicable;
}
if (parameters.length == 1 && argumentTypes.length == 0 && InheritanceUtil.isInheritor(parameters[0].getType(), CommonClassNames.JAVA_UTIL_MAP)) {
return GrClosureSignatureUtil.ApplicabilityResult.inapplicable;
}
}
LOG.assertTrue(signature != null);
GrClosureSignatureUtil.ApplicabilityResult result = GrClosureSignatureUtil.isSignatureApplicableConcrete(signature, argumentTypes, place);
if (result != GrClosureSignatureUtil.ApplicabilityResult.inapplicable) {
return result;
}
if (method instanceof GrBuilderMethod && !((GrBuilderMethod) method).hasObligatoryNamedArguments()) {
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length > 0 && parameters[0].getType() instanceof GrMapType && (argumentTypes.length == 0 || !(argumentTypes[0] instanceof GrMapType))) {
return GrClosureSignatureUtil.isSignatureApplicableConcrete(GrClosureSignatureUtil.removeParam(signature, 0), argumentTypes, place);
}
}
return GrClosureSignatureUtil.ApplicabilityResult.inapplicable;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class SubstitutorComputer method inferMethodTypeParameters.
private PsiSubstitutor inferMethodTypeParameters(@NotNull PsiMethod method, @NotNull PsiSubstitutor partialSubstitutor, @NotNull PsiTypeParameter[] typeParameters, @NotNull PsiType[] argTypes) {
if (typeParameters.length == 0 || myArgumentTypes == null)
return partialSubstitutor;
final GrClosureSignature erasedSignature = GrClosureSignatureUtil.createSignature(method, partialSubstitutor, true);
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, partialSubstitutor);
final GrClosureParameter[] params = signature.getParameters();
final GrClosureSignatureUtil.ArgInfo<PsiType>[] argInfos = GrClosureSignatureUtil.mapArgTypesToParameters(erasedSignature, argTypes, myPlace, true);
if (argInfos == null)
return partialSubstitutor;
int max = Math.max(params.length, argTypes.length);
PsiType[] parameterTypes = PsiType.createArray(max);
PsiType[] argumentTypes = PsiType.createArray(max);
int i = 0;
for (int paramIndex = 0; paramIndex < argInfos.length; paramIndex++) {
PsiType paramType = params[paramIndex].getType();
GrClosureSignatureUtil.ArgInfo<PsiType> argInfo = argInfos[paramIndex];
if (argInfo != null) {
if (argInfo.isMultiArg) {
if (paramType instanceof PsiArrayType)
paramType = ((PsiArrayType) paramType).getComponentType();
}
for (PsiType type : argInfo.args) {
argumentTypes[i] = handleConversion(paramType, type);
parameterTypes[i] = paramType;
i++;
}
} else {
parameterTypes[i] = paramType;
argumentTypes[i] = PsiType.NULL;
i++;
}
}
PsiSubstitutor substitutor = myHelper.inferTypeArguments(typeParameters, parameterTypes, argumentTypes, LanguageLevel.JDK_1_7);
for (PsiTypeParameter typeParameter : typeParameters) {
if (!substitutor.getSubstitutionMap().containsKey(typeParameter)) {
substitutor = inferFromContext(typeParameter, PsiUtil.getSmartReturnType(method), substitutor);
if (!substitutor.getSubstitutionMap().containsKey(typeParameter)) {
substitutor = substitutor.put(typeParameter, null);
}
}
}
return partialSubstitutor.putAll(substitutor);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class GrMethodConflictUtil method checkForClosurePropertySignatureOverload.
private static void checkForClosurePropertySignatureOverload(PsiClass clazz, GrMethod prototype, final GrMethod refactoredMethod, final MultiMap<PsiElement, String> conflicts, final List<MethodSignature> prototypeSignatures) {
final boolean isStatic = prototype.hasModifierProperty(PsiModifier.STATIC);
final String name = prototype.getName();
if (!GroovyPropertyUtils.isProperty(clazz, name, isStatic))
return;
final PsiMethod getter = GroovyPropertyUtils.findPropertyGetter(clazz, name, isStatic, true);
final PsiType returnType;
if (getter instanceof GrMethod) {
returnType = ((GrMethod) getter).getInferredReturnType();
} else if (getter instanceof GrAccessorMethod) {
returnType = ((GrAccessorMethod) getter).getInferredReturnType();
} else {
return;
}
if (!(returnType instanceof GrClosureType))
return;
final GrSignature signature = ((GrClosureType) returnType).getSignature();
signature.accept(new GrRecursiveSignatureVisitor() {
@Override
public void visitClosureSignature(GrClosureSignature signature) {
NextSignature: for (MethodSignature prototypeSignature : prototypeSignatures) {
final GrClosureParameter[] params = signature.getParameters();
final PsiType[] types = prototypeSignature.getParameterTypes();
if (types.length != params.length)
continue;
for (int i = 0; i < types.length; i++) {
if (!TypesUtil.isAssignableByMethodCallConversion(types[i], params[i].getType(), refactoredMethod.getParameterList())) {
continue NextSignature;
}
}
conflicts.putValue(getter, GroovyRefactoringBundle.message("refactored.method.will.cover.closure.property", name, RefactoringUIUtil.getDescription(getter.getContainingClass(), false)));
}
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.
the class DefaultDelegatesToProvider method getDelegatesToInfo.
@Nullable
public DelegatesToInfo getDelegatesToInfo(@NotNull final GrClosableBlock closableBlock) {
GrCall call = getContainingCall(closableBlock);
if (call == null)
return null;
final GroovyResolveResult result = resolveActualCall(call);
final PsiElement element = result.getElement();
if (GdkMethodUtil.isWithOrIdentity(element)) {
final GrExpression qualifier = inferCallQualifier((GrMethodCall) call);
if (qualifier == null)
return null;
return new DelegatesToInfo(qualifier.getType(), Closure.DELEGATE_FIRST);
}
final GrClosureSignature signature = inferSignature(element);
if (signature == null)
return null;
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] map = mapArgs(closableBlock, call, signature);
if (map == null)
return null;
if (!(element instanceof PsiMethod))
return null;
final PsiMethod method = (PsiMethod) element;
final PsiParameterList parameterList = method.getParameterList();
final PsiParameter parameter = findParameter(parameterList, closableBlock, map);
if (parameter == null)
return null;
final String delegateFqnData = parameter.getUserData(DELEGATES_TO_KEY);
final Integer strategyData = parameter.getUserData(DELEGATES_TO_STRATEGY_KEY);
if (delegateFqnData != null) {
return new DelegatesToInfo(TypesUtil.createType(delegateFqnData, closableBlock), strategyData == null ? Closure.OWNER_FIRST : strategyData);
}
final PsiModifierList modifierList = parameter.getModifierList();
if (modifierList == null)
return null;
final PsiAnnotation delegatesTo = modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO);
if (delegatesTo == null)
return null;
PsiType delegateType = getFromValue(delegatesTo);
if (delegateType == null)
delegateType = getFromTarget(parameterList, delegatesTo, signature, map);
if (delegateType == null)
delegateType = getFromType(result, delegatesTo);
final int strategyValue = getStrategyValue(delegatesTo.findAttributeValue("strategy"));
return new DelegatesToInfo(delegateType, strategyValue);
}
Aggregations