use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod in project intellij-community by JetBrains.
the class GroovyMethodInliner method prepareNewMethod.
/**
* Prepare temporary method with non-conflicting local names
*/
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
if (method instanceof GrReflectedMethod) {
method = ((GrReflectedMethod) method).getBaseMethod();
}
GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
if (qualifier != null) {
Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
}
ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
// there are only local variables and parameters (possible of inner closures)
for (PsiNamedElement namedElement : innerDefinitions) {
String name = namedElement.getName();
if (name != null) {
String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
if (!newName.equals(namedElement.getName())) {
final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (element instanceof GrReferenceExpression) {
GrExpression newExpr = factory.createExpressionFromText(newName);
((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
}
}
namedElement.setName(newName);
}
}
}
GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
return newMethod;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod in project intellij-community by JetBrains.
the class GrChangeSignatureHandler method invoke.
private static void invoke(PsiMethod method, final Project project) {
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method))
return;
if (method instanceof GrReflectedMethod)
method = ((GrReflectedMethod) method).getBaseMethod();
PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
if (newMethod == null)
return;
if (!newMethod.equals(method)) {
ChangeSignatureUtil.invokeChangeSignatureOn(newMethod, project);
return;
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method))
return;
//todo
if (!(method instanceof GrMethod))
return;
final GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, new GrMethodDescriptor((GrMethod) method), true, null);
dialog.show();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod in project intellij-community by JetBrains.
the class ClosureGenerator method generate.
public void generate(@NotNull GrClosableBlock closure) {
builder.append("new ");
TypeWriter.writeTypeForNew(builder, closure.getType(), closure);
builder.append('(');
final CharSequence owner = getOwner(closure);
builder.append(owner);
builder.append(", ");
builder.append(owner);
builder.append(") {\n");
generateClosureMainMethod(closure);
final ClassItemGeneratorImpl generator = new ClassItemGeneratorImpl(context);
final GrMethod method = generateClosureMethod(closure);
final GrReflectedMethod[] reflectedMethods = method.getReflectedMethods();
if (reflectedMethods.length > 0) {
for (GrReflectedMethod reflectedMethod : reflectedMethods) {
if (reflectedMethod.getSkippedParameters().length > 0) {
generator.writeMethod(builder, reflectedMethod);
builder.append('\n');
}
}
}
builder.append('}');
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod in project intellij-community by JetBrains.
the class XmlMarkupBuilderNonCodeMemberContributor method processDynamicMethods.
@Override
boolean processDynamicMethods(@NotNull PsiType qualifierType, @NotNull PsiClass clazz, @NotNull String name, @NotNull PsiElement place, @NotNull Processor<PsiElement> processor) {
GrLightMethodBuilder res;
// ()
res = createMethod(name, clazz, place);
if (!processor.process(res))
return false;
// (Closure)
res = createMethod(name, clazz, place);
res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, false).putUserData(DELEGATES_TO_KEY, FQN);
if (!processor.process(res))
return false;
// (Object, Closure)
res = createMethod(name, clazz, place);
res.addParameter("value", JAVA_LANG_OBJECT, false);
res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, false).putUserData(DELEGATES_TO_KEY, FQN);
if (!processor.process(res))
return false;
// (Map, Closure)
res = createMethod(name, clazz, place);
res.addParameter("attributes", JAVA_UTIL_MAP, false);
res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, false).putUserData(DELEGATES_TO_KEY, FQN);
if (!processor.process(res))
return false;
// (Map)
// (Map, Object)
// (Map, Object, Closure)
res = createMethod(name, clazz, place);
res.addParameter("attributes", JAVA_UTIL_MAP, false);
res.addParameter("value", JAVA_LANG_OBJECT, true);
res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, true).putUserData(DELEGATES_TO_KEY, FQN);
for (GrReflectedMethod method : res.getReflectedMethods()) {
if (!processor.process(method))
return false;
}
// (Object)
// (Object, Map)
// (Object, Map, Closure)
res = createMethod(name, clazz, place);
res.addParameter("value", JAVA_LANG_OBJECT, false);
res.addParameter("attributes", JAVA_UTIL_MAP, true);
res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, true).putUserData(DELEGATES_TO_KEY, FQN);
for (GrReflectedMethod method : res.getReflectedMethods()) {
if (!processor.process(method))
return false;
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod in project intellij-community by JetBrains.
the class GrReflectedMethodImpl method doCreateReflectedMethods.
@NotNull
public static GrReflectedMethod[] doCreateReflectedMethods(@NotNull GrMethod targetMethod, @Nullable PsiClassType categoryType, @NotNull GrParameter[] parameters) {
int count = 0;
for (GrParameter parameter : parameters) {
if (parameter.isOptional())
count++;
}
if (count == 0 && categoryType == null)
return GrReflectedMethod.EMPTY_ARRAY;
final GrReflectedMethod[] methods = new GrReflectedMethod[count + 1];
for (int i = 0; i <= count; i++) {
methods[i] = new GrReflectedMethodImpl(targetMethod, parameters, count - i, categoryType);
}
return methods;
}
Aggregations