use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall in project intellij-community by JetBrains.
the class MissingReturnInspection method getExpectedClosureReturnType.
@Nullable
public static PsiType getExpectedClosureReturnType(GrClosableBlock closure) {
List<PsiType> expectedReturnTypes = new ArrayList<>();
PsiElement parent = closure.getParent();
if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall || parent instanceof GrMethodCall) {
GrMethodCall call = (GrMethodCall) (parent instanceof GrArgumentList ? parent.getParent() : parent);
GroovyResolveResult[] variants = call.getCallVariants(null);
for (GroovyResolveResult variant : variants) {
Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(variant, closure, true, true, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
if (map != null) {
Pair<PsiParameter, PsiType> pair = map.get(closure);
if (pair == null)
continue;
PsiParameter parameter = pair.getFirst();
PsiType type = parameter.getType();
if (TypesUtil.isPsiClassTypeToClosure(type)) {
PsiType[] parameters = ((PsiClassType) type).getParameters();
if (parameters.length == 1) {
expectedReturnTypes.add(parameters[0]);
}
}
}
}
} else {
for (PsiType expectedType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
if (TypesUtil.isPsiClassTypeToClosure(expectedType)) {
PsiType[] parameters = ((PsiClassType) expectedType).getParameters();
if (parameters.length == 1) {
expectedReturnTypes.add(parameters[0]);
}
}
}
}
for (PsiType type : expectedReturnTypes) {
if (PsiType.VOID.equals(type))
return PsiType.VOID;
}
return TypesUtil.getLeastUpperBoundNullable(expectedReturnTypes, closure.getManager());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall in project intellij-community by JetBrains.
the class UnnecessaryQualifiedReferenceInspection method isQualifiedStaticMethodWithUnnecessaryQualifier.
private static boolean isQualifiedStaticMethodWithUnnecessaryQualifier(GrReferenceExpression ref) {
if (ref.getQualifier() == null)
return false;
final PsiElement resolved = ref.resolve();
if (!(resolved instanceof PsiMember))
return false;
if (!((PsiMember) resolved).hasModifierProperty(PsiModifier.STATIC))
return false;
PsiElement copyResolved;
final PsiElement parent = ref.getParent();
if (parent instanceof GrMethodCall) {
final GrMethodCall copy = (GrMethodCall) parent.copy();
GrReferenceExpression invoked = (GrReferenceExpression) copy.getInvokedExpression();
assert invoked != null;
invoked.setQualifier(null);
copyResolved = ((GrReferenceExpression) copy.getInvokedExpression()).resolve();
} else {
final GrReferenceExpression copy = (GrReferenceExpression) ref.copy();
copy.setQualifier(null);
copyResolved = copy.resolve();
}
return ref.getManager().areElementsEquivalent(copyResolved, resolved);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall 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);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall in project intellij-community by JetBrains.
the class GdkMethodUtil method getTypeToMixIn.
@Nullable
private static Pair<PsiClassType, GrReferenceExpression> getTypeToMixIn(GrMethodCall methodCall) {
GrExpression invoked = methodCall.getInvokedExpression();
if (invoked instanceof GrReferenceExpression && GrImportUtil.acceptName((GrReferenceExpression) invoked, "mixin")) {
PsiElement resolved = ((GrReferenceExpression) invoked).resolve();
if (resolved instanceof PsiMethod && isMixinMethod((PsiMethod) resolved)) {
GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
Pair<PsiClassType, GrReferenceExpression> type = getPsiClassFromReference(qualifier);
if (type != null) {
return type;
}
if (qualifier == null) {
qualifier = PsiImplUtil.getRuntimeQualifier((GrReferenceExpression) invoked);
}
if (qualifier != null && isMetaClass(qualifier.getType())) {
if (qualifier instanceof GrMethodCall)
qualifier = ((GrMethodCall) qualifier).getInvokedExpression();
if (qualifier instanceof GrReferenceExpression) {
GrExpression qqualifier = ((GrReferenceExpression) qualifier).getQualifier();
if (qqualifier != null) {
Pair<PsiClassType, GrReferenceExpression> type1 = getPsiClassFromReference(qqualifier);
if (type1 != null) {
return type1;
}
} else {
PsiType qtype = PsiImplUtil.getQualifierType((GrReferenceExpression) qualifier);
if (qtype instanceof PsiClassType && ((PsiClassType) qtype).resolve() != null) {
return Pair.create((PsiClassType) qtype, (GrReferenceExpression) qualifier);
}
}
}
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall in project intellij-community by JetBrains.
the class ResolveUtil method getSelfOrWithQualifier.
@Nullable
public static GrExpression getSelfOrWithQualifier(GrReferenceExpression ref) {
final GrExpression qualifier = ref.getQualifierExpression();
if (qualifier != null) {
return qualifier;
}
PsiElement place = ref;
while (true) {
final GrClosableBlock closure = PsiTreeUtil.getParentOfType(place, GrClosableBlock.class, true, GrMember.class, GroovyFile.class);
if (closure == null)
break;
place = closure;
PsiElement clParent = closure.getParent();
if (clParent instanceof GrArgumentList)
clParent = clParent.getParent();
if (!(clParent instanceof GrMethodCall))
continue;
final GrExpression expression = ((GrMethodCall) clParent).getInvokedExpression();
if (expression instanceof GrReferenceExpression && GdkMethodUtil.isWithName(((GrReferenceExpression) expression).getReferenceName()) && ((GrReferenceExpression) expression).resolve() instanceof GrGdkMethod) {
final GrExpression withQualifier = ((GrReferenceExpression) expression).getQualifierExpression();
if (withQualifier != null) {
return withQualifier;
}
}
}
return null;
}
Aggregations