Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

GROOVY-5212: Stub generator should not output "final" for enums

  • Loading branch information...
commit 5e2c99d43a9452564f28ebb28c611fb718fbfdd3 1 parent 8576aa8
@melix melix authored
View
3  src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -183,7 +183,8 @@ protected void addDefaultConstructor(ClassNode node) {
boolean isAnnotationDefinition = classNode.isAnnotationDefinition();
printAnnotations(out, classNode);
printModifiers(out, classNode.getModifiers()
- & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0));
+ & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0)
+ & ~(isEnum ? Opcodes.ACC_FINAL : 0));
if (isInterface) {
if (isAnnotationDefinition) {
View
88 src/test/groovy/bugs/Groovy5212Bug.groovy
@@ -0,0 +1,88 @@
+package groovy.bugs
+
+import org.codehaus.groovy.ast.ClassNode
+import org.objectweb.asm.Opcodes
+import org.codehaus.groovy.tools.javac.JavaStubGenerator
+import org.codehaus.groovy.ast.ModuleNode
+import org.codehaus.groovy.ast.CompileUnit
+import org.codehaus.groovy.antlr.EnumHelper
+
+class Groovy5212Bug extends GroovyTestCase implements Opcodes {
+ File outputDir
+
+ @Override
+ protected void setUp() {
+ super.setUp()
+ outputDir = File.createTempFile("stub","groovy")
+ outputDir.mkdirs()
+ }
+
+ @Override
+ protected void tearDown() {
+ super.tearDown()
+ outputDir.delete()
+ }
+
+ void testGeneratedEnumJavaStubShouldNotHaveFinalModifier() {
+ ClassNode cn = EnumHelper.makeEnumNode("MyEnum", ACC_PUBLIC, ClassNode.EMPTY_ARRAY, null)
+ ModuleNode module = new ModuleNode(new CompileUnit(null,null))
+ cn.setModule(module)
+ StringWriter wrt = new StringWriter()
+ PrintWriter out = new PrintWriter(wrt)
+ JavaStubGenerator generator = new StringJavaStubGenerator(outputDir, out)
+ generator.generateClass(cn)
+
+ String stub = wrt.toString()
+ assert !(stub =~ /final/)
+ }
+
+ void testShouldNotAllowExtendingEnum() {
+ shouldFail {
+ assertScript '''
+ enum MyEnum { a }
+ enum MyExtendedEnum extends MyEnum { b }
+ '''
+ }
+ }
+
+ void testShouldNotAllowExtendingEnumWithClass() {
+ shouldFail {
+ assertScript '''
+ enum MyEnum { a }
+ class MyExtendedEnum extends MyEnum { }
+ new MyExtendedEnum()
+ '''
+ }
+ }
+
+ /**
+ * Helper class, which generates code in a string instead of an output file.
+ */
+ private final static class StringJavaStubGenerator extends JavaStubGenerator {
+ PrintWriter out
+ StringJavaStubGenerator(File outFile, PrintWriter out) {
+ super(outFile)
+ this.out = out
+ }
+ public void generateClass(ClassNode classNode) throws FileNotFoundException {
+
+
+ try {
+ String packageName = classNode.getPackageName();
+ if (packageName != null) {
+ out.println("package " + packageName + ";\n");
+ }
+
+ super.printImports(out, classNode);
+ super.printClassContents(out, classNode);
+
+ } finally {
+ try {
+ out.close();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.