Semyon Danilov
Semyon Danilov

Reputation: 1773

Maven compiler plugin error: can't access enum (bad signature, bad class)

I am using maven-compiler-plugin:2.3.2 and every time I make changes in classes which have an enum (ContentType) in imports, I need to make clean, otherwise it gives me:

ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project wp2: Compilation failure
[ERROR] /home/semyon/development/.../ContentManager.java:[15,46] error: cannot access ContentType
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project wp2: Compilation failure
/home/semyon/development/.../ContentManager.java:[15,46] error: cannot access ContentType

at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.MojoExecutor.executeForkedExecutions(MojoExecutor.java:364)
...

ContentType is enum and looks like this:

import org.jetbrains.annotations.NotNull;

public enum ContentType {

    ...; 

    private final String title;

    private final boolean hasJad;

    private final CoreType coreType;

    private final String[] searchKeywords;



    ContentType(@NotNull String title, CoreType coreType, boolean hasJad, String[] searchKeywords) {
        this.title = title;
        this.coreType = coreType;

        this.hasJad = hasJad;
        this.searchKeywords = searchKeywords;
    }

    @NotNull
    public String getTitle() {
         return title;
    }

    @NotNull
    public String getName() {
        return name();
    }

    @NotNull
    public CoreType getCoreType() {
        return coreType;
    }

    public enum CoreType {

         ...;

        private String title;

        CoreType(String title) {
            this.title = title;
        }

        public String getTitle() {
            return title;
        }

    }
}

UPD1, project structure:

        /wp2
             /core
                  /cpe
                     /widget
                           /ContentManager.java
                  /cdr
                     /entities
                           /ContentType.java

UPD 2:

ContentManager.java:[15,46] is import wp2.core.cdr.entities.ContentType;

UPD 3: Modern compiler will show bad class and bad signature errors as well

Upvotes: 3

Views: 2314

Answers (2)

Semyon Danilov
Semyon Danilov

Reputation: 1773

I finally found an answer

The error is in the costructor:

ContentType(@NotNull String title...

Constructors in enum must not have annotations in it, as javac is bugged. Javac stores incorrect signature for enum constructor (the one you writes, and not the one actually is used - it has two additional params as I recall). When javac verifies the signature it sees annotated parameter and in my case that was the first parameter. BUT in the actual signature (String name, int ordinal, String title, CoreType coreType, boolean hasJad, String[] searchKeywords, two first params are added by enum -> Enum translation) title is only third parameter and first parameter is name which is not annotated and javac thinks that class is incorrect.

tl;dr remove annotation from constructor, javac is buggy

Upvotes: 13

Aaron Digulla
Aaron Digulla

Reputation: 328614

Maybe a bug in the code which tries to determine which classes need to be compiled after a change.

Try the latest version of the Maven compiler plugin. The version number is on the page behind the link, in the header on the right, below the Maven logo (3.2 at the time of writing).

Upvotes: 1

Related Questions