use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrChangeSignatureHandler method findTargetMember.
@Override
@Nullable
public PsiElement findTargetMember(PsiElement element) {
final GrParameterList parameterList = PsiTreeUtil.getParentOfType(element, GrParameterList.class);
if (parameterList != null) {
final PsiElement parent = parameterList.getParent();
if (parent instanceof PsiMethod)
return parent;
}
if (element.getParent() instanceof GrMethod && ((GrMethod) element.getParent()).getNameIdentifierGroovy() == element) {
return element.getParent();
}
final GrCall expression = PsiTreeUtil.getParentOfType(element, GrCall.class);
if (expression != null) {
return expression.resolveMethod();
}
final PsiReference ref = element.getReference();
if (ref == null)
return null;
return ref.resolve();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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.GrMethod in project intellij-community by JetBrains.
the class ClosureGenerator method generateClosureMethod.
@NotNull
private GrMethod generateClosureMethod(@NotNull GrClosableBlock block) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
final GrMethod method = factory.createMethodFromText("def doCall(){}", block);
GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(context.typeProvider.getReturnType(block)));
if (block.hasParametersSection()) {
method.getParameterList().replace(block.getParameterList());
} else {
final GrParameter[] allParameters = block.getAllParameters();
LOG.assertTrue(allParameters.length == 1);
final GrParameter itParameter = allParameters[0];
final GrParameter parameter = factory.createParameter("it", itParameter.getType().getCanonicalText(), "null", block);
method.getParameterList().add(parameter);
}
((GroovyFileImpl) method.getContainingFile()).setContextNullable(null);
return method;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyCompletionData method hasReturnValue.
private static boolean hasReturnValue(PsiElement context) {
GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(context);
if (flowOwner instanceof GrClosableBlock)
return true;
if (flowOwner instanceof GroovyFile)
return true;
if (flowOwner == null)
return true;
PsiElement parent = flowOwner.getParent();
if (parent instanceof GrMethod) {
return !PsiType.VOID.equals(((GrMethod) parent).getReturnType());
} else if (parent instanceof GrClassInitializer) {
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 ReplaceAbstractClassInstanceByMapIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiDocumentManager.getInstance(project).commitAllDocuments();
GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
final PsiElement resolved = ref.resolve();
// && ((PsiClass)resolved).isInterface();
assert resolved instanceof PsiClass;
GrTypeDefinitionBody body = anonymous.getBody();
assert body != null;
List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
for (GrMethod method : body.getMethods()) {
methods.add(new Pair<>(method, method.getBlock()));
}
final PsiClass iface = (PsiClass) resolved;
final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
for (CandidateInfo info : collection) {
methods.add(new Pair<>((PsiMethod) info.getElement(), null));
}
StringBuilder buffer = new StringBuilder();
if (methods.size() == 1) {
final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
buffer.append(" as ").append(iface.getQualifiedName());
}
} else {
buffer.append("[");
buffer.append("\n");
for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
final PsiMethod method = pair.getFirst();
final GrOpenBlock block = pair.getSecond();
buffer.append(method.getName()).append(": ");
appendClosureTextByMethod(method, buffer, block, newExpr);
buffer.append(",\n");
}
if (!methods.isEmpty()) {
buffer.delete(buffer.length() - 2, buffer.length());
buffer.append('\n');
}
buffer.append("]");
buffer.append(" as ").append(iface.getQualifiedName());
}
createAndAdjustNewExpression(project, newExpr, buffer);
}
Aggregations