use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.
the class RenameGrFieldProcessor method findCollisions.
@Override
public void findCollisions(PsiElement element, String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) {
List<UsageInfo> collisions = new ArrayList<>();
for (UsageInfo info : result) {
if (!(info instanceof MoveRenameUsageInfo))
continue;
final PsiElement infoElement = info.getElement();
final PsiElement referencedElement = ((MoveRenameUsageInfo) info).getReferencedElement();
if (!(infoElement instanceof GrReferenceExpression))
continue;
final GrReferenceExpression refExpr = (GrReferenceExpression) infoElement;
if (!(referencedElement instanceof GrField || refExpr.advancedResolve().isInvokedOnProperty()))
continue;
if (!(refExpr.getParent() instanceof GrCall))
continue;
final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
final PsiType[] typeArguments = refExpr.getTypeArguments();
final MethodResolverProcessor processor = new MethodResolverProcessor(newName, refExpr, false, null, argTypes, typeArguments);
final PsiMethod resolved = ResolveUtil.resolveExistingElement(refExpr, processor, PsiMethod.class);
if (resolved == null)
continue;
collisions.add(new UnresolvableCollisionUsageInfo(resolved, refExpr) {
@Override
public String getDescription() {
return GroovyRefactoringBundle.message("usage.will.be.overriden.by.method", refExpr.getParent().getText(), PsiFormatUtil.formatMethod(resolved, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME, PsiFormatUtilBase.SHOW_TYPE));
}
});
}
result.addAll(collisions);
super.findCollisions(element, newName, allRenames, result);
}
use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.
the class GenerationUtil method suggestMethodName.
public static String suggestMethodName(GroovyPsiElement place, String initialName, ExpressionContext context) {
int count = 0;
String name = initialName;
Class[] classes = { PsiMethod.class };
final Map<PsiMethod, String> setters = context.getSetters();
while (setters.containsValue(name) || ResolveUtil.resolveExistingElement(place, new MethodResolverProcessor(name, place, false, null, null, null, true), classes) != null) {
name = initialName + count;
count++;
}
return name;
}
use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.
the class GDKSuperMethodSearcher method execute.
@Override
public boolean execute(@NotNull SuperMethodsSearch.SearchParameters queryParameters, @NotNull Processor<MethodSignatureBackedByPsiMethod> consumer) {
final PsiMethod method = queryParameters.getMethod();
if (!(method instanceof GrMethod)) {
return true;
}
if (method.hasModifierProperty(PsiModifier.STATIC))
return true;
final PsiClass psiClass = method.getContainingClass();
if (psiClass == null)
return true;
final HierarchicalMethodSignature hierarchicalSignature = method.getHierarchicalMethodSignature();
if (!hierarchicalSignature.getSuperSignatures().isEmpty())
return true;
final Project project = method.getProject();
final String name = method.getName();
final MethodResolverProcessor processor = new MethodResolverProcessor(name, ((GrMethod) method), false, null, null, PsiType.EMPTY_ARRAY);
ResolveUtil.processNonCodeMembers(JavaPsiFacade.getElementFactory(project).createType(psiClass), processor, (GrMethod) method, ResolveState.initial());
final GroovyResolveResult[] candidates = processor.getCandidates();
final PsiManager psiManager = PsiManager.getInstance(project);
final MethodSignature signature = method.getHierarchicalMethodSignature();
List<PsiMethod> goodSupers = new ArrayList<>();
for (GroovyResolveResult candidate : candidates) {
final PsiElement element = candidate.getElement();
if (element instanceof PsiMethod) {
final PsiMethod m = (PsiMethod) element;
if (!isTheSameMethod(method, psiManager, m) && PsiImplUtil.isExtendsSignature(m.getHierarchicalMethodSignature(), signature)) {
goodSupers.add(m);
}
}
}
if (goodSupers.isEmpty())
return true;
List<PsiMethod> result = new ArrayList<>(goodSupers.size());
result.add(goodSupers.get(0));
final Comparator<PsiMethod> comparator = (o1, o2) -> {
//compare by first parameter type
final PsiType type1 = getRealType(o1);
final PsiType type2 = getRealType(o2);
if (TypesUtil.isAssignableByMethodCallConversion(type1, type2, o1)) {
return -1;
} else if (TypesUtil.isAssignableByMethodCallConversion(type2, type1, o1)) {
return 1;
}
return 0;
};
Outer: for (PsiMethod current : goodSupers) {
for (Iterator<PsiMethod> i = result.iterator(); i.hasNext(); ) {
PsiMethod m = i.next();
final int res = comparator.compare(m, current);
if (res > 0) {
continue Outer;
} else if (res < 0) {
i.remove();
}
}
result.add(current);
}
for (PsiMethod psiMethod : result) {
if (!consumer.process(getRealMethod(psiMethod).getHierarchicalMethodSignature())) {
return false;
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.
the class GrConstructorInvocationImpl method multiResolve.
@Override
@NotNull
public GroovyResolveResult[] multiResolve(boolean incompleteCode) {
PsiClass clazz = getDelegatedClass();
if (clazz != null) {
PsiType[] argTypes = PsiUtil.getArgumentTypes(getFirstChild(), false);
PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
PsiSubstitutor substitutor;
if (isThisCall()) {
substitutor = PsiSubstitutor.EMPTY;
} else {
PsiClass enclosing = PsiUtil.getContextClass(this);
assert enclosing != null;
substitutor = TypeConversionUtil.getSuperClassSubstitutor(clazz, enclosing, PsiSubstitutor.EMPTY);
}
PsiType thisType = factory.createType(clazz, substitutor);
MethodResolverProcessor processor = new MethodResolverProcessor(clazz.getName(), this, true, thisType, argTypes, PsiType.EMPTY_ARRAY, incompleteCode);
final ResolveState state = ResolveState.initial().put(PsiSubstitutor.KEY, substitutor);
clazz.processDeclarations(processor, state, null, this);
ResolveUtil.processNonCodeMembers(thisType, processor, getInvokedExpression(), state);
return processor.getCandidates();
}
return GroovyResolveResult.EMPTY_ARRAY;
}
use of org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor in project intellij-community by JetBrains.
the class GrDocFieldReferenceImpl method multiResolveImpl.
@Override
protected ResolveResult[] multiResolveImpl() {
String name = getReferenceName();
GrDocReferenceElement holder = getReferenceHolder();
PsiElement resolved;
if (holder != null) {
GrCodeReferenceElement referenceElement = holder.getReferenceElement();
resolved = referenceElement != null ? referenceElement.resolve() : null;
} else {
resolved = PsiUtil.getContextClass(GrDocCommentUtil.findDocOwner(this));
}
if (resolved instanceof PsiClass) {
PropertyResolverProcessor processor = new PropertyResolverProcessor(name, this);
resolved.processDeclarations(processor, ResolveState.initial(), resolved, this);
GroovyResolveResult[] candidates = processor.getCandidates();
if (candidates.length == 0) {
PsiType thisType = JavaPsiFacade.getInstance(getProject()).getElementFactory().createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
MethodResolverProcessor methodProcessor = new MethodResolverProcessor(name, this, false, thisType, null, PsiType.EMPTY_ARRAY);
MethodResolverProcessor constructorProcessor = new MethodResolverProcessor(name, this, true, thisType, null, PsiType.EMPTY_ARRAY);
resolved.processDeclarations(methodProcessor, ResolveState.initial(), resolved, this);
resolved.processDeclarations(constructorProcessor, ResolveState.initial(), resolved, this);
candidates = ArrayUtil.mergeArrays(methodProcessor.getCandidates(), constructorProcessor.getCandidates());
if (candidates.length > 0) {
candidates = new GroovyResolveResult[] { candidates[0] };
}
}
return candidates;
}
return ResolveResult.EMPTY_ARRAY;
}
Aggregations