You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "Achal1607 (via GitHub)" <gi...@apache.org> on 2024/02/24 10:01:24 UTC

[PR] Refactoring pom.xml as well when renaming class inside editor [netbeans]

Achal1607 opened a new pull request, #7104:
URL: https://github.com/apache/netbeans/pull/7104

   When we currently rename any class inside the vscode editor, it refactors the file name, class name and other places where the class name is used, but it doesn't update the pom.xml in a maven project. So, this PR tries to address that issue.
   
   For more info : https://github.com/oracle/javavscode/issues/30
   
   
   ---
   **^Add meaningful description above**
   
   By opening a pull request you confirm that, unless explicitly stated otherwise, the changes -
   
    - are all your own work, and you have the right to contribute them.
    - are contributed solely under the terms and conditions of the Apache License 2.0 (see section 5 of the license for more information).
   
   Please make sure (eg. `git log`) that all commits have a valid name and email address for you in the Author field.
   
   If you're a first time contributor, see the Contributing guidelines for more information.
   
   If you're a committer, please label the PR before pressing "Create pull request" so that the right test jobs can run.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [PR] Refactoring pom.xml as well when renaming class inside editor [netbeans]

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien merged PR #7104:
URL: https://github.com/apache/netbeans/pull/7104


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [PR] Refactoring pom.xml as well when renaming class inside editor [netbeans]

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on PR #7104:
URL: https://github.com/apache/netbeans/pull/7104#issuecomment-1966501420

   ran a quick test and it worked great, thanks! merging.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [PR] Refactoring pom.xml as well when renaming class inside editor [netbeans]

Posted by "mbien (via GitHub)" <gi...@apache.org>.
mbien commented on code in PR #7104:
URL: https://github.com/apache/netbeans/pull/7104#discussion_r1502968286


##########
java/maven.refactoring/src/org/netbeans/modules/maven/refactoring/MavenRefactoringPlugin.java:
##########
@@ -73,6 +93,46 @@ class MavenRefactoringPlugin implements RefactoringPlugin {
             }
             @Override public void cancel() {}
         };
+        
+        if (refactoring != null) {
+            ModelOperation<POMModel> operation = (final POMModel model) -> {
+                Properties pr = model.getProject().getProperties();
+                ElementHandle e = handle.getElementHandle();
+                if (e != null) {
+                    String oldName = e.getBinaryName();
+                    String newName = refactoring.getNewName();
+
+                    if (pr.getProperty(RUN_MAIN_CLASS) != null) {
+                        String oldProperty = pr.getProperty(RUN_MAIN_CLASS);
+                        if (oldProperty.equals(oldName)) {
+                            int lastIndex = oldName.lastIndexOf('.');
+                            String newPropertyValue = newName;
+                            if (lastIndex >= 0) {
+                                String packageName = oldName.substring(0, lastIndex + 1);
+                                newPropertyValue = packageName + newPropertyValue;
+                            }
+                            pr.setProperty(RUN_MAIN_CLASS, newPropertyValue);
+                        }
+                    }
+                }
+            };
+
+            try {
+                FileObject fo = handle.getFileObject();
+                Project p = FileOwnerQuery.getOwner(fo);
+                final FileObject pom = p.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+                pom.getFileSystem().runAtomicAction(() -> {
+                    List<ModelOperation<POMModel>> operations = new ArrayList<>();
+                    operations.add(operation);
+                    org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);

Review Comment:
   a bit shorter:
   ```java
   performPOMModelOperations(pom, Arrays.asList(operation))
   ```
   



##########
java/maven.refactoring/src/org/netbeans/modules/maven/refactoring/MavenRefactoringPlugin.java:
##########
@@ -73,6 +93,46 @@ class MavenRefactoringPlugin implements RefactoringPlugin {
             }
             @Override public void cancel() {}
         };
+        
+        if (refactoring != null) {
+            ModelOperation<POMModel> operation = (final POMModel model) -> {

Review Comment:
   just a thought: if `operation` is renamed to `renameMainClassProp`, it would implicitly document what it does.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: [PR] Refactoring pom.xml as well when renaming class inside editor [netbeans]

Posted by "Achal1607 (via GitHub)" <gi...@apache.org>.
Achal1607 commented on code in PR #7104:
URL: https://github.com/apache/netbeans/pull/7104#discussion_r1503089650


##########
java/maven.refactoring/src/org/netbeans/modules/maven/refactoring/MavenRefactoringPlugin.java:
##########
@@ -73,6 +93,46 @@ class MavenRefactoringPlugin implements RefactoringPlugin {
             }
             @Override public void cancel() {}
         };
+        
+        if (refactoring != null) {
+            ModelOperation<POMModel> operation = (final POMModel model) -> {
+                Properties pr = model.getProject().getProperties();
+                ElementHandle e = handle.getElementHandle();
+                if (e != null) {
+                    String oldName = e.getBinaryName();
+                    String newName = refactoring.getNewName();
+
+                    if (pr.getProperty(RUN_MAIN_CLASS) != null) {
+                        String oldProperty = pr.getProperty(RUN_MAIN_CLASS);
+                        if (oldProperty.equals(oldName)) {
+                            int lastIndex = oldName.lastIndexOf('.');
+                            String newPropertyValue = newName;
+                            if (lastIndex >= 0) {
+                                String packageName = oldName.substring(0, lastIndex + 1);
+                                newPropertyValue = packageName + newPropertyValue;
+                            }
+                            pr.setProperty(RUN_MAIN_CLASS, newPropertyValue);
+                        }
+                    }
+                }
+            };
+
+            try {
+                FileObject fo = handle.getFileObject();
+                Project p = FileOwnerQuery.getOwner(fo);
+                final FileObject pom = p.getProjectDirectory().getFileObject("pom.xml"); // NOI18N
+                pom.getFileSystem().runAtomicAction(() -> {
+                    List<ModelOperation<POMModel>> operations = new ArrayList<>();
+                    operations.add(operation);
+                    org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, operations);

Review Comment:
   Sure done
   



##########
java/maven.refactoring/src/org/netbeans/modules/maven/refactoring/MavenRefactoringPlugin.java:
##########
@@ -73,6 +93,46 @@ class MavenRefactoringPlugin implements RefactoringPlugin {
             }
             @Override public void cancel() {}
         };
+        
+        if (refactoring != null) {
+            ModelOperation<POMModel> operation = (final POMModel model) -> {

Review Comment:
   Sure done
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists