Skip to content

Commit

Permalink
Merge pull request #15 from sergei-lapin/target-only-kotlin-classes
Browse files Browse the repository at this point in the history
target only kotlin classes as annotation processor arguments
  • Loading branch information
sergei-lapin authored Nov 9, 2022
2 parents 1f9a854 + 1148ae1 commit ef936cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/gradle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {

group = "com.sergei-lapin.napt"

version = "1.17"
version = "1.18"

tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "9" }

Expand Down
48 changes: 45 additions & 3 deletions plugins/gradle/src/main/java/com/slapin/napt/NaptGradlePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -125,11 +129,49 @@ class NaptGradlePlugin : Plugin<Project> {
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<String>,
) : CommandLineArgumentProvider {

override fun asArguments(): Iterable<String> {
return listOf(
"-Xplugin:Napt",
"-XDKotlinClassesDir=${File(kotlinClassesDirPath.get())}",
)
}
}

private class NaptForkOptionsJvmArgumentsProvider(
@get:Input val forkJvmArgs: ListProperty<String>,
@get:Input val jvmArgsStrongEncapsulation: ListProperty<String>,
) : CommandLineArgumentProvider {
override fun asArguments(): Iterable<String> {
return (forkJvmArgs.get() + jvmArgsStrongEncapsulation.get()).toSet()
}
}
4 changes: 2 additions & 2 deletions plugins/javac/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "io.github.sergei-lapin.napt"
version = "1.1"
version = "1.2"

ext.isRelease = !version.endsWith("SNAPSHOT")

Expand Down Expand Up @@ -93,4 +93,4 @@ afterEvaluate {
required { isRelease }
sign publishing.publications.maven
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,10 +23,12 @@ static Set<String> 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<String> 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) {
Expand Down Expand Up @@ -59,4 +62,4 @@ private static String toClassName(String path) {
.substring(0, path.length() - ".class".length())
.replace(File.separatorChar, '.');
}
}
}

0 comments on commit ef936cc

Please sign in to comment.