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

ClassCastException for Lombok 1.6.22 and Gradle 4.8 #1716

Closed
pkubowicz opened this issue Jun 5, 2018 · 13 comments · Fixed by #1718
Closed

ClassCastException for Lombok 1.6.22 and Gradle 4.8 #1716

pkubowicz opened this issue Jun 5, 2018 · 13 comments · Fixed by #1718

Comments

@pkubowicz
Copy link

pkubowicz commented Jun 5, 2018

To reproduce:

Run gradle init --type java-library and modify generated project:

build.gradle

plugins { id 'java-library' }

dependencies {
    compileOnly 'org.projectlombok:lombok:1.16.22'
    annotationProcessor 'org.projectlombok:lombok:1.16.22'
}

compileJava {
    options.incremental = true
}

repositories {
    jcenter()
}

Foo.java

import lombok.Data;

@Data
public class Foo {
    private final boolean historical;
}

Running gradle build causes error:

> Task :compileJava FAILED
src/main/java/Library.java:4: warning: lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation: java.lang.ClassCastException: org.gradle.api.internal.tasks.compile.processing.IncrementalFiler cannot be cast to com.sun.tools.javac.processing.JavacFiler
public class Library {
       ^
        at lombok.javac.apt.LombokProcessor.getJavacFiler(LombokProcessor.java:447)
        at lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:90)
        at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:112)
        at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:165)
        at lombok.launch.AnnotationProcessorHider$AnnotationProcessor.init(AnnotationProcessor.java:73)
        at org.gradle.api.internal.tasks.compile.processing.DelegatingProcessor.init(DelegatingProcessor.java:57)
        at org.gradle.api.internal.tasks.compile.processing.IsolatingProcessor.init(IsolatingProcessor.java:44)

Note that the error appears in file not annotated with Lombok annotations (created by gradle init).

Workaround - either:

  1. Downgrade Lombok to 1.16.20 (implicitly disables incremental compilation); or
  2. Downgrade Gradle to 4.7
@victorwss
Copy link
Contributor

victorwss commented Jun 5, 2018

The title is wrong. It is not 1.6.12, it is 1.16.22.

What happens with 1.18.0?

@pkubowicz pkubowicz changed the title ClassCastException for Lombok 1.6.12 and Gradle 4.8 ClassCastException for Lombok 1.6.22 and Gradle 4.8 Jun 5, 2018
@pkubowicz
Copy link
Author

1.18.0 causes the same error with the same stacktrace.

@arana198
Copy link

arana198 commented Jun 5, 2018

I have a similar issue on all lombok "@"Data annotation. Example error:

../user/service/impl/UserServiceImpl.java:178: error: cannot find symbol
    userBO.getRoles()
          ^
  symbol:   method getRoles()
  location: variable userBO of type UserBO

I'm using:

  • Java 10
  • Lombok 1.18.0 (also tried 1.16.22)
  • Gradle 4.8 (also tried with 4.7)

PS: I have requires static lombok in module-info.java and IDE does not show any error

@ryonday
Copy link

ryonday commented Jun 5, 2018

With:

  • Oracle Java 9.0.4,
  • Gradle 4.8
  • Lombok 1.6.22 OR 1.18.0

I receive this error.

Reverting to Gradle 4.7 results in proper functionality.

@victorwss
Copy link
Contributor

@arana198 Your problem seems to be something else unrelated to this. Perhaps #1572?

@soberich
Copy link

soberich commented Jun 6, 2018

@pkubowicz @arana198 This started to show up again starting form Gradle 4.6 see #1603
@victorwss You've asked for MVCE there. So, finally we have a kind of it. It is hardly reproducible while I was trying to provide MVCE on a small project and minimal setup - it did not appear. Now this example is finally small and shows the issue.

mszabo-wikia added a commit to Wikia/lombok that referenced this issue Jun 6, 2018
@mszabo-wikia
Copy link
Contributor

looks like Gradle internals changed in 4.8, necessitating some case handling— ☝️ should address that

@victorwss
Copy link
Contributor

victorwss commented Jun 6, 2018

Funny, this is the code where the error happens:

https://github.com/rzwitserloot/lombok/blob/master/src/core/lombok/javac/apt/LombokProcessor.java#L429-L448:

	/**
	 * This class casts the given filer to a JavacFiler. In case of
	 * gradle incremental compilation, the delegate Filer of the gradle wrapper is returned.
	 */
	public JavacFiler getJavacFiler(Filer filer) {
		final Class<?> filerSuperClass = filer.getClass().getSuperclass();
		if (filerSuperClass.getName().equals("org.gradle.api.internal.tasks.compile.processing.IncrementalFiler")) {
			try {
				Field field = filerSuperClass.getDeclaredField("delegate");
				field.setAccessible(true);
				Object delegate = field.get(filer);
				return (JavacFiler) delegate;
			} catch (final Exception e) {
				e.printStackTrace();
				processingEnv.getMessager().printMessage(Kind.WARNING,
						"Can't get the delegate of the gradle IncrementalFiler. Lombok won't work.");
			}
		}
		return (JavacFiler) filer;
	}

The code is clearly aware of gradle's IncrementalFiler, but something was wrong anyway. It tests if the filer has IncrementalFiler as the direct superclass, but the error message suggests that it is a direct instance of IncrementalFiler and not a subclass of that.

Also, that if a reflection exception happens and is caught in the exception handler, a ClassCastException will be throw in the end of the method, but this is not the real problem.

@victorwss
Copy link
Contributor

Also, the code could test if the filer is something that is neither JavacFiler nor IncrementalFiler, giving a better error message in this case to help us with future debugging and issue reporting.

@pkubowicz
Copy link
Author

Can you release this soon?

In Gradle 4.9 you cannot use Lombok 1.18.0 or 1.16.22 at all - not even for non-incremental compilation (tested on the same project configuration as in the original bug report).

To get Java sources compiled with Gradle 4.9 you need to downgrade to Lombok 1.16.20.

@gerritdrost
Copy link

Gradle 4.9 just hit the Arch Linux repo, so I suspect more people will start suffering from this.

@sergey-morenets
Copy link

After upgrade to Gradle 4.9 my project failed to compile.

JDK 10.0.1
Lombok 1.18.0


warning: lombok.javac.apt.LombokProcessor could not be initialized. Lombok will not run during this compilation: java.lang.ClassCastException: org.gradle.api.internal.tasks.compile.processing.IncrementalFiler cannot be cast to jdk.compiler/com.sun.tools.javac.processing.JavacFiler
public class ExceptionHandler {
       ^
        at lombok.javac.apt.LombokProcessor.getJavacFiler(LombokProcessor.java:447)
        at lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:90)
        at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:112)
        at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:165)

@sergey-morenets
Copy link

@pkubowicz Unfortunately I cannot downgrade Lombok to 1.16.20 because I use JDK 10 and this version doesn't support it

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

Successfully merging a pull request may close this issue.

8 participants