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

Compile error - No such class: jdk.Profile+Annotation #1231

Closed
walery opened this issue Mar 3, 2021 · 3 comments
Closed

Compile error - No such class: jdk.Profile+Annotation #1231

walery opened this issue Mar 3, 2021 · 3 comments
Assignees
Labels
Milestone

Comments

@walery
Copy link

walery commented Mar 3, 2021

A few days ago, I had faced an issue that only appears under JDK >=11 but not under JDK 8.
The error was:

Groovy: compiler error: exception in phase 'canonicalization' in source unit '/private/tmp/j11i/groovy-eclipse-compiler-jdk11-issue/src/main/java/compilerissue/Foo.groovy' No such class: jdk.Profile+Annotation -- JDTClassNode.getTypeClass() cannot locate it using transform loader

I was not able to nail down the issue itself. To share the problem, I've isolated and shrunk the project as much as I could. A full example with one small class and one pom.xml is in this repo:

https://github.com/walery/groovy-eclipse-compiler-jdk11-issue

The main purpose is to share the problem with you. I've fixed it by switching to gmavenplus plugin.

@walery
Copy link
Author

walery commented Mar 3, 2021

BTW: This issue was the reason why I asked in #1223 (comment) about a newer version of the compiler (I hoped a newer version could have a fix 😄).

@eric-milles
Copy link
Member

eric-milles commented Mar 3, 2021

Update: I tried a number of scenarios and found that using Java 9+ to build with a target of Java 8 can work with the current groovy-eclipse-batch releases.

The parent POM here sets the release config value when maven is run with Java 9+. Clearing this value allows the sample project to build successfully:

  <properties>
    <maven.compiler.release></maven.compiler.release>
  </properties>

You could also choose to run the compiler in a separate process (Java 8 or Java 9+), which also works if the release value is cleared:

  <properties>
    <maven.compiler.fork>true</maven.compiler.fork>
    <maven.compiler.release></maven.compiler.release>
  </properties>

Also, there is a newer batch compiler artifact (3.0.7-02) than the one you are using (3.0.7-01). Batch compiler 2.5.14-03 and 3.0.8-02 will deal with the jdk.Profile+Annotation directly -- I am working out publishing due to recent bintray shutdown.

@eric-milles
Copy link
Member

eric-milles commented Mar 4, 2021

In this case, there is an AST transform that is calling ClassNode#getTypeClass, which is an unsafe call to make (see below). Within Groovy-Eclipse, we create JDTClassNode instances for the Java references that exist between sources and classes or other sources. JDTClassNode does not have a Class by default, but tries to accommodate the needs of the compiler and its AST transformations by using Class#forName to try and get one from the current context.

    @Override
    public Class getTypeClass() {
        if (hasClass()) return clazz;
        throw new MissingClassException(this, "-- JDTClassNode.getTypeClass() cannot locate it using transform loader");
    }

    @Override
    public boolean hasClass() {
        if (clazz == null && !unfindable) {
            // Some AST transforms are written such that they refer to typeClass on a ClassNode.
            // This is not available within Eclipse. However, we can support it in a rudimentary
            // fashion by attempting a class load using the transform loader (if it's available).
            ClassLoader transformLoader = resolver.compilationUnit.getTransformLoader();
            if (transformLoader != null) {
                try {
                    clazz = Class.forName(getName(), false, transformLoader);
                } catch (ReflectiveOperationException | LinkageError e) {
                    unfindable = true;
                }
            }
        }
        return (clazz != null);
    }

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants