use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class AccessorMethodReferencesSearcher method processQuery.
@Override
public void processQuery(@NotNull MethodReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
final PsiMethod method = queryParameters.getMethod();
final String propertyName;
if (GdkMethodUtil.isCategoryMethod(method, null, null, PsiSubstitutor.EMPTY)) {
final GrGdkMethod cat = GrGdkMethodImpl.createGdkMethod(method, false, null);
propertyName = GroovyPropertyUtils.getPropertyName((PsiMethod) cat);
} else {
propertyName = GroovyPropertyUtils.getPropertyName(method);
}
if (propertyName == null)
return;
final SearchScope onlyGroovyFiles = GroovyScopeUtil.restrictScopeToGroovyFiles(queryParameters.getEffectiveSearchScope(), GroovyScopeUtil.getEffectiveScope(method));
queryParameters.getOptimizer().searchWord(propertyName, onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);
if (!GroovyPropertyUtils.isPropertyName(propertyName)) {
queryParameters.getOptimizer().searchWord(StringUtil.decapitalize(propertyName), onlyGroovyFiles, UsageSearchContext.IN_CODE, true, method);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class GroovyTargetElementEvaluator method getElementByReference.
@Override
public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) {
PsiElement sourceElement = ref.getElement();
if (sourceElement instanceof GrCodeReferenceElement) {
GrNewExpression newExpr;
if (sourceElement.getParent() instanceof GrNewExpression) {
newExpr = (GrNewExpression) sourceElement.getParent();
} else if (sourceElement.getParent().getParent() instanceof GrNewExpression) {
//anonymous class declaration
newExpr = (GrNewExpression) sourceElement.getParent().getParent();
} else {
return null;
}
final PsiMethod constructor = newExpr.resolveMethod();
final GrArgumentList argumentList = newExpr.getArgumentList();
if (constructor != null && argumentList != null && PsiImplUtil.hasNamedArguments(argumentList) && !PsiImplUtil.hasExpressionArguments(argumentList)) {
if (constructor.getParameterList().getParametersCount() == 0)
return constructor.getContainingClass();
}
return constructor;
}
if (sourceElement instanceof GrReferenceExpression) {
PsiElement resolved = ((GrReferenceExpression) sourceElement).resolve();
if (resolved instanceof GrGdkMethod || !(resolved instanceof GrRenameableLightElement)) {
return correctSearchTargets(resolved);
}
return resolved;
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class PsiImplUtil method getRuntimeQualifier.
@Nullable
public static GrExpression getRuntimeQualifier(@NotNull GrReferenceExpression refExpr) {
GrExpression qualifier = refExpr.getQualifierExpression();
if (qualifier != null)
return qualifier;
for (GrClosableBlock closure = PsiTreeUtil.getParentOfType(refExpr, GrClosableBlock.class); closure != null; closure = PsiTreeUtil.getParentOfType(closure, GrClosableBlock.class)) {
PsiElement parent = closure.getParent();
if (parent instanceof GrArgumentList)
parent = parent.getParent();
if (!(parent instanceof GrMethodCall))
continue;
GrExpression funExpr = ((GrMethodCall) parent).getInvokedExpression();
if (!(funExpr instanceof GrReferenceExpression))
return funExpr;
final PsiElement resolved = ((GrReferenceExpression) funExpr).resolve();
if (!(resolved instanceof PsiMethod))
return funExpr;
if (resolved instanceof GrGdkMethod && isFromDGM((GrGdkMethod) resolved) && !GdkMethodUtil.isWithName(((GrGdkMethod) resolved).getStaticMethod().getName())) {
continue;
}
qualifier = ((GrReferenceExpression) funExpr).getQualifierExpression();
if (qualifier != null)
return qualifier;
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method findConflicts.
private static boolean findConflicts(InitialInfo info) {
//new ConflictsDialog()
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final PsiElement declarationOwner = info.getContext().getParent();
GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
}
}
//skip 'print' and 'println'
private boolean skipResult(GroovyResolveResult result) {
PsiElement element = result.getElement();
if (element instanceof PsiMethod) {
String name = ((PsiMethod) element).getName();
if (!name.startsWith("print"))
return false;
if (element instanceof GrGdkMethod)
element = ((GrGdkMethod) element).getStaticMethod();
PsiClass aClass = ((PsiMethod) element).getContainingClass();
if (aClass != null) {
String qname = aClass.getQualifiedName();
return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
}
}
return false;
}
};
GrStatement[] statements = info.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
if (conflicts.isEmpty())
return false;
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
}
ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
dialog.show();
return !dialog.isOK();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod in project intellij-community by JetBrains.
the class ExpressionGenerator method invokeMethodOn.
public void invokeMethodOn(@NotNull PsiMethod method, @Nullable GrExpression caller, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closures, @NotNull PsiSubstitutor substitutor, @NotNull GroovyPsiElement context) {
if (method instanceof GrGdkMethod) {
if (CustomMethodInvocator.invokeMethodOn(this, (GrGdkMethod) method, caller, exprs, namedArgs, closures, substitutor, context))
return;
GrExpression[] newArgs = new GrExpression[exprs.length + 1];
System.arraycopy(exprs, 0, newArgs, 1, exprs.length);
if (method.hasModifierProperty(PsiModifier.STATIC)) {
newArgs[0] = factory.createExpressionFromText("null");
} else {
if (caller == null) {
caller = factory.createExpressionFromText("this", context);
}
newArgs[0] = caller;
}
invokeMethodOn(((GrGdkMethod) method).getStaticMethod(), null, newArgs, namedArgs, closures, substitutor, context);
return;
}
if (method.hasModifierProperty(PsiModifier.STATIC) && caller == null) {
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && !PsiTreeUtil.isAncestor(containingClass, context, true)) {
builder.append(containingClass.getQualifiedName()).append('.');
}
} else {
if (caller != null) {
final boolean castNeeded = GenerationUtil.isCastNeeded(caller, method, this.context);
if (castNeeded) {
writeCastForMethod(caller, method, context);
}
caller.accept(this);
if (castNeeded) {
builder.append(')');
}
builder.append('.');
}
}
builder.append(method.getName());
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, substitutor);
new ArgumentListGenerator(builder, this.context).generate(signature, exprs, namedArgs, closures, context);
}
Aggregations