You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "mbien (via GitHub)" <gi...@apache.org> on 2025/02/02 13:50:39 UTC

[PR] Organize Members hint: don't rearrange record components [netbeans]

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

   if the formatter is configured to sort members alphabetically, the hint should not rearrange record components.
   
   The fix reuses the existing special case for enums, which holds the enum "members" in place and does the same for synthetic record members.
   
   ```java
   import java.awt.Color;
   
   // enable formatter setting: ordering -> alphabetical member sort
   // this should not influence record components
   public record OrderMembersRecord(Color borderColor, double borderSize, int width, int height) {
   
       public static int c;
       public static int b;
       public static int d;
       public static int a;
   
   }
   ```


-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


##########
java/java.hints/src/org/netbeans/modules/java/hints/OrganizeMembers.java:
##########
@@ -122,49 +122,44 @@ private static void doOrganizeMembers(WorkingCopy copy, TreePath path) {
         ClassTree clazz = (ClassTree) path.getLeaf();
         clazz = gu.importComments(clazz, copy.getCompilationUnit());
         TreeMaker maker = copy.getTreeMaker();
+
         ClassTree nue = maker.Class(clazz.getModifiers(), clazz.getSimpleName(), clazz.getTypeParameters(), clazz.getExtendsClause(), clazz.getImplementsClause(), clazz.getPermitsClause(), Collections.<Tree>emptyList());
-        List<Tree> members = new ArrayList<>(clazz.getMembers().size());
-        Map<Tree, Tree> memberMap = new HashMap<>(clazz.getMembers().size());
+        List<Tree> members = new ArrayList<>();
+        Map<Tree, Tree> memberMap = new HashMap<>();
         
-        List<Tree> enumValues = new ArrayList<>();
+        List<Tree> fixedMembers = new ArrayList<>();
         for (Tree tree : clazz.getMembers()) {
-            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))) {
+            // isSynthetic does not consider record components to be synthetic
+            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))
+                    && !(tree.getKind() == Kind.VARIABLE && copy.getTreeUtilities().isRecordComponent((VariableTree)tree))) {
                 continue;
             }

Review Comment:
   thought this is lower risk than updating `isSynthetic()` although it probably should be updated at some point.
   
   inserting
   ```java
               if (path.getLeaf().getKind() == Kind.VARIABLE &&
                   path.getParentPath() != null &&
                   path.getParentPath().getLeaf().getKind() == Kind.RECORD) {
                   Set<Modifier> mods = ((VariableTree) path.getLeaf()).getModifiers().getFlags();
                   if (!mods.contains(Modifier.STATIC)) {
                       return true; // all non static record fields are synthetic
                   }
               }
   ```
   at https://github.com/apache/netbeans/blob/e38f61876b02c5f47b0a4b019426812e54c2689f/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java#L212 would work



##########
java/java.hints/test/unit/src/org/netbeans/modules/java/hints/OrganizeMembersTest.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.hints;
+
+import java.util.prefs.Preferences;
+import org.netbeans.api.editor.mimelookup.MimeLookup;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.editor.java.JavaKit;
+import org.netbeans.modules.java.hints.test.api.HintTest;
+import org.netbeans.modules.java.ui.FmtOptions;
+import org.openide.util.NbBundle;
+
+/**
+ * @author mbien
+ */
+public class OrganizeMembersTest extends NbTestCase {
+    
+    public OrganizeMembersTest(String name) {
+        super(name);
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        HintTest.create(); // initializes lookup (stolen from bugs.TinyTest)
+        MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE)
+                  .lookup(Preferences.class)
+                  .putBoolean(FmtOptions.sortMembersInGroups, true);
+    }
+
+    @Override
+    protected boolean runInEQ() {
+        // without it, hint markers are sometimes not found
+        return true;
+    }

Review Comment:
   don't know why this is needed. `OrganizeMembers` invokes `runModificationTask` which calls `copy.rewrite()` at some point. 
   
   `result.getDifferences(source.getFileObject())` is sometimes null if not on EDT.



