lishunli / projectlombok

Automatically exported from code.google.com/p/projectlombok
0 stars 0 forks source link

Compilation error "call to super not allowed in enum constructor" at enum #586

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create an enum class (see the example below)
2. Compile with javac (or using maven)

What is the expected output? What do you see instead?
The compilation throws an error: call to super not allowed in enum constructor

What version of the product are you using? On what operating system?
0.12.0 at Windows and oracle javac compiler

Please provide any additional information below.

Example of a enum that matches the case above explained.

@Getter
@RequiredArgsConstructor
@ExtensionMethod({ MyUtilityClassWithExtensionMethods.class })
public enum MyClass {

    CONSTANT("some string info like a description") {
        @Override
        public boolean method(String value) {
            // using the attribute
            System.out.println(getAttributeToBeInitialized().someExtensionMethod());
            // the error occurs even if you do not use the extension method
            return true;
        }
    };

    final String attributeToBeInitialized;

    public boolean method(String value) {
        return false;
    }

}

public class MyUtilityClassWithExtensionMethods {

    public static String someExtensionMethod(String string) {
        return string;
    }

}

Thanks.

Original issue reported on code.google.com by jrdalpra on 8 Oct 2013 at 8:34

GoogleCodeExporter commented 9 years ago
Thanks for finding this; we call attribution for @ExtensionMethod and a few 
other things; this adds implicit super() calls all over the place (it's hard to 
find out which source is exactly affected, unfortunately, it's not just the 
code we attribute, it also happens to code referenced by the code we 
attribute). We thought it didn't matter much (other than generate extra lines 
in delombok, where we just remove all super() calls), but it turns out it 
matters here.

Fixing this is going to be extremely difficult, unfortunately. Awesome find, 
though :)

NB: For future volunteers: The key aspects are @ExtensionMethod, a 'complex' 
enum value (with its own block; attributing this vardecl is what generates the 
superfluous super() call), and at least 1 method call, to anything, in that 
block, to trigger attribution to check if @ExtensionMethod needs to go to work. 
It doesn't have to be an actual extensionmethod call, even 
System.out.println(); will do.

Original comment by reini...@gmail.com on 10 Oct 2013 at 9:03

GoogleCodeExporter commented 9 years ago
Thanks for you reply

Original comment by jrdalpra on 11 Oct 2013 at 12:19

GoogleCodeExporter commented 9 years ago
This looks suspiciously much like issue 276.

Original comment by askon...@gmail.com on 11 Nov 2013 at 10:35

GoogleCodeExporter commented 9 years ago
Hello,

I got the same compilation error without using the @ExtensionMethod annotation.
I stripped down to implementation to the following code where the combination 
of overriding an abstract member function and usage of 'val' within this 
function seems to trigger the error.

lombok version: 0.12.0
javac version: 1.6.0_35

import lombok.val;

public enum MyEnum  {
  VAL1("title1") {
    @Override 
    public String getValue() {
      // usage of 'val' here causes the compilation error
      val s = "value1";
      return s;
    }
  };

  private final String title;

  private MyEnum(String title) 
  {
    this.title = title;
  }

  public abstract String getValue();
}

Btw. - many thanks to the lombok project - it's really great!

Original comment by tom1...@gmail.com on 5 Jan 2014 at 9:26