Skip to content

Commit

Permalink
Fix projectlombok#1716: Update for Gradle incremental compilation API…
Browse files Browse the repository at this point in the history
… change in Gradle 4.8 and above
  • Loading branch information
mszabo-wikia committed Jun 6, 2018
1 parent 5f58f0a commit ca99bdf
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/core/lombok/javac/apt/LombokProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
*/
@SupportedAnnotationTypes("*")
public class LombokProcessor extends AbstractProcessor {
private static final String GRADLE_INCREMENTAL_FILER_CLASS =
"org.gradle.api.internal.tasks.compile.processing.IncrementalFiler";

private ProcessingEnvironment processingEnv;
private JavacProcessingEnvironment javacProcessingEnv;
private JavacFiler javacFiler;
Expand Down Expand Up @@ -431,20 +434,30 @@ public JavacProcessingEnvironment getJavacProcessingEnvironment(ProcessingEnviro
* 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.");
try {
final Class<?> filerClass = filer.getClass();
final Class<?> filerSuperClass = filerClass.getSuperclass();

if (filerSuperClass.getName().equals(GRADLE_INCREMENTAL_FILER_CLASS)) {
// Gradle before 4.8
return tryGetJavacFilerDelegate(filerSuperClass, filer);
} else if (filer.getClass().getName().equals(GRADLE_INCREMENTAL_FILER_CLASS)) {
// Gradle 4.8 and above
return tryGetJavacFilerDelegate(filerClass, filer);
}
} 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;
}

private JavacFiler tryGetJavacFilerDelegate(Class<?> filerDelegateClass, Object instance) throws Exception {
Field field = filerDelegateClass.getDeclaredField("delegate");
field.setAccessible(true);
Object delegate = field.get(instance);
return (JavacFiler) delegate;
}
}

0 comments on commit ca99bdf

Please sign in to comment.