Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

review: fix: visibility of enum values #1999

Merged
merged 2 commits into from
May 21, 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
8 changes: 7 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,13 @@ public boolean visit(FieldDeclaration fieldDeclaration, MethodScope scope) {

Set<CtExtendedModifier> modifierSet = new HashSet<>();
if (fieldDeclaration.binding != null) {
field.setExtendedModifiers(getModifiers(fieldDeclaration.binding.modifiers, true, false));
if (fieldDeclaration.binding.declaringClass != null && fieldDeclaration.binding.declaringClass.isEnum()) {
//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));
} else {
field.setExtendedModifiers(getModifiers(fieldDeclaration.binding.modifiers, true, false));
}
}
for (CtExtendedModifier extendedModifier : getModifiers(fieldDeclaration.modifiers, false, false)) {
field.addModifier(extendedModifier.getKind()); // avoid to keep implicit AND explicit modifier of the same kind.
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/spoon/test/enums/EnumsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.test.annotation.AnnotationTest;
import spoon.test.enums.testclasses.Burritos;
import spoon.test.enums.testclasses.Foo;
import spoon.test.enums.testclasses.NestedEnums;
import spoon.testing.utils.ModelUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class EnumsTest {

@Test
Expand Down Expand Up @@ -70,4 +77,33 @@ public void testGetAllMethods() throws Exception {
assertTrue(burritos.getAllMethods().contains(name));
}

@Test
public void testNestedPrivateEnumValues() throws Exception {
// contract: enum values have correct modifiers
CtType<?> ctClass = ModelUtils.buildClass(NestedEnums.class);
{
CtEnum<?> ctEnum = ctClass.getNestedType("PrivateENUM");
assertEquals(asSet(ModifierKind.PRIVATE), ctEnum.getModifiers());
assertEquals(asSet(ModifierKind.PRIVATE, ModifierKind.STATIC, ModifierKind.FINAL), ctEnum.getField("VALUE").getModifiers());
}
{
CtEnum<?> ctEnum = ctClass.getNestedType("PublicENUM");
assertEquals(asSet(ModifierKind.PUBLIC), ctEnum.getModifiers());
assertEquals(asSet(ModifierKind.PUBLIC, ModifierKind.STATIC, ModifierKind.FINAL), ctEnum.getField("VALUE").getModifiers());
}
{
CtEnum<?> ctEnum = ctClass.getNestedType("ProtectedENUM");
assertEquals(asSet(ModifierKind.PROTECTED), ctEnum.getModifiers());
assertEquals(asSet(ModifierKind.PROTECTED, ModifierKind.STATIC, ModifierKind.FINAL), ctEnum.getField("VALUE").getModifiers());
}
{
CtEnum<?> ctEnum = ctClass.getNestedType("PackageProtectedENUM");
assertEquals(asSet(), ctEnum.getModifiers());
assertEquals(asSet(ModifierKind.STATIC, ModifierKind.FINAL), ctEnum.getField("VALUE").getModifiers());
}
}

private <T> Set<T> asSet(T... values) {
return new HashSet<>(Arrays.asList(values));
}
}
17 changes: 17 additions & 0 deletions src/test/java/spoon/test/enums/testclasses/NestedEnums.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package spoon.test.enums.testclasses;

public class NestedEnums {

private enum PrivateENUM {
VALUE
}
public enum PublicENUM {
VALUE
}
protected enum ProtectedENUM {
VALUE
}
enum PackageProtectedENUM {
VALUE
}
}