Repository: groovy Updated Branches: refs/heads/GROOVY_2_4_X 10632b46c -> aacd53967
GROOVY-7747: Generated Java stub for enum with abstract method is invalid (closes #254) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/aacd5396 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/aacd5396 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/aacd5396 Branch: refs/heads/GROOVY_2_4_X Commit: aacd53967f3d2cae0d521ef684981290df2c1590 Parents: 10632b4 Author: paulk <pa...@asert.com.au> Authored: Fri Feb 5 21:07:05 2016 +1000 Committer: paulk <pa...@asert.com.au> Committed: Sat Feb 6 16:37:25 2016 +1000 ---------------------------------------------------------------------- .../groovy/tools/javac/JavaStubGenerator.java | 8 +-- .../tools/stubgenerator/Groovy7747Bug.groovy | 63 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/aacd5396/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java index 384381e..053e08b 100644 --- a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java +++ b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java @@ -196,12 +196,12 @@ public class JavaStubGenerator { currentModule = classNode.getModule(); boolean isInterface = isInterfaceOrTrait(classNode); - boolean isEnum = (classNode.getModifiers() & Opcodes.ACC_ENUM) != 0; + boolean isEnum = classNode.isEnum(); boolean isAnnotationDefinition = classNode.isAnnotationDefinition(); printAnnotations(out, classNode); printModifiers(out, classNode.getModifiers() & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0) - & ~(isEnum ? Opcodes.ACC_FINAL : 0)); + & ~(isEnum ? Opcodes.ACC_FINAL | Opcodes.ACC_ABSTRACT : 0)); if (isInterface) { if (isAnnotationDefinition) { @@ -598,7 +598,7 @@ public class JavaStubGenerator { if (isDefaultTraitImpl(methodNode)) { modifiers ^= Opcodes.ACC_ABSTRACT; } - printModifiers(out, modifiers); + printModifiers(out, modifiers & ~(clazz.isEnum() ? Opcodes.ACC_ABSTRACT : 0)); } printGenericsBounds(out, methodNode.getGenericsTypes()); @@ -622,7 +622,7 @@ public class JavaStubGenerator { if (Traits.isTrait(clazz)) { out.println(";"); - } else if (isAbstract(methodNode)) { + } else if (isAbstract(methodNode) && !clazz.isEnum()) { if (clazz.isAnnotationDefinition() && methodNode.hasAnnotationDefault()) { Statement fs = methodNode.getFirstStatement(); if (fs instanceof ExpressionStatement) { http://git-wip-us.apache.org/repos/asf/groovy/blob/aacd5396/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy7747Bug.groovy ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy7747Bug.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy7747Bug.groovy new file mode 100644 index 0000000..7c551b9 --- /dev/null +++ b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy7747Bug.groovy @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.groovy.tools.stubgenerator + +/** + * Test that enums with an abstract method are compiled successfully. + */ +class Groovy7747Bug extends StringSourcesStubTestCase { + + Map<String, String> provideSources() { + [ + 'Main.java' : ''' + import enums.EnumWithAbstractMethod; + public class Main { + public static void main(String[] args) { + System.out.println(EnumWithAbstractMethod.values()); + System.out.println(EnumWithAbstractMethod.ONE.getInt()); + } + } + ''', + 'enums/EnumWithAbstractMethod.groovy' : ''' + package enums + enum EnumWithAbstractMethod { + ONE { + @Override + int getInt() { + return 1 + } + }, + TWO { + @Override + int getInt() { + return 2 + } + } + abstract int getInt() + } + ''' + ] + } + + void verifyStubs() { + def stubSource = stubJavaSourceFor('enums.EnumWithAbstractMethod') + assert stubSource.contains('enum EnumWithAbstractMethod') + assert !stubSource.matches('abstract.*enum') + } +}