Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

bug: Invalid enum members modifiers #2630

Merged
merged 3 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,9 @@ public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {
}
field.setSimpleName(CharOperation.charToString(fieldDeclaration.name));
if (fieldDeclaration.binding != null) {
if (fieldDeclaration.binding.declaringClass != null && fieldDeclaration.binding.declaringClass.isEnum()) {
if (fieldDeclaration.binding.declaringClass != null
&& fieldDeclaration.binding.declaringClass.isEnum()
&& field instanceof CtEnumValue) {
//enum values take over visibility from enum type
//JDT compiler has a bug that enum values are always public static final, even for private enum
field.setExtendedModifiers(getModifiers(fieldDeclaration.binding.declaringClass.modifiers, true, false));
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/enums/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import spoon.reflect.declaration.CtEnumValue;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
Expand All @@ -18,6 +19,7 @@
import spoon.test.enums.testclasses.Foo;
import spoon.test.enums.testclasses.NestedEnums;
import spoon.test.enums.testclasses.Regular;
import spoon.test.enums.testclasses.EnumWithMembers;
import spoon.testing.utils.ModelUtils;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -170,4 +172,21 @@ public void testEnumValue() {
}
}
}

@Test
public void testEnumMembersModifiers() throws Exception {
// contract: enum members should have correct modifiers
final Factory factory = build(EnumWithMembers.class);
CtModel model = factory.getModel();

CtField lenField = model.getElements(new TypeFilter<>(CtField.class)).stream()
.filter(p -> p.getSimpleName().equals("len"))
.findFirst().get();

assertTrue(lenField.isPrivate());
assertTrue(lenField.isStatic());
assertFalse(lenField.isFinal());
assertFalse(lenField.isPublic());
assertFalse(lenField.isProtected());
}
}
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/enums/testclasses/EnumWithMembers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package spoon.test.enums.testclasses;

public enum EnumWithMembers {
ONE,
TWO,
THREE;

private static int len = -1;

public static void f() {
len = 44;
}
}