use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method createField.
@NotNull
private GrVariableDeclaration createField(@NotNull PsiClass targetClass) {
final String name = mySettings.getName();
final PsiType type = mySettings.getSelectedType();
final String modifier = mySettings.getVisibilityModifier();
List<String> modifiers = new ArrayList<>();
if (targetClass instanceof GroovyScriptClass) {
modifiers.add("@" + GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD);
}
if (mySettings.isStatic())
modifiers.add(PsiModifier.STATIC);
if (!PsiModifier.PACKAGE_LOCAL.equals(modifier))
modifiers.add(modifier);
if (mySettings.declareFinal())
modifiers.add(PsiModifier.FINAL);
final String[] arr_modifiers = ArrayUtil.toStringArray(modifiers);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myContext.getProject());
if (targetClass instanceof GroovyScriptClass) {
return factory.createVariableDeclaration(arr_modifiers, ((GrExpression) null), type, name);
} else {
return factory.createFieldDeclaration(arr_modifiers, name, null, type);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method generateConstructor.
@NotNull
private PsiMethod generateConstructor(@NotNull PsiClass scope) {
final String name = scope.getName();
LOG.assertTrue(name != null, scope.getText());
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myContext.getProject());
final GrMethod constructor = factory.createConstructorFromText(name, ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", scope);
if (scope instanceof GroovyScriptClass)
constructor.getModifierList().setModifierProperty(GrModifier.DEF, true);
return (PsiMethod) scope.add(constructor);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class FieldConflictsResolver method qualifyReference.
public static GrReferenceExpression qualifyReference(GrReferenceExpression referenceExpression, final PsiMember member, @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
PsiManager manager = referenceExpression.getManager();
GrReferenceExpression expressionFromText;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(referenceExpression.getProject());
if (qualifyingClass == null) {
PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
final PsiClass containingClass = member.getContainingClass();
if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
}
LOG.assertTrue(parentClass != null);
expressionFromText = factory.createReferenceExpressionFromText("A.this." + member.getName());
//noinspection ConstantConditions
((GrReferenceExpression) expressionFromText.getQualifier()).getQualifier().replace(factory.createReferenceElementForClass(parentClass));
} else {
expressionFromText = (GrReferenceExpression) factory.createExpressionFromText("this." + member.getName());
}
} else {
expressionFromText = (GrReferenceExpression) factory.createExpressionFromText("A." + member.getName());
expressionFromText.setQualifier(factory.createReferenceElementForClass(qualifyingClass));
}
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
expressionFromText = (GrReferenceExpression) codeStyleManager.reformat(expressionFromText);
return (GrReferenceExpression) referenceExpression.replace(expressionFromText);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceConstantProcessor method createField.
@NotNull
private GrVariableDeclaration createField(PsiClass targetClass) {
final String name = settings.getName();
final PsiType type = settings.getSelectedType();
String[] modifiers = collectModifiers(targetClass);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
return factory.createFieldDeclaration(modifiers, name, getInitializer(), type);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method initializeInAnonymousClassInitializer.
private void initializeInAnonymousClassInitializer(@NotNull GrVariable field, @NotNull GrAnonymousClassDefinition scope, @NotNull Collection<PsiElement> replaced) {
final GrClassInitializer[] initializers = scope.getInitializers();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myContext.getProject());
final GrClassInitializer initializer = initializers.length == 0 ? (GrClassInitializer) scope.add(factory.createClassInitializer()) : initializers[0];
final GrStatement anchor = findAnchorForAssignment(initializer.getBlock(), replaced);
initializeInMethodInner(field, initializer.getBlock(), anchor, ContainerUtil.getFirstItem(replaced));
}
Aggregations