private NecessaryAccessorsVisitor checkNecessaryGettersSetters4SourceClass() { final NecessaryAccessorsVisitor visitor = new NecessaryAccessorsVisitor() { @Override protected boolean hasGetterOrSetter(PsiMethod[] getters) { for (PsiMethod getter : getters) { if (!isInMovedElement(getter)) return true; } return false; } @Override protected boolean isProhibitedReference(PsiField field) { if (fields.contains(field)) { return false; } if (innerClasses.contains(field.getContainingClass())) { return false; } return true; } }; for (PsiField field : fields) { field.accept(visitor); } for (PsiMethod method : methods) { method.accept(visitor); } for (PsiClass innerClass : innerClasses) { innerClass.accept(visitor); } return visitor; }
private NecessaryAccessorsVisitor checkNecessaryGettersSetters4ExtractedClass() { final NecessaryAccessorsVisitor visitor = new NecessaryAccessorsVisitor() { @Override protected boolean hasGetterOrSetter(PsiMethod[] getters) { for (PsiMethod getter : getters) { if (isInMovedElement(getter)) return true; } return false; } @Override protected boolean isProhibitedReference(PsiField field) { if (fields.contains(field)) { return true; } if (innerClasses.contains(field.getContainingClass())) { return true; } return false; } @Override public void visitMethod(PsiMethod method) { if (methods.contains(method)) return; super.visitMethod(method); } @Override public void visitField(PsiField field) { if (fields.contains(field)) return; super.visitField(field); } @Override public void visitClass(PsiClass aClass) { if (innerClasses.contains(aClass)) return; super.visitClass(aClass); } }; sourceClass.accept(visitor); return visitor; }
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; } }