use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class EachWithIndexClosureCompleter method getParameterInfos.
@Nullable
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
final String name = method.getName();
if (!"eachWithIndex".equals(name))
return null;
if (method instanceof GrGdkMethod)
method = ((GrGdkMethod) method).getStaticMethod();
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null)
return null;
final String qname = containingClass.getQualifiedName();
if (!GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname))
return null;
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length != 2)
return null;
final PsiType type = parameters[0].getType();
final PsiType collection = substitutor.substitute(type);
final PsiType iterable = getIteratedType(place, collection);
if (iterable != null) {
return Arrays.asList(new ClosureParameterInfo(iterable.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
if (InheritanceUtil.isInheritor(collection, CommonClassNames.JAVA_UTIL_MAP)) {
final PsiType[] typeParams = ((PsiClassType) collection).getParameters();
final Project project = context.getProject();
final PsiClass entry = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_UTIL_MAP_ENTRY, place.getResolveScope());
if (entry == null)
return null;
final PsiClassType entryType = JavaPsiFacade.getElementFactory(project).createType(entry, typeParams);
return Arrays.asList(new ClosureParameterInfo(entryType.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
return Arrays.asList(new ClosureParameterInfo(collection.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class GrMainCompletionProvider method assignPriority.
private static int assignPriority(LookupElement lookupElement, PsiType qualifierType) {
Object object = lookupElement.getObject();
PsiSubstitutor substitutor = null;
GroovyResolveResult resolveResult = null;
if (object instanceof GroovyResolveResult) {
resolveResult = (GroovyResolveResult) object;
substitutor = resolveResult.getSubstitutor();
object = ((GroovyResolveResult) object).getElement();
}
// default groovy methods
if (object instanceof GrGdkMethod && GroovyCompletionUtil.skipDefGroovyMethod((GrGdkMethod) object, substitutor, qualifierType)) {
return -1;
}
// operator methods
if (object instanceof PsiMethod && PsiUtil.OPERATOR_METHOD_NAMES.contains(((PsiMethod) object).getName()) && !checkForIterator((PsiMethod) object)) {
return -3;
}
// accessors if there is no get, set, is prefix
if (object instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod) object)) {
return -1;
}
// inaccessible elements
if (resolveResult != null && !resolveResult.isAccessible()) {
return -2;
}
return 0;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method checkMethodApplicability.
private <T extends GroovyPsiElement> boolean checkMethodApplicability(@NotNull final GroovyResolveResult methodResolveResult, boolean checkUnknownArgs, @NotNull final CallInfo<T> info) {
final PsiElement element = methodResolveResult.getElement();
if (!(element instanceof PsiMethod))
return true;
if (element instanceof GrBuilderMethod)
return true;
final PsiMethod method = (PsiMethod) element;
if ("call".equals(method.getName()) && info.getInvokedExpression() instanceof GrReferenceExpression) {
final GrExpression qualifierExpression = ((GrReferenceExpression) info.getInvokedExpression()).getQualifierExpression();
if (qualifierExpression != null) {
final PsiType type = qualifierExpression.getType();
if (type instanceof GrClosureType) {
GrClosureSignatureUtil.ApplicabilityResult result = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), (GrClosureType) type, info.getInvokedExpression());
switch(result) {
case inapplicable:
highlightInapplicableMethodUsage(methodResolveResult, info, method);
return false;
case //q(1,2)
canBeApplicable:
if (checkUnknownArgs) {
highlightUnknownArgs(info);
}
return !checkUnknownArgs;
default:
return true;
}
}
}
}
if (method instanceof GrGdkMethod && info.getInvokedExpression() instanceof GrReferenceExpression) {
final GrReferenceExpression invoked = (GrReferenceExpression) info.getInvokedExpression();
final GrExpression qualifier = PsiImplUtil.getRuntimeQualifier(invoked);
if (qualifier == null && method.getName().equals("call")) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(invoked.getProject());
final GrReferenceExpression callRef = factory.createReferenceExpressionFromText("qualifier.call", invoked);
callRef.setQualifier(invoked);
return checkMethodApplicability(methodResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {
@Nullable
@Override
public GrExpression getInvokedExpression() {
return callRef;
}
@NotNull
@Override
public GroovyResolveResult advancedResolve() {
return methodResolveResult;
}
@NotNull
@Override
public GroovyResolveResult[] multiResolve() {
return new GroovyResolveResult[] { methodResolveResult };
}
@Nullable
@Override
public PsiType getQualifierInstanceType() {
return info.getInvokedExpression().getType();
}
});
}
final PsiMethod staticMethod = ((GrGdkMethod) method).getStaticMethod();
PsiType qualifierType = info.getQualifierInstanceType();
if (method.hasModifierProperty(PsiModifier.STATIC)) {
qualifierType = ResolveUtil.unwrapClassType(qualifierType);
}
//check methods processed by @Category(ClassWhichProcessMethod) annotation
if (qualifierType != null && !GdkMethodUtil.isCategoryMethod(staticMethod, qualifierType, qualifier, methodResolveResult.getSubstitutor()) && !checkCategoryQualifier(invoked, qualifier, staticMethod, methodResolveResult.getSubstitutor())) {
registerError(info.getHighlightElementForCategoryQualifier(), ProblemHighlightType.GENERIC_ERROR, GroovyInspectionBundle.message("category.method.0.cannot.be.applied.to.1", method.getName(), qualifierType.getCanonicalText()));
return false;
}
}
if (info.getArgumentTypes() == null)
return true;
GrClosureSignatureUtil.ApplicabilityResult applicable = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), method, methodResolveResult.getSubstitutor(), info.getCall(), false);
switch(applicable) {
case inapplicable:
highlightInapplicableMethodUsage(methodResolveResult, info, method);
return false;
case canBeApplicable:
if (checkUnknownArgs) {
highlightUnknownArgs(info);
}
return !checkUnknownArgs;
default:
return true;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class GdkMethodHolder method processMethods.
public boolean processMethods(PsiScopeProcessor processor, @NotNull ResolveState state, PsiType qualifierType, Project project) {
if (qualifierType == null)
return true;
NameHint nameHint = processor.getHint(NameHint.KEY);
String name = nameHint == null ? null : nameHint.getName(state);
final MultiMap<String, PsiMethod> map = name != null ? myOriginalMethodsByNameAndType.get(name) : myOriginalMethodByType.getValue();
if (map.isEmpty()) {
return true;
}
for (String superType : ResolveUtil.getAllSuperTypes(qualifierType, project)) {
for (PsiMethod method : map.get(superType)) {
String info = GdkMethodUtil.generateOriginInfo(method);
GrGdkMethod gdk = GrGdkMethodImpl.createGdkMethod(method, myStatic, info);
if (!processor.execute(gdk, state)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method highlightInapplicableMethodUsage.
private void highlightInapplicableMethodUsage(@NotNull GroovyResolveResult methodResolveResult, @NotNull CallInfo info, @NotNull PsiMethod method) {
final PsiClass containingClass = method instanceof GrGdkMethod ? ((GrGdkMethod) method).getStaticMethod().getContainingClass() : method.getContainingClass();
PsiType[] argumentTypes = info.getArgumentTypes();
if (containingClass == null) {
registerCannotApplyError(method.getName(), info);
return;
}
final String typesString = buildArgTypesList(argumentTypes);
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(method.getProject());
final PsiClassType containingType = factory.createType(containingClass, methodResolveResult.getSubstitutor());
final String canonicalText = containingType.getInternalCanonicalText();
String message = method.isConstructor() ? GroovyBundle.message("cannot.apply.constructor", method.getName(), canonicalText, typesString) : GroovyBundle.message("cannot.apply.method1", method.getName(), canonicalText, typesString);
registerError(info.getElementToHighlight(), message, genCastFixes(GrClosureSignatureUtil.createSignature(methodResolveResult), argumentTypes, info.getArgumentList()), ProblemHighlightType.GENERIC_ERROR);
}
Aggregations