use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class AddReturnTypeFix method findMethod.
@Nullable
private static GrMethod findMethod(PsiFile file, final int offset) {
final PsiElement at = file.findElementAt(offset);
if (at == null)
return null;
if (at.getParent() instanceof GrReturnStatement) {
final GrReturnStatement returnStatement = ((GrReturnStatement) at.getParent());
final PsiElement word = returnStatement.getReturnWord();
if (!word.getTextRange().contains(offset))
return null;
final GroovyPsiElement returnOwner = PsiTreeUtil.getParentOfType(returnStatement, GrClosableBlock.class, GrMethod.class);
if (returnOwner instanceof GrMethod) {
final GrTypeElement returnTypeElement = ((GrMethod) returnOwner).getReturnTypeElementGroovy();
if (returnTypeElement == null) {
return (GrMethod) returnOwner;
}
}
return null;
}
final GrMethod method = PsiTreeUtil.getParentOfType(at, GrMethod.class, false, GrTypeDefinition.class, GrClosableBlock.class);
if (method != null && GrHighlightUtil.getMethodHeaderTextRange(method).contains(offset)) {
if (method.getReturnTypeElementGroovy() == null) {
return method;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrMethodBodyFixer method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
if (!(psiElement instanceof GrMethod))
return;
GrMethod method = (GrMethod) psiElement;
final PsiClass aClass = method.getContainingClass();
if (aClass != null && aClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT))
return;
final GrCodeBlock body = method.getBlock();
final Document doc = editor.getDocument();
if (body != null) {
// See IDEADEV-1093. This is quite hacky heuristic but it seem to be best we can do.
String bodyText = body.getText();
if (StringUtil.startsWithChar(bodyText, '{')) {
final GrStatement[] statements = body.getStatements();
if (statements.length > 0) {
// [todo]
// if (statements[0] instanceof PsiDeclarationStatement) {
// if (PsiTreeUtil.getDeepestLast(statements[0]) instanceof PsiErrorElement) {
// if (method.getContainingClass().getRBrace() == null) {
// doc.insertString(body.getTextRange().getStartOffset() + 1, "\n}");
// }
// }
// }
}
}
return;
}
int endOffset = method.getTextRange().getEndOffset();
if (StringUtil.endsWithChar(method.getText(), ';')) {
doc.deleteString(endOffset - 1, endOffset);
endOffset--;
}
doc.insertString(endOffset, "{\n}");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class AnnotatedMembersSearcher method execute.
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
final PsiClass annClass = p.getAnnotationClass();
assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";
final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return annClass.getQualifiedName();
}
});
assert annotationFQN != null;
final SearchScope scope = p.getScope();
final List<PsiModifierListOwner> candidates;
if (scope instanceof GlobalSearchScope) {
candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope) scope));
} else {
candidates = new ArrayList<>();
for (final PsiElement element : ((LocalSearchScope) scope).getScope()) {
ApplicationManager.getApplication().runReadAction(() -> {
if (element instanceof GroovyPsiElement) {
((GroovyPsiElement) element).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitMethod(@NotNull GrMethod method) {
candidates.add(method);
}
@Override
public void visitField(@NotNull GrField field) {
candidates.add(field);
}
});
}
});
}
}
for (final PsiModifierListOwner candidate : candidates) {
boolean accepted = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
if (AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
PsiModifierList list = candidate.getModifierList();
if (list != null) {
for (PsiAnnotation annotation : list.getAnnotations()) {
if ((p.isApproximate() || annotationFQN.equals(annotation.getQualifiedName())) && !consumer.process(candidate)) {
return false;
}
}
}
}
return true;
}
});
if (!accepted)
return false;
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processImplicitConstructorCall.
private static void processImplicitConstructorCall(@NotNull final PsiMember usage, final Processor<PsiReference> processor, final PsiMethod constructor) {
if (constructor instanceof GrMethod) {
GrParameter[] grParameters = (GrParameter[]) constructor.getParameterList().getParameters();
if (grParameters.length > 0 && !grParameters[0].isOptional())
return;
} else if (constructor.getParameterList().getParameters().length > 0)
return;
PsiManager manager = constructor.getManager();
if (manager.areElementsEquivalent(usage, constructor) || manager.areElementsEquivalent(constructor.getContainingClass(), usage.getContainingClass()))
return;
processor.process(new LightMemberReference(manager, usage, PsiSubstitutor.EMPTY) {
@Override
public PsiElement getElement() {
return usage;
}
@Override
public TextRange getRangeInElement() {
if (usage instanceof PsiClass) {
PsiIdentifier identifier = ((PsiClass) usage).getNameIdentifier();
if (identifier != null)
return TextRange.from(identifier.getStartOffsetInParent(), identifier.getTextLength());
} else if (usage instanceof PsiMethod) {
PsiIdentifier identifier = ((PsiMethod) usage).getNameIdentifier();
if (identifier != null)
return TextRange.from(identifier.getStartOffsetInParent(), identifier.getTextLength());
}
return super.getRangeInElement();
}
});
}
Aggregations