From 1148ae1dde901c6b686d03ef7c11155d299b6ca2 Mon Sep 17 00:00:00 2001 From: Sergey Lapin Date: Wed, 9 Nov 2022 22:18:49 +0600 Subject: [PATCH] target only kotlin classes as annotation processor arguments --- plugins/gradle/build.gradle.kts | 2 +- .../java/com/slapin/napt/NaptGradlePlugin.kt | 48 +++++++++++++++++-- plugins/javac/build.gradle | 4 +- .../java/com/slapin/napt/ClassesProvider.java | 7 ++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/plugins/gradle/build.gradle.kts b/plugins/gradle/build.gradle.kts index c7f195f..bbf1485 100644 --- a/plugins/gradle/build.gradle.kts +++ b/plugins/gradle/build.gradle.kts @@ -9,7 +9,7 @@ plugins { group = "com.sergei-lapin.napt" -version = "1.17" +version = "1.18" tasks.withType { kotlinOptions.jvmTarget = "9" } diff --git a/plugins/gradle/src/main/java/com/slapin/napt/NaptGradlePlugin.kt b/plugins/gradle/src/main/java/com/slapin/napt/NaptGradlePlugin.kt index 9d580ac..3830575 100644 --- a/plugins/gradle/src/main/java/com/slapin/napt/NaptGradlePlugin.kt +++ b/plugins/gradle/src/main/java/com/slapin/napt/NaptGradlePlugin.kt @@ -7,14 +7,18 @@ import java.io.File import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.file.Directory +import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Provider +import org.gradle.api.tasks.Input import org.gradle.api.tasks.SourceSetContainer import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.compile.JavaCompile import org.gradle.plugins.ide.idea.GenerateIdeaModule import org.gradle.plugins.ide.idea.model.IdeaModel +import org.gradle.process.CommandLineArgumentProvider +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile -private const val CompilerPlugin = "io.github.sergei-lapin.napt:javac:1.1" +private const val CompilerPlugin = "io.github.sergei-lapin.napt:javac:1.2" private const val AnnotationProcessor = "annotationProcessor" private const val MainSourceSet = "main" @@ -125,11 +129,49 @@ class NaptGradlePlugin : Plugin { sourceSetName in extension.additionalSourceSetsForTriggerGeneration.get() private fun Project.notifyJavaCompilerAboutPlugin(extension: NaptGradleExtension) { + val rootProjectDir = rootDir tasks.withType(JavaCompile::class.java).configureEach { javaCompile -> + javaCompile.options.compilerArgumentProviders.add( + NaptCompilerArgumentsProvider( + kotlinClassesDirPath = + tasks + .withType(KotlinCompile::class.java) + .named(javaCompile.name.replace("JavaWithJavac", "Kotlin").replace("Java", "Kotlin")) + .flatMap(KotlinCompile::destinationDirectory) + .map { directory -> directory.asFile.relativeTo(rootProjectDir).path } + ) + ) javaCompile.options.compilerArgs.add("-Xplugin:Napt") javaCompile.options.isFork = true - requireNotNull(javaCompile.options.forkOptions.jvmArgs) - .addAll((extension.forkJvmArgs.get() + JvmArgsStrongEncapsulation).toSet()) + @Suppress("UnstableApiUsage") + javaCompile.options.forkOptions.jvmArgumentProviders.add( + NaptForkOptionsJvmArgumentsProvider( + forkJvmArgs = extension.forkJvmArgs, + jvmArgsStrongEncapsulation = + objects.listProperty(String::class.java).convention(JvmArgsStrongEncapsulation), + ) + ) } } } + +private class NaptCompilerArgumentsProvider( + @get:Input val kotlinClassesDirPath: Provider, +) : CommandLineArgumentProvider { + + override fun asArguments(): Iterable { + return listOf( + "-Xplugin:Napt", + "-XDKotlinClassesDir=${File(kotlinClassesDirPath.get())}", + ) + } +} + +private class NaptForkOptionsJvmArgumentsProvider( + @get:Input val forkJvmArgs: ListProperty, + @get:Input val jvmArgsStrongEncapsulation: ListProperty, +) : CommandLineArgumentProvider { + override fun asArguments(): Iterable { + return (forkJvmArgs.get() + jvmArgsStrongEncapsulation.get()).toSet() + } +} diff --git a/plugins/javac/build.gradle b/plugins/javac/build.gradle index 10eb87f..c618a37 100644 --- a/plugins/javac/build.gradle +++ b/plugins/javac/build.gradle @@ -5,7 +5,7 @@ plugins { } group = "io.github.sergei-lapin.napt" -version = "1.1" +version = "1.2" ext.isRelease = !version.endsWith("SNAPSHOT") @@ -93,4 +93,4 @@ afterEvaluate { required { isRelease } sign publishing.publications.maven } -} \ No newline at end of file +} diff --git a/plugins/javac/src/main/java/com/slapin/napt/ClassesProvider.java b/plugins/javac/src/main/java/com/slapin/napt/ClassesProvider.java index 67b81da..3c55030 100644 --- a/plugins/javac/src/main/java/com/slapin/napt/ClassesProvider.java +++ b/plugins/javac/src/main/java/com/slapin/napt/ClassesProvider.java @@ -1,6 +1,7 @@ package com.slapin.napt; import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Options; import java.io.File; import java.io.IOException; @@ -22,10 +23,12 @@ static Set getClassNames(Context context) { final URLClassLoader classLoader = (URLClassLoader) context.get(JavaFileManager.class) .getClassLoader(StandardLocation.CLASS_PATH); + final String kotlinClassesDir = Options.instance(context).get("KotlinClassesDir"); + final boolean isWindows = System.getProperty("os.name").toLowerCase().contains("win"); try (final Stream stream = Arrays.stream(classLoader.getURLs()) - .filter(it -> !it.getFile().endsWith("jar")) + .filter(it -> !it.getFile().endsWith("jar") && it.getFile().contains(kotlinClassesDir)) .map(url -> { String file = url.getFile(); if (isWindows) { @@ -59,4 +62,4 @@ private static String toClassName(String path) { .substring(0, path.length() - ".class".length()) .replace(File.separatorChar, '.'); } -} \ No newline at end of file +}