encapsulate fields: javadoc placement settings (IDEA-52195)
authoranna <anna.kozlova@jetbrains.com>
Fri, 12 Feb 2010 15:10:30 +0000 (12 18:10 +0300)
committeranna <anna.kozlova@jetbrains.com>
Fri, 12 Feb 2010 15:57:11 +0000 (12 18:57 +0300)
java/java-impl/src/com/intellij/refactoring/encapsulateFields/EncapsulateFieldsDescriptor.java
java/java-impl/src/com/intellij/refactoring/encapsulateFields/EncapsulateFieldsDialog.java
java/java-impl/src/com/intellij/refactoring/encapsulateFields/EncapsulateFieldsProcessor.java
java/java-tests/testData/refactoring/encapsulateFields/moveJavadocToGetter/after/Test.java [new file with mode: 0644]
java/java-tests/testData/refactoring/encapsulateFields/moveJavadocToGetter/before/Test.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/EncapsulateFieldsTest.java

index 4431cb5..d3fd0e5 100644 (file)
@@ -26,6 +26,7 @@ import com.intellij.psi.util.PsiFormatUtil;
 import com.intellij.refactoring.HelpID;
 import com.intellij.refactoring.RefactoringBundle;
 import com.intellij.refactoring.JavaRefactoringSettings;
+import com.intellij.refactoring.ui.DocCommentPanel;
 import com.intellij.refactoring.ui.RefactoringDialog;
 import com.intellij.refactoring.util.CommonRefactoringUtil;
 import com.intellij.refactoring.util.RefactoringMessageUtil;
@@ -84,6 +85,7 @@ public class EncapsulateFieldsDialog extends RefactoringDialog implements Encaps
   private final JRadioButton myRbAccessorPrivate = new JRadioButton();
   private final JRadioButton myRbAccessorPackageLocal = new JRadioButton();
   private static final String REFACTORING_NAME = RefactoringBundle.message("encapsulate.fields.title");
+  private DocCommentPanel myJavadocPolicy;
 
   {
     myCbEncapsulateGet.setFocusable(false);
@@ -224,6 +226,10 @@ public class EncapsulateFieldsDialog extends RefactoringDialog implements Encaps
     }
   }
 
+  public int getJavadocPolicy() {
+    return myJavadocPolicy.getPolicy();
+  }
+
   protected String getDimensionServiceKey() {
     return "#com.intellij.refactoring.encapsulateFields.EncalpsulateFieldsDialog";
   }