-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


##########
java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java:
##########
@@ -151,7 +151,14 @@ public boolean isEnum(ClassTree tree) {
     public boolean isEnumConstant(VariableTree tree) {
         return (((JCTree.JCModifiers) tree.getModifiers()).flags & Flags.ENUM) != 0;
     }
-    
+
+    /**
+     * Checks whether given variable tree represents a record component.
+     */

Review Comment:
   Might be good to at least add `@since`.



-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


##########
java/java.hints/src/org/netbeans/modules/java/hints/OrganizeMembers.java:
##########
@@ -123,48 +123,41 @@ private static void doOrganizeMembers(WorkingCopy copy, TreePath path) {
         clazz = gu.importComments(clazz, copy.getCompilationUnit());
         TreeMaker maker = copy.getTreeMaker();
         ClassTree nue = maker.Class(clazz.getModifiers(), clazz.getSimpleName(), clazz.getTypeParameters(), clazz.getExtendsClause(), clazz.getImplementsClause(), Collections.<Tree>emptyList());
-        List<Tree> members = new ArrayList<>(clazz.getMembers().size());
-        Map<Tree, Tree> memberMap = new HashMap<>(clazz.getMembers().size());
+        List<Tree> members = new ArrayList<>();
+        Map<Tree, Tree> memberMap = new HashMap<>();
         
-        List<Tree> enumValues = new ArrayList<>();
+        List<Tree> fixedMembers = new ArrayList<>();
         for (Tree tree : clazz.getMembers()) {
-            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))) {
+            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))
+                    && !(tree.getKind() == Kind.VARIABLE && copy.getTreeUtilities().isRecordComponent((VariableTree)tree))) {

Review Comment:
   please note that the `!isRecordComponent()` check is currently redundant since `isSynthetic()` does currently not return true for synthetic record members, representing the record components.
   
   inserting
   ```java
               if (path.getLeaf().getKind() == Kind.VARIABLE &&
                   path.getParentPath() != null &&
                   path.getParentPath().getLeaf().getKind() == Kind.RECORD) {
                   Set<Modifier> mods = ((VariableTree) path.getLeaf()).getModifiers().getFlags();
                   if (!mods.contains(Modifier.STATIC)) {
                       return true; // all non static record fields are synthetic
                   }
               }
   ```
   to https://github.com/apache/netbeans/blob/e38f61876b02c5f47b0a4b019426812e54c2689f/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java#L212
   
   would update it but it could break other things



-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


##########
java/java.hints/test/unit/src/org/netbeans/modules/java/hints/OrganizeMembersTest.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.java.hints;
+
+import java.util.prefs.Preferences;
+import org.netbeans.api.editor.mimelookup.MimeLookup;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.editor.java.JavaKit;
+import org.netbeans.modules.java.hints.test.api.HintTest;
+import org.netbeans.modules.java.ui.FmtOptions;
+import org.openide.util.NbBundle;
+
+/**
+ * @author mbien
+ */
+public class OrganizeMembersTest extends NbTestCase {
+    
+    public OrganizeMembersTest(String name) {
+        super(name);
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        HintTest.create(); // initializes lookup (stolen from bugs.TinyTest)
+        MimeLookup.getLookup(JavaKit.JAVA_MIME_TYPE)
+                  .lookup(Preferences.class)
+                  .putBoolean(FmtOptions.sortMembersInGroups, true);
+    }
+
+    @Override
+    protected boolean runInEQ() {
+        // without it, hint markers are sometimes not found
+        return true;
+    }

Review Comment:
   don't know why this is needed. `OrganizeMembers` invokes `runModificationTask` which calls `copy.rewrite()` at some point. 
   
   `result.getDifferences(source.getFileObject())` is sometimes null if tests don't run on EDT.



-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


##########
java/java.hints/src/org/netbeans/modules/java/hints/OrganizeMembers.java:
##########
@@ -123,48 +123,41 @@ private static void doOrganizeMembers(WorkingCopy copy, TreePath path) {
         clazz = gu.importComments(clazz, copy.getCompilationUnit());
         TreeMaker maker = copy.getTreeMaker();
         ClassTree nue = maker.Class(clazz.getModifiers(), clazz.getSimpleName(), clazz.getTypeParameters(), clazz.getExtendsClause(), clazz.getImplementsClause(), Collections.<Tree>emptyList());
-        List<Tree> members = new ArrayList<>(clazz.getMembers().size());
-        Map<Tree, Tree> memberMap = new HashMap<>(clazz.getMembers().size());
+        List<Tree> members = new ArrayList<>();
+        Map<Tree, Tree> memberMap = new HashMap<>();
         
-        List<Tree> enumValues = new ArrayList<>();
+        List<Tree> fixedMembers = new ArrayList<>();
         for (Tree tree : clazz.getMembers()) {
-            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))) {
+            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))
+                    && !(tree.getKind() == Kind.VARIABLE && copy.getTreeUtilities().isRecordComponent((VariableTree)tree))) {

Review Comment:
   please note that the `!isRecordComponent()` check is currently redundant since `isSynthetic()` does currently not return true for synthetic record members, representing the record components.
   
   inserting
   ```java
               if (path.getLeaf().getKind() == Kind.VARIABLE &&
                   path.getParentPath() != null &&
                   path.getParentPath().getLeaf().getKind() == Kind.RECORD) {
                   Set<Modifier> mods = ((VariableTree) path.getLeaf()).getModifiers().getFlags();
                   if (!mods.contains(Modifier.STATIC)) {
                       return true; // all non static record fields are synthetic
                   }
               }
   ```
   to https://github.com/apache/netbeans/blob/e38f61876b02c5f47b0a4b019426812e54c2689f/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java#L212
   
   would update it but it could break other things



-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


##########
java/java.hints/src/org/netbeans/modules/java/hints/OrganizeMembers.java:
##########
@@ -123,48 +123,41 @@ private static void doOrganizeMembers(WorkingCopy copy, TreePath path) {
         clazz = gu.importComments(clazz, copy.getCompilationUnit());
         TreeMaker maker = copy.getTreeMaker();
         ClassTree nue = maker.Class(clazz.getModifiers(), clazz.getSimpleName(), clazz.getTypeParameters(), clazz.getExtendsClause(), clazz.getImplementsClause(), Collections.<Tree>emptyList());
-        List<Tree> members = new ArrayList<>(clazz.getMembers().size());
-        Map<Tree, Tree> memberMap = new HashMap<>(clazz.getMembers().size());
+        List<Tree> members = new ArrayList<>();
+        Map<Tree, Tree> memberMap = new HashMap<>();
         
-        List<Tree> enumValues = new ArrayList<>();
+        List<Tree> fixedMembers = new ArrayList<>();
         for (Tree tree : clazz.getMembers()) {
-            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))) {
+            if (copy.getTreeUtilities().isSynthetic(new TreePath(path, tree))
+                    && !(tree.getKind() == Kind.VARIABLE && copy.getTreeUtilities().isRecordComponent((VariableTree)tree))) {

Review Comment:
   please note that the `!isRecordComponent()` check is currently redundant since `isSynthetic()` does currently not return true for synthetic record members, representing the record components.
   
   inserting
   ```
               if (path.getLeaf().getKind() == Kind.VARIABLE &&
                   path.getParentPath() != null &&
                   path.getParentPath().getLeaf().getKind() == Kind.RECORD) {
                   Set<Modifier> mods = ((VariableTree) path.getLeaf()).getModifiers().getFlags();
                   if (!mods.contains(Modifier.STATIC)) {
                       return true; // all non static record fields are synthetic
                   }
               }
   ```
   to https://github.com/apache/netbeans/blob/e38f61876b02c5f47b0a4b019426812e54c2689f/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java#L212
   
   would update it but it could break other things



-- 
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] Organize Members hint: don't rearrange record components [netbeans]

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


-- 
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