mbien commented on code in PR #9764:
URL: https://github.com/apache/netbeans/pull/9764#discussion_r4414551185


##########
java/java.hints/test/unit/src/org/netbeans/modules/java/hints/encapsulation/ReturnEncapsulationTest.java:
##########
@@ -85,6 +85,20 @@ public void testReturnUnmodifiableCollectionField195557() 
throws Exception {
                 .run(ReturnEncapsulation.class)
                 .assertWarnings();
     }
+    
+    public void testReturnArrayFieldWithZeroLength() throws Exception {
+        HintTest
+                .create()
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "    private final int[] l = {};\n" +

Review Comment:
   nitpick: multiline Strings for new tests please ;)
   
   the other two cases are not covered atm i think, would be good to add them 
too:
   ```java
   private final int[] l = new int[]{};
   private final int[] l = new int[0];
   ```
   
   
   feel free to squash with existing commit and force push



##########
java/java.hints/src/org/netbeans/modules/java/hints/encapsulation/ReturnEncapsulation.java:
##########
@@ -214,13 +217,30 @@ private static ErrorDescription create(final HintContext 
ctx,
             TreePath decl = ctx.getInfo().getTrees().getPath(exprElement);
 
             if (decl != null && decl.getLeaf().getKind() == Kind.VARIABLE && 
((VariableTree) decl.getLeaf()).getInitializer() != null) {
-                TreePath init = new TreePath(decl, ((VariableTree) 
decl.getLeaf()).getInitializer());
+                ExpressionTree etInit = ((VariableTree) 
decl.getLeaf()).getInitializer();
+                TreePath init = new TreePath(decl,etInit);

Review Comment:
   space after `,`
   
   you'll laugh but we had a bug because of this once. Committer meant to 
remove the last parameter of a method and misread the comma as dot -> removed 
two parameters. It matched an overloaded method, compiled fine and made it into 
the repo as bug.



##########
java/java.hints/src/org/netbeans/modules/java/hints/encapsulation/ReturnEncapsulation.java:
##########
@@ -214,13 +217,30 @@ private static ErrorDescription create(final HintContext 
ctx,
             TreePath decl = ctx.getInfo().getTrees().getPath(exprElement);
 
             if (decl != null && decl.getLeaf().getKind() == Kind.VARIABLE && 
((VariableTree) decl.getLeaf()).getInitializer() != null) {
-                TreePath init = new TreePath(decl, ((VariableTree) 
decl.getLeaf()).getInitializer());
+                ExpressionTree etInit = ((VariableTree) 
decl.getLeaf()).getInitializer();
+                TreePath init = new TreePath(decl,etInit);
 
                 for (String p : UNMODIFIABLE) {
                     if (MatcherUtilities.matches(ctx, init, p)) {
                         return null;
                     }
                 }
+                // check arrays with zero length or empty initializer i.e new 
int[0] or new int[] {}
+                if (etInit.getKind() == Tree.Kind.NEW_ARRAY) {
+                    NewArrayTree newArray = (NewArrayTree) etInit;
+                    List<? extends ExpressionTree> dimensions = 
newArray.getDimensions();
+                    if (dimensions != null &&
+                        !dimensions.isEmpty() &&
+                        dimensions.get(0).getKind() == Tree.Kind.INT_LITERAL &&
+                        
Integer.valueOf(0).equals(((LiteralTree)dimensions.get(0)).getValue())) {
+                        return null;

Review Comment:
   2x `dimensions.getFirst()`



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

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

Reply via email to