@Nullable private static String verifyDestinationForElement( final PsiElement element, final MoveDestination moveDestination) { final String message; if (element instanceof PsiDirectory) { message = moveDestination.verify((PsiDirectory) element); } else if (element instanceof PsiPackage) { message = moveDestination.verify((PsiPackage) element); } else { message = moveDestination.verify(element.getContainingFile()); } return message; }
private void invokeMoveToPackage() { final MoveDestination destination = selectDestination(); if (destination == null) return; saveRefactoringSettings(); for (final PsiElement element : myElementsToMove) { String message = verifyDestinationForElement(element, destination); if (message != null) { String helpId = HelpID.getMoveHelpID(myElementsToMove[0]); CommonRefactoringUtil.showErrorMessage( RefactoringBundle.message("error.title"), message, helpId, getProject()); return; } } try { for (PsiElement element : myElementsToMove) { if (element instanceof PsiClass) { final PsiClass aClass = (PsiClass) element; LOG.assertTrue(aClass.isPhysical(), aClass); /*PsiElement toAdd; if (aClass.getContainingFile() instanceof PsiJavaFile && ((PsiJavaFile)aClass.getContainingFile()).getClasses().length > 1) { toAdd = aClass; } else { toAdd = aClass.getContainingFile(); }*/ final PsiDirectory targetDirectory = destination.getTargetIfExists(element.getContainingFile()); if (targetDirectory != null) { MoveFilesOrDirectoriesUtil.checkMove(aClass, targetDirectory); } } } MoveClassesOrPackagesProcessor processor = createMoveToPackageProcessor(destination, myElementsToMove, myMoveCallback); if (processor.verifyValidPackageName()) { processor.setOpenInEditor(isOpenInEditor()); invokeRefactoring(processor); } } catch (IncorrectOperationException e) { String helpId = HelpID.getMoveHelpID(myElementsToMove[0]); CommonRefactoringUtil.showErrorMessage( RefactoringBundle.message("error.title"), e.getMessage(), helpId, getProject()); } }
@Override protected boolean preprocessUsages(@NotNull final Ref<UsageInfo[]> refUsages) { MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>(); if (myUseExistingClass) { if (existingClass == null) { conflicts.putValue( null, RefactorJBundle.message("cannot.perform.the.refactoring") + "Could not find the selected class"); } if (myExistingClassCompatibleConstructor == null) { conflicts.putValue( existingClass, RefactorJBundle.message("cannot.perform.the.refactoring") + "Selected class has no compatible constructors"); } } else { if (existingClass != null) { conflicts.putValue( existingClass, RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message("there.already.exists.a.class.with.the.chosen.name")); } if (myMoveDestination != null) { if (!myMoveDestination.isTargetAccessible( myProject, method.getContainingFile().getVirtualFile())) { conflicts.putValue(method, "Created class won't be accessible"); } } } for (UsageInfo usageInfo : refUsages.get()) { if (usageInfo instanceof FixableUsageInfo) { final String conflictMessage = ((FixableUsageInfo) usageInfo).getConflictMessage(); if (conflictMessage != null) { conflicts.putValue(usageInfo.getElement(), conflictMessage); } } } return showConflicts(conflicts, refUsages.get()); }
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; } }