provide context for new methods: createMethod(name, returnType, context)

This commit is contained in:
Anna Kozlova
2014-10-23 20:18:52 +02:00
parent 58de0c83c3
commit 0109983010
5 changed files with 21 additions and 2 deletions
@@ -475,7 +475,7 @@ public class GenerateMembersUtil {
if (method.isConstructor()) {
return factory.createConstructor(method.getName(), target);
}
return factory.createMethodFromText("public void " + method.getName() + "(){}", target);
return factory.createMethod(method.getName(), PsiType.VOID, target);
}
private static void substituteReturnType(@NotNull PsiManager manager,
@@ -84,6 +84,9 @@ public interface JVMElementFactory {
@NotNull
PsiMethod createMethod(@NotNull @NonNls String name, PsiType returnType) throws IncorrectOperationException;
@NotNull
PsiMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException;
/**
* Creates an empty constructor.
*
@@ -247,6 +247,12 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
return (PsiMethod)CodeStyleManager.getInstance(myManager.getProject()).reformat(method);
}
@NotNull
@Override
public PsiMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException {
return createMethodFromText("public " + returnType.getCanonicalText(true) + " " + name + "() {}", context);
}
@NotNull
@Override
public PsiMethod createConstructor() {
@@ -265,6 +265,10 @@ public abstract class GroovyPsiElementFactory implements JVMElementFactory {
@NotNull
public abstract GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType) throws IncorrectOperationException;
@Override
@NotNull
public abstract GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType, @Nullable PsiElement context) throws IncorrectOperationException;
@NotNull
@Override
public abstract GrMethod createConstructor();
@@ -1016,6 +1016,12 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
@NotNull
@Override
public GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType) throws IncorrectOperationException {
return createMethod(name, returnType, null);
}
@NotNull
@Override
public GrMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException {
StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
builder.append("def <T>");
@@ -1032,7 +1038,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
builder.append('"');
}
builder.append("(){}");
GrMethod method = createMethodFromText(builder);
GrMethod method = createMethodFromText(builder.toString(), context);
if (returnType != null) {
method.getModifierList().setModifierProperty(GrModifier.DEF, false);
}