use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrFieldMember method generateGetter.
@Override
@Nullable
public GroovyGenerationInfo<GrMethod> generateGetter() {
PsiField field = getElement();
final GrMethod method = createMethodIfNotExists(field, GroovyPropertyUtils.generateGetterPrototype(field));
return method != null ? new GroovyGenerationInfo<>(method) : null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyGenerateConstructorHandler method chooseOriginalMembers.
@Override
@Nullable
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
final ClassMember[] classMembers = chooseOriginalMembersImpl(aClass, project);
if (classMembers == null)
return null;
List<ClassMember> res = new ArrayList<>();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
for (ClassMember classMember : classMembers) {
if (classMember instanceof PsiMethodMember) {
final PsiMethod method = ((PsiMethodMember) classMember).getElement();
PsiMethod copy = factory.createMethodFromText(GroovyToJavaGenerator.generateMethodStub(method), method);
if (method instanceof GrMethod) {
GrParameter[] parameters = ((GrMethod) method).getParameterList().getParameters();
PsiParameter[] copyParameters = copy.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].getTypeElementGroovy() == null) {
copyParameters[i].setName(DEF_PSEUDO_ANNO + parameters[i].getName());
}
}
}
res.add(new PsiMethodMember(copy));
} else if (classMember instanceof PsiFieldMember) {
final PsiField field = ((PsiFieldMember) classMember).getElement();
String prefix = field instanceof GrField && ((GrField) field).getTypeElementGroovy() == null ? DEF_PSEUDO_ANNO : "";
res.add(new PsiFieldMember(factory.createFieldFromText(field.getType().getCanonicalText() + " " + prefix + field.getName(), aClass)));
}
}
return res.toArray(new ClassMember[res.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyGenerateConstructorHandler method generateMemberPrototypes.
@Override
@NotNull
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
final List<? extends GenerationInfo> list = super.generateMemberPrototypes(aClass, members);
List<PsiGenerationInfo<GrMethod>> grConstructors = new ArrayList<>();
final Project project = aClass.getProject();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
for (GenerationInfo generationInfo : list) {
final PsiMember constructorMember = generationInfo.getPsiMember();
assert constructorMember instanceof PsiMethod;
final PsiMethod constructor = (PsiMethod) constructorMember;
final PsiCodeBlock block = constructor.getBody();
assert block != null;
final String constructorName = aClass.getName();
final String body = StringUtil.replace(StringUtil.replace(block.getText(), DEF_PSEUDO_ANNO, ""), ";", "");
final PsiParameterList list1 = constructor.getParameterList();
List<String> parametersNames = new ArrayList<>();
List<String> parametersTypes = new ArrayList<>();
for (PsiParameter parameter : list1.getParameters()) {
final String fullName = parameter.getName();
parametersNames.add(StringUtil.trimStart(fullName, DEF_PSEUDO_ANNO));
parametersTypes.add(fullName.startsWith(DEF_PSEUDO_ANNO) ? null : parameter.getType().getCanonicalText());
}
final String[] paramNames = ArrayUtil.toStringArray(parametersNames);
final String[] paramTypes = ArrayUtil.toStringArray(parametersTypes);
assert constructorName != null;
GrMethod grConstructor = factory.createConstructorFromText(constructorName, paramTypes, paramNames, body);
PsiReferenceList throwsList = grConstructor.getThrowsList();
for (PsiJavaCodeReferenceElement element : constructor.getThrowsList().getReferenceElements()) {
throwsList.add(element);
}
codeStyleManager.shortenClassReferences(grConstructor);
grConstructors.add(new GroovyGenerationInfo<>(grConstructor));
}
return grConstructors;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class CategoryTransformationSupport method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
PsiClassType type = GdkMethodUtil.getCategoryType(context.getCodeClass());
if (type == null)
return;
for (GrMethod method : context.getCodeClass().getCodeMethods()) {
context.removeMethod(method);
context.addMethods(GrReflectedMethodImpl.doCreateReflectedMethods(method, type, method.getParameters()));
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method generateDelegate.
private static boolean generateDelegate(JavaChangeInfo grInfo) {
final GrMethod method = (GrMethod) grInfo.getMethod();
final PsiClass psiClass = method.getContainingClass();
GrMethod newMethod = (GrMethod) method.copy();
newMethod = (GrMethod) psiClass.addAfter(newMethod, method);
StringBuilder buffer = new StringBuilder();
buffer.append("\n");
if (method.isConstructor()) {
buffer.append("this");
} else {
if (!PsiType.VOID.equals(method.getReturnType())) {
buffer.append("return ");
}
buffer.append(GrChangeSignatureUtil.getNameWithQuotesIfNeeded(grInfo.getNewName(), method.getProject()));
}
generateParametersForDelegateCall(grInfo, method, buffer);
final GrCodeBlock codeBlock = GroovyPsiElementFactory.getInstance(method.getProject()).createMethodBodyFromText(buffer.toString());
newMethod.setBlock(codeBlock);
newMethod.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false);
CodeStyleManager.getInstance(method.getProject()).reformat(newMethod);
return processPrimaryMethodInner(grInfo, method, null);
}
Aggregations