@@ -310,22 +316,25 @@ public String getAccessorsVisibility() {
     myCbEncapsulateGet.setPreferredSize(myCbUseAccessorsWhenAccessible.getPreferredSize());
     leftBox.add(myCbEncapsulateGet);
     leftBox.add(myCbEncapsulateSet);
+    leftBox.add(Box.createVerticalStrut(10));
+    leftBox.add(myCbUseAccessorsWhenAccessible);
     JPanel leftPanel = new JPanel(new BorderLayout());
     leftPanel.setBorder(IdeBorderFactory.createTitledBorder(RefactoringBundle.message("encapsulate.fields.encapsulate.border.title")));
     leftPanel.add(leftBox, BorderLayout.CENTER);
     leftPanel.add(Box.createHorizontalStrut(5), BorderLayout.WEST);
 
-    Box rightBox = Box.createVerticalBox();
-    rightBox.add(myCbUseAccessorsWhenAccessible);
-    JPanel rightPanel = new JPanel(new BorderLayout());
-    rightPanel.setBorder(IdeBorderFactory.createTitledBorder(RefactoringBundle.message("encapsulate.fields.options.border.title")));
-    rightPanel.add(rightBox, BorderLayout.CENTER);
-    rightPanel.add(Box.createHorizontalStrut(5), BorderLayout.WEST);
-
-    Box encapsulateBox = Box.createHorizontalBox();
-    encapsulateBox.add(leftPanel);
-    encapsulateBox.add(Box.createHorizontalStrut(5));
-    encapsulateBox.add(rightPanel);
+    JPanel encapsulateBox = new JPanel(new BorderLayout());
+    encapsulateBox.add(leftPanel, BorderLayout.CENTER);
+    myJavadocPolicy = new DocCommentPanel("JavaDoc");
+    encapsulateBox.add(myJavadocPolicy, BorderLayout.EAST);
+    boolean hasJavadoc = false;
+    for (PsiField field : myFields) {
+      if (field.getDocComment() != null) {
+        hasJavadoc = true;
+        break;
+      }
+    }
+    myJavadocPolicy.setVisible(hasJavadoc);
 
     Box fieldsBox = Box.createVerticalBox();
     fieldsBox.add(myRbFieldPrivate);
index 10fc740..b2d8662 100644 (file)
@@ -22,6 +22,7 @@ import com.intellij.openapi.util.Ref;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.CodeStyleManager;
+import com.intellij.psi.javadoc.PsiDocComment;
 import com.intellij.psi.search.searches.ClassInheritorsSearch;
 import com.intellij.psi.search.searches.ReferencesSearch;
 import com.intellij.psi.tree.IElementType;
@@ -32,6 +33,7 @@ import com.intellij.psi.util.PsiUtil;
 import com.intellij.refactoring.BaseRefactoringProcessor;
 import com.intellij.refactoring.RefactoringBundle;
 import com.intellij.refactoring.util.CommonRefactoringUtil;
+import com.intellij.refactoring.util.DocCommentPolicy;
 import com.intellij.refactoring.util.RefactoringUIUtil;
 import com.intellij.refactoring.util.RefactoringUtil;
 import com.intellij.usageView.UsageInfo;
@@ -256,17 +258,27 @@ public class EncapsulateFieldsProcessor extends BaseRefactoringProcessor {
     myNameToGetter = new HashMap<String, PsiMethod>();
     myNameToSetter = new HashMap<String, PsiMethod>();
     for(int i = 0; i < myFields.length; i++){
+      final DocCommentPolicy<PsiDocComment> commentPolicy = new DocCommentPolicy<PsiDocComment>(myDescriptor.getJavadocPolicy());
       PsiField field = myFields[i];
+      final PsiDocComment docComment = field.getDocComment();
       if (myDescriptor.isToEncapsulateGet()){
         PsiMethod[] prototypes = myDescriptor.getGetterPrototypes();
         assert prototypes != null;
-        addOrChangeAccessor(prototypes[i], myNameToGetter);
+        final PsiMethod getter = addOrChangeAccessor(prototypes[i], myNameToGetter);
+        if (docComment != null) {
+          final PsiDocComment getterJavadoc = (PsiDocComment)getter.addBefore(docComment, getter.getFirstChild());
+          commentPolicy.processNewJavaDoc(getterJavadoc);
+        }
       }
       if (myDescriptor.isToEncapsulateSet() && !field.hasModifierProperty(PsiModifier.FINAL)){
         PsiMethod[] prototypes = myDescriptor.getSetterPrototypes();
         assert prototypes != null;
         addOrChangeAccessor(prototypes[i], myNameToSetter);
       }
+
+      if (docComment != null) {
+        commentPolicy.processOldJavaDoc(docComment);
+      }
     }
 
     Map<PsiFile, List<MyUsageInfo>> usagesInFiles = new HashMap<PsiFile, List<MyUsageInfo>>();
@@ -305,7 +317,7 @@ public class EncapsulateFieldsProcessor extends BaseRefactoringProcessor {
     }
   }
 
-  private void addOrChangeAccessor(PsiMethod prototype, HashMap<String,PsiMethod> nameToAncestor) {
+  private PsiMethod addOrChangeAccessor(PsiMethod prototype, HashMap<String,PsiMethod> nameToAncestor) {
     PsiMethod existing = myClass.findMethodBySignature(prototype, false);
     PsiMethod result = existing;
     try{
@@ -317,10 +329,12 @@ public class EncapsulateFieldsProcessor extends BaseRefactoringProcessor {
         //TODO : change visibility
       }
       nameToAncestor.put(prototype.getName(), result);
+      return result;
     }
     catch(IncorrectOperationException e){
       LOG.error(e);
     }
+    return null;
   }
 
   private boolean isUsedInExistingAccessor(PsiMethod prototype, PsiElement element) {
diff --git a/java/java-tests/testData/refactoring/encapsulateFields/moveJavadocToGetter/after/Test.java b/java/java-tests/testData/refactoring/encapsulateFields/moveJavadocToGetter/after/Test.java
new file mode 100644 (file)
index 0000000..ca50188
--- /dev/null
@@ -0,0 +1,21 @@
+public class A {
+  public int i;
+
+
+  public static void main(String[] args) {
+    A a = new B();
+    a.setI(23);
+    System.out.println(a.getI());
+  }
+
+    /**
+     * my javadoc for field
+     */
+    public int getI() {
+        return i;
+    }
+
+    public void setI(int i) {
+        this.i = i;
+    }
+}
diff --git a/java/java-tests/testData/refactoring/encapsulateFields/moveJavadocToGetter/before/Test.java b/java/java-tests/testData/refactoring/encapsulateFields/moveJavadocToGetter/before/Test.java
new file mode 100644 (file)
index 0000000..5a63451
--- /dev/null
@@ -0,0 +1,13 @@
+public class A {
+  /**
+   * my javadoc for field
+   */
+  public int i;
+
+
+  public static void main(String[] args) {
+    A a = new B();
+    a.i = 23;
+    System.out.println(a.i);
+  }
+}
index be80001..45e6a73 100644 (file)
@@ -14,6 +14,7 @@ import com.intellij.psi.search.GlobalSearchScope;
 import com.intellij.psi.util.PropertyUtil;
 import com.intellij.refactoring.encapsulateFields.EncapsulateFieldsDescriptor;
 import com.intellij.refactoring.encapsulateFields.EncapsulateFieldsProcessor;
+import com.intellij.refactoring.util.DocCommentPolicy;
 import junit.framework.Assert;
 import org.jetbrains.annotations.Nullable;
 
@@ -38,6 +39,18 @@ public class EncapsulateFieldsTest extends MultiFileTestCase{
     doTest("i", "A.B", "There is already a <b><code>method <b><code>A.getI()</code></b></code></b> which would be hidden by generated getter");
   }
 
+  public void testMoveJavadocToGetter() throws Exception {
+    doTest(new PerformAction() {
+      public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
+        final PsiClass aClass = myJavaFacade.findClass("A", GlobalSearchScope.projectScope(myProject));
+        assertNotNull("Tested class not found", aClass);
+        final PsiField field = aClass.findFieldByName("i", false);
+        assertNotNull(field);
+        doTest(aClass, field, null, true, true);
+      }
+    });
+  }
+
   @Override
   protected String getTestDataPath() {
     return JavaTestUtil.getJavaTestDataPath();
@@ -115,6 +128,10 @@ public class EncapsulateFieldsTest extends MultiFileTestCase{
         public String getAccessorsVisibility() {
           return PsiModifier.PUBLIC;
         }
+
+        public int getJavadocPolicy() {
+          return DocCommentPolicy.MOVE;
+        }
       });
       processor.run();
       LocalFileSystem.getInstance().refresh(false);