@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { if (!(file instanceof GroovyFile)) return; VirtualFile vfile = file.getVirtualFile(); if (vfile == null) return; final Module module = ModuleUtilCore.findModuleForFile(vfile, project); if (module == null) return; final String packageName = ((GroovyFile) file).getPackageName(); PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(module, packageName, null, true); if (directory == null) return; String error = RefactoringMessageUtil.checkCanCreateFile(directory, file.getName()); if (error != null) { Messages.showMessageDialog( project, error, CommonBundle.getErrorTitle(), Messages.getErrorIcon()); return; } new MoveFilesOrDirectoriesProcessor( project, new PsiElement[] {file}, directory, false, false, false, null, null) .run(); }
private PsiClass buildClass() { if (existingClass != null) { return existingClass; } final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder(); beanClassBuilder.setVisibility(myCreateInnerClass ? PsiModifier.PRIVATE : PsiModifier.PUBLIC); beanClassBuilder.setProject(myProject); beanClassBuilder.setTypeArguments(typeParams); beanClassBuilder.setClassName(className); beanClassBuilder.setPackageName(packageName); for (ParameterChunk parameterChunk : parameters) { final VariableData parameter = parameterChunk.parameter; final boolean setterRequired = paramsNeedingSetters.contains(parameter.variable); beanClassBuilder.addField( (PsiParameter) parameter.variable, parameter.name, parameter.type, setterRequired); } final String classString = beanClassBuilder.buildBeanClass(); try { final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject()); final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(className + ".java", JavaFileType.INSTANCE, classString); if (myCreateInnerClass) { final PsiClass containingClass = method.getContainingClass(); final PsiClass[] classes = newFile.getClasses(); assert classes.length > 0 : classString; final PsiClass innerClass = (PsiClass) containingClass.add(classes[0]); PsiUtil.setModifierProperty(innerClass, PsiModifier.STATIC, true); return (PsiClass) JavaCodeStyleManager.getInstance(newFile.getProject()) .shortenClassReferences(innerClass); } else { final PsiFile containingFile = method.getContainingFile(); final PsiDirectory containingDirectory = containingFile.getContainingDirectory(); final PsiDirectory directory; if (myMoveDestination != null) { directory = myMoveDestination.getTargetDirectory(containingDirectory); } else { final Module module = ModuleUtil.findModuleForPsiElement(containingFile); directory = PackageUtil.findOrCreateDirectoryForPackage( module, packageName, containingDirectory, true, true); } if (directory != null) { final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject()); final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()) .shortenClassReferences(newFile); final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile); return ((PsiJavaFile) directory.add(reformattedFile)).getClasses()[0]; } } } catch (IncorrectOperationException e) { logger.info(e); } return null; }
private PsiClass buildClass() { final PsiManager manager = sourceClass.getManager(); final Project project = sourceClass.getProject(); final ExtractedClassBuilder extractedClassBuilder = new ExtractedClassBuilder(); extractedClassBuilder.setProject(myProject); extractedClassBuilder.setClassName(newClassName); extractedClassBuilder.setPackageName(newPackageName); extractedClassBuilder.setOriginalClassName(sourceClass.getQualifiedName()); extractedClassBuilder.setRequiresBackPointer(requiresBackpointer); extractedClassBuilder.setExtractAsEnum(enumConstants); for (PsiField field : fields) { extractedClassBuilder.addField(field); } for (PsiMethod method : methods) { extractedClassBuilder.addMethod(method); } for (PsiClass innerClass : innerClasses) { extractedClassBuilder.addInnerClass( innerClass, innerClassesToMakePublic.contains(innerClass)); } extractedClassBuilder.setTypeArguments(typeParams); final List<PsiClass> interfaces = calculateInterfacesSupported(); extractedClassBuilder.setInterfaces(interfaces); if (myGenerateAccessors) { final NecessaryAccessorsVisitor visitor = checkNecessaryGettersSetters4ExtractedClass(); sourceClass.accept(visitor); extractedClassBuilder.setFieldsNeedingGetters(visitor.getFieldsNeedingGetter()); extractedClassBuilder.setFieldsNeedingSetters(visitor.getFieldsNeedingSetter()); } final String classString = extractedClassBuilder.buildBeanClass(); if (extractInnerClass) { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText( newClassName + ".java", JavaFileType.INSTANCE, classString); final PsiClass psiClass = newFile.getClasses()[0]; if (!psiClass.isEnum()) { final PsiModifierList modifierList = psiClass.getModifierList(); assert modifierList != null; modifierList.setModifierProperty(PsiModifier.STATIC, true); } final PsiElement addedClass = sourceClass.add(psiClass); return (PsiClass) CodeStyleManager.getInstance(manager) .reformat( JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedClass)); } try { final PsiFile containingFile = sourceClass.getContainingFile(); final PsiDirectory directory; final PsiDirectory containingDirectory = containingFile.getContainingDirectory(); if (myMoveDestination != null) { directory = myMoveDestination.getTargetDirectory(containingDirectory); } else { final Module module = ModuleUtil.findModuleForPsiElement(containingFile); assert module != null; directory = PackageUtil.findOrCreateDirectoryForPackage( module, newPackageName, containingDirectory, false, true); } if (directory != null) { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final PsiFile newFile = factory.createFileFromText(newClassName + ".java", JavaFileType.INSTANCE, classString); final PsiElement addedFile = directory.add(newFile); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(project).shortenClassReferences(addedFile); return ((PsiJavaFile) codeStyleManager.reformat(shortenedFile)).getClasses()[0]; } else { return null; } } catch (IncorrectOperationException e) { return null; } }