You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@netbeans.apache.org by GitBox <gi...@apache.org> on 2017/11/27 10:31:26 UTC
[GitHub] sdedic closed pull request #258: Handle implicitly static interface field declarations
sdedic closed pull request #258: Handle implicitly static interface field declarations
URL: https://github.com/apache/incubator-netbeans/pull/258
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/java.hints/src/org/netbeans/modules/java/hints/introduce/Flow.java b/java.hints/src/org/netbeans/modules/java/hints/introduce/Flow.java
index 348508c37..53db6daef 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/introduce/Flow.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/introduce/Flow.java
@@ -1385,7 +1385,7 @@ public Boolean visitClass(ClassTree node, ConstructorData p) {
List<Tree> instanceInitializers = new ArrayList<Tree>(node.getMembers().size());
List<MethodTree> constructors = new ArrayList<MethodTree>(node.getMembers().size());
List<Tree> others = new ArrayList<Tree>(node.getMembers().size());
-
+
for (Tree member : node.getMembers()) {
if (member.getKind() == Kind.BLOCK) {
if (((BlockTree) member).isStatic()) {
@@ -1394,10 +1394,31 @@ public Boolean visitClass(ClassTree node, ConstructorData p) {
instanceInitializers.add(member);
}
} else if (member.getKind() == Kind.VARIABLE && ((VariableTree) member).getInitializer() != null) {
- if (((VariableTree) member).getModifiers().getFlags().contains(Modifier.STATIC)) {
- staticInitializers.add((VariableTree) member);
+ // If the field is either marked static or is implicitly static,
+ // then add to staticInitializers; otherwise add to instanceInitializers.
+ //
+ // ?8.9.3 Enum Members
+ // "For each enum constant c declared in the body of the declaration of E,
+ // E has an implicitly declared public static final field of type E that has
+ // the same name as c. The field has a variable initializer which instantiates
+ // E and passes any arguments of c to the constructor chosen for E."
+ //
+ // ?9.3 Field (Constant) Declarations
+ // "Every field declaration in the body of an interface is implicitly public,
+ // static, and final. It is permitted to redundantly specify any or all of
+ // these modifiers for such fields."
+ if (node.getKind() == Kind.INTERFACE || ((VariableTree) member).getModifiers().getFlags().contains(Modifier.STATIC)) {
+ {
+ final Element e;
+ assert (e = info.getTrees().getElement(TreePath.getPath(getCurrentPath(), member))) == null || e.getModifiers().contains(Modifier.STATIC);
+ }
+ staticInitializers.add(member);
} else {
- instanceInitializers.add((VariableTree) member);
+ {
+ final Element e;
+ assert (e = info.getTrees().getElement(TreePath.getPath(getCurrentPath(), member))) == null || !e.getModifiers().contains(Modifier.STATIC);
+ }
+ instanceInitializers.add(member);
}
} else if (isConstructor(new TreePath(getCurrentPath(), member))) {
constructors.add((MethodTree) member);
@@ -1405,7 +1426,7 @@ public Boolean visitClass(ClassTree node, ConstructorData p) {
others.add(member);
}
}
-
+
Map< Element, State> oldVariable2State = variable2State;
variable2State = new HashMap<>(variable2State);
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/UnusedAssignmentOrBranchTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/UnusedAssignmentOrBranchTest.java
index 381a6168b..71a0f5e78 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/UnusedAssignmentOrBranchTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/UnusedAssignmentOrBranchTest.java
@@ -179,4 +179,36 @@ public void testCompoundAssignment267508Negative() throws Exception {
.run(UnusedAssignmentOrBranch.class)
.assertWarnings();
}
+
+ // #271736 - Wrong "not used" hint
+ public void testImplicitlyStaticFieldWithInitializer() throws Exception {
+ HintTest
+ .create()
+ .input("package test;\n" +
+ "\n" +
+ "import java.util.function.Consumer;\n" +
+ "\n" +
+ "public interface Test {\n" +
+ " Consumer<CharSequence> CONSUMER = new Consumer<CharSequence>() {\n" +
+ " @Override\n" +
+ " public void accept(CharSequence cs) {\n" +
+ " final String str = (String) cs;\n" +
+ " System.out.println(str.length());\n" +
+ " }\n" +
+ " };\n" +
+ "}" +
+ "\n" +
+ "enum SomeEnum {\n" +
+ " ALT() {\n" +
+ " public void doSomething(CharSequence cs) {\n" +
+ " final String str = (String) cs;\n" +
+ " System.out.println(str.length());\n" +
+ " }\n" +
+ " };\n" +
+ "\n" +
+ " private SomeEnum() { }\n" +
+ "}")
+ .run(UnusedAssignmentOrBranch.class)
+ .assertWarnings();
+ }
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services