Skip to content

Commit

Permalink
JDT unable to detect variable reference errors in nested enum with an…
Browse files Browse the repository at this point in the history
… AnonymousClassDeclaration(#3078)

* Fixes #1368
  • Loading branch information
srikanth-sankaran authored Oct 14, 2024
1 parent f767688 commit dbd7428
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,19 @@ private void checkAndSetModifiers() {
if (compilerOptions().complianceLevel < ClassFileConstants.JDK9)
modifiers |= ClassFileConstants.AccFinal;
// set AccEnum flag for anonymous body of enum constants
if (this.referenceContext.allocation.type == null)
if (this.referenceContext.allocation.type == null) {
modifiers |= ClassFileConstants.AccEnum;
// 8.1.1.4 local enum classes are implicitly static - we can't trust isLocalType() which answers true for all anonymous types.
Scope scope = this;
while ((scope = scope.parent) != null) {
if (scope instanceof MethodScope methodScope) {
if (methodScope.referenceContext instanceof TypeDeclaration)
continue;
modifiers |= ClassFileConstants.AccStatic;
break;
}
}
}
} else if (this.parent.referenceContext() instanceof TypeDeclaration) {
TypeDeclaration typeDecl = (TypeDeclaration) this.parent.referenceContext();
if (TypeDeclaration.kind(typeDecl.modifiers) == TypeDeclaration.INTERFACE_DECL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3891,24 +3891,40 @@ public void test112() {
}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=93789
public void test113() {
if (this.complianceLevel >= ClassFileConstants.JDK16) {
return;
}
this.runNegativeTest(
new String[] {
"X.java",
"enum BugDemo {\n" +
" FOO() {\n" +
" static int bar;\n" +
" }\n" +
"}\n",
},
"----------\n" +
"1. ERROR in X.java (at line 3)\n" +
" static int bar;\n" +
" ^^^\n" +
"The field bar cannot be declared static in a non-static inner type, unless initialized with a constant expression\n" +
"----------\n");
if (this.complianceLevel < ClassFileConstants.JDK16)
this.runNegativeTest(
new String[] {
"X.java",
"enum BugDemo {\n" +
" FOO() {\n" +
" static int bar;\n" +
" }\n" +
"}\n" +
"public class X {\n" +
" public static void main(String [] args) {}\n" +
"}\n",
},
"----------\n" +
"1. ERROR in X.java (at line 3)\n" +
" static int bar;\n" +
" ^^^\n" +
"The field bar cannot be declared static in a non-static inner type, unless initialized with a constant expression\n" +
"----------\n");
else
this.runConformTest(
new String[] {
"X.java",
"enum BugDemo {\n" +
" FOO() {\n" +
" static int bar;\n" +
" }\n" +
"}\n" +
"public class X {\n" +
" public static void main(String [] args) {}\n" +
"}\n",
},
"");

}
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=99428 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=99655
public void test114() {
Expand Down Expand Up @@ -7346,4 +7362,75 @@ enum Inner extends com.test.Option {
"com.test.Option cannot be resolved to a type\n" +
"----------\n");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1368
// JDT unable to detect variable reference errors in nested enum with an AnonymousClassDeclaration
public void testGHIssue1368() {
if (this.complianceLevel < ClassFileConstants.JDK16)
return;
this.runNegativeTest(new String[] {
"X.java",
"""
public class X {
static int f() {
int h = 20;
enum ENUM_C {
INT_C () {
@Override
void k() {
System.out.println(h + "null");
}
};
void k() {
System.out.println(h);
}
}
ENUM_C i = ENUM_C.INT_C;
i.k();
return 20;
}
}
class C {
int h = 20;
enum ENUM_C {
INT_C () {
@Override
void k() {
System.out.println(h + "null");
}
};
void k() {
System.out.println(h);
}
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 8)\n" +
" System.out.println(h + \"null\");\n" +
" ^\n" +
"Cannot make a static reference to the non-static variable h\n" +
"----------\n" +
"2. ERROR in X.java (at line 13)\n" +
" System.out.println(h);\n" +
" ^\n" +
"Cannot make a static reference to the non-static variable h\n" +
"----------\n" +
"3. ERROR in X.java (at line 31)\n" +
" System.out.println(h + \"null\");\n" +
" ^\n" +
"Cannot make a static reference to the non-static field h\n" +
"----------\n" +
"4. ERROR in X.java (at line 36)\n" +
" System.out.println(h);\n" +
" ^\n" +
"Cannot make a static reference to the non-static field h\n" +
"----------\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5945,7 +5945,7 @@ public void test151() throws Exception {
+ " [inner class info: #1 p/X$1E, outer class info: #0\n"
+ " inner name: #54 E, accessflags: 17416 abstract static],\n"
+ " [inner class info: #14 p/X$1E$1, outer class info: #0\n"
+ " inner name: #0, accessflags: 16384 default]\n"
+ " inner name: #0, accessflags: 16392 static]\n"
+ " Enclosing Method: #48 #50 p/X.main([Ljava/lang/String;)V\n"
+ "\n"
+ "Nest Host: #48 p/X\n"
Expand Down

0 comments on commit dbd7428

Please sign in to comment.