use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyShellLanguageConsoleView method prepareTypeDefinition.
@NotNull
private GrTypeDefinition prepareTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
GroovyFile file = factory.createGroovyFile("", false, getFile());
return (GrTypeDefinition) file.add(typeDefinition);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyTestFramework method findOrCreateSetUpMethod.
@Override
protected PsiMethod findOrCreateSetUpMethod(PsiClass clazz) throws IncorrectOperationException {
LOG.assertTrue(clazz.getLanguage() == GroovyLanguage.INSTANCE);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(clazz.getProject());
final PsiMethod patternMethod = createSetUpPatternMethod(factory);
final PsiClass baseClass = clazz.getSuperClass();
if (baseClass != null) {
final PsiMethod baseMethod = baseClass.findMethodBySignature(patternMethod, false);
if (baseMethod != null && baseMethod.hasModifierProperty(PsiModifier.PUBLIC)) {
PsiUtil.setModifierProperty(patternMethod, PsiModifier.PROTECTED, false);
PsiUtil.setModifierProperty(patternMethod, PsiModifier.PUBLIC, true);
}
}
PsiMethod inClass = clazz.findMethodBySignature(patternMethod, false);
if (inClass == null) {
PsiMethod testMethod = JUnitUtil.findFirstTestMethod(clazz);
if (testMethod != null) {
return (PsiMethod) clazz.addBefore(patternMethod, testMethod);
}
return (PsiMethod) clazz.add(patternMethod);
} else if (inClass.getBody() == null) {
return (PsiMethod) inClass.replace(patternMethod);
}
return inClass;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyTestGenerator method addSuperClass.
private static void addSuperClass(@NotNull GrTypeDefinition targetClass, @NotNull Project project, @Nullable String superClassName) throws IncorrectOperationException {
if (superClassName == null)
return;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
PsiClass superClass = findClass(project, superClassName);
GrCodeReferenceElement superClassRef;
if (superClass != null) {
superClassRef = factory.createCodeReferenceElementFromClass(superClass);
} else {
superClassRef = factory.createCodeReferenceElementFromText(superClassName);
}
GrExtendsClause extendsClause = targetClass.getExtendsClause();
if (extendsClause == null) {
extendsClause = (GrExtendsClause) targetClass.addAfter(factory.createExtendsClause(), targetClass.getNameIdentifierGroovy());
}
extendsClause.add(superClassRef);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyTestGenerator method generateMethod.
private static void generateMethod(@NotNull TestIntegrationUtils.MethodKind methodKind, TestFramework descriptor, PsiClass targetClass, Editor editor, @Nullable String name, Set<String> existingNames) {
GroovyPsiElementFactory f = GroovyPsiElementFactory.getInstance(targetClass.getProject());
PsiMethod method = (PsiMethod) targetClass.add(f.createMethod("dummy", PsiType.VOID));
PsiDocumentManager.getInstance(targetClass.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument());
TestIntegrationUtils.runTestMethodTemplate(methodKind, descriptor, editor, targetClass, method, name, true, existingNames);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceVariableHandler method generateDeclaration.
@NotNull
private static GrVariableDeclaration generateDeclaration(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
final String[] modifiers = settings.isDeclareFinal() ? new String[] { PsiModifier.FINAL } : null;
final GrVariableDeclaration declaration = factory.createVariableDeclaration(modifiers, "foo", settings.getSelectedType(), settings.getName());
generateInitializer(context, declaration.getVariables()[0]);
return declaration;
}
Aggregations