Skip to content

Commit 5a59a3f

Browse files
authored
Implement javac plugin core
1 parent 995db39 commit 5a59a3f

13 files changed

Lines changed: 546 additions & 85 deletions

File tree

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- "**"
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
steps:
15+
- name: Check out repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Java
19+
uses: actions/setup-java@v4
20+
with:
21+
distribution: temurin
22+
java-version: 17
23+
24+
- name: Set up Gradle
25+
uses: gradle/actions/setup-gradle@v4
26+
27+
- name: Build and test
28+
run: ./gradlew build

.github/workflows/publish.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
workflow_dispatch:
8+
9+
jobs:
10+
publish:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
packages: write
15+
steps:
16+
- name: Check out repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Java
20+
uses: actions/setup-java@v4
21+
with:
22+
distribution: temurin
23+
java-version: 17
24+
25+
- name: Set up Gradle
26+
uses: gradle/actions/setup-gradle@v4
27+
28+
- name: Publish to GitHub Packages
29+
env:
30+
GITHUB_ACTOR: ${{ github.actor }}
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
GITHUB_REPOSITORY: ${{ github.repository }}
33+
run: ./gradlew publish

build.gradle.kts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
import org.gradle.jvm.tasks.Jar
3+
4+
plugins {
5+
`java-library`
6+
`maven-publish`
7+
}
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
val javacExports = listOf(
14+
"jdk.compiler/com.sun.tools.javac.api",
15+
"jdk.compiler/com.sun.tools.javac.code",
16+
"jdk.compiler/com.sun.tools.javac.tree",
17+
"jdk.compiler/com.sun.tools.javac.util",
18+
)
19+
20+
dependencies {
21+
testImplementation(libs.junit.jupiter)
22+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
23+
}
24+
25+
java {
26+
toolchain {
27+
languageVersion = JavaLanguageVersion.of(17)
28+
}
29+
withJavadocJar()
30+
withSourcesJar()
31+
}
32+
33+
tasks.withType<JavaCompile>().configureEach {
34+
options.compilerArgs.addAll(
35+
javacExports.flatMap { export -> listOf("--add-exports", "$export=ALL-UNNAMED") },
36+
)
37+
}
38+
39+
tasks.withType<Test>().configureEach {
40+
useJUnitPlatform()
41+
jvmArgs(javacExports.map { export -> "--add-exports=$export=ALL-UNNAMED" })
42+
testLogging {
43+
exceptionFormat = TestExceptionFormat.FULL
44+
}
45+
}
46+
47+
tasks.named<Jar>("jar") {
48+
manifest {
49+
attributes["Automatic-Module-Name"] = "net.ikvm.javarefplugin"
50+
}
51+
}
52+
53+
publishing {
54+
publications {
55+
create<MavenPublication>("mavenJava") {
56+
from(components["java"])
57+
artifactId = rootProject.name
58+
pom {
59+
name = "java-ref-plugin"
60+
description = project.description
61+
url = "https://github.com/ikvmnet/java-ref-plugin"
62+
licenses {
63+
license {
64+
name = "MIT License"
65+
url = "https://opensource.org/licenses/MIT"
66+
}
67+
}
68+
developers {
69+
developer {
70+
id = "ikvmnet"
71+
name = "IKVM"
72+
}
73+
}
74+
scm {
75+
connection = "scm:git:https://github.com/ikvmnet/java-ref-plugin.git"
76+
developerConnection = "scm:git:ssh://git@github.com/ikvmnet/java-ref-plugin.git"
77+
url = "https://github.com/ikvmnet/java-ref-plugin"
78+
}
79+
}
80+
}
81+
}
82+
repositories {
83+
maven {
84+
name = "GitHubPackages"
85+
val repositoryPath = providers.environmentVariable("GITHUB_REPOSITORY")
86+
.orElse("ikvmnet/java-ref-plugin")
87+
url = uri(repositoryPath.map { "https://maven.pkg.github.com/$it" })
88+
credentials {
89+
username = providers.gradleProperty("githubUsername")
90+
.orElse(providers.environmentVariable("GITHUB_ACTOR"))
91+
.orNull
92+
password = providers.gradleProperty("githubToken")
93+
.orElse(providers.environmentVariable("GITHUB_TOKEN"))
94+
.orNull
95+
}
96+
}
97+
}
98+
}

gradle.properties

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# This file was generated by the Gradle 'init' task.
2-
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
3-
41
org.gradle.configuration-cache=true
5-
2+
group=net.ikvm
3+
version=0.1.0-SNAPSHOT
4+
description=Javac plugin that rewrites concrete method bodies into NoSuchMethodError throws for reference-only API artifacts.

gradle/libs.versions.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
# This file was generated by the Gradle 'init' task.
2-
# https://docs.gradle.org/current/userguide/version_catalogs.html#sec::toml-dependencies-format
3-
41
[versions]
5-
commons-math3 = "3.6.1"
6-
guava = "33.5.0-jre"
72
junit-jupiter = "6.0.1"
83

94
[libraries]
10-
commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" }
11-
guava = { module = "com.google.guava:guava", version.ref = "guava" }
125
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }

lib/build.gradle.kts

Lines changed: 0 additions & 41 deletions
This file was deleted.

lib/src/main/java/net/ikvm/javarefplugin/Library.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/src/test/java/net/ikvm/javarefplugin/LibraryTest.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

settings.gradle.kts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
/*
2-
* This file was generated by the Gradle 'init' task.
3-
*
4-
* The settings file is used to specify which projects to include in your build.
5-
* For more detailed information on multi-project builds, please refer to https://docs.gradle.org/9.5.1/userguide/multi_project_builds.html in the Gradle documentation.
6-
*/
7-
81
plugins {
9-
// Apply the foojay-resolver plugin to allow automatic download of JDKs
102
id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
113
}
124

135
rootProject.name = "java-ref-plugin"
14-
include("lib")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.ikvm.javarefplugin;
2+
3+
import com.sun.source.util.JavacTask;
4+
import com.sun.source.util.Plugin;
5+
import com.sun.source.util.TaskEvent;
6+
import com.sun.source.util.TaskListener;
7+
import com.sun.tools.javac.api.BasicJavacTask;
8+
import com.sun.tools.javac.util.Context;
9+
import java.util.Collections;
10+
import java.util.IdentityHashMap;
11+
import java.util.Set;
12+
13+
public final class JavaRefPlugin implements Plugin {
14+
15+
public static final String NAME = "JavaRef";
16+
17+
@Override
18+
public String getName() {
19+
return NAME;
20+
}
21+
22+
@Override
23+
public void init(JavacTask task, String... args) {
24+
Context context = ((BasicJavacTask) task).getContext();
25+
MethodBodyStripper stripper = new MethodBodyStripper(context);
26+
Set<Object> processedUnits = Collections.newSetFromMap(new IdentityHashMap<>());
27+
28+
task.addTaskListener(new TaskListener() {
29+
@Override
30+
public void started(TaskEvent event) {
31+
}
32+
33+
@Override
34+
public void finished(TaskEvent event) {
35+
if (event.getKind() != TaskEvent.Kind.ENTER) {
36+
return;
37+
}
38+
39+
Object unit = event.getCompilationUnit();
40+
if (unit != null && processedUnits.add(unit)) {
41+
stripper.strip(unit);
42+
}
43+
}
44+
});
45+
}
46+
}

0 commit comments

Comments
 (0)