-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
add javac plugin #22
Open
rhysdh540
wants to merge
6
commits into
unimined:main
Choose a base branch
from
rhysdh540:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add javac plugin #22
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1377f1
add javac plugin and test
rhysdh540 2167dd1
remove old commented code
rhysdh540 6f651f9
use argument parsing from `Main`, misc. cleanup
rhysdh540 ebcebd9
more cleanup, don't rerun coverageReport all the time
rhysdh540 4ea96f4
depend correctly
rhysdh540 7f81a5e
depend correctly v2
rhysdh540 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(8)) | ||
} | ||
} | ||
|
||
val java8 = javaToolchains.compilerFor { languageVersion.set(JavaLanguageVersion.of(8)) }.get() | ||
|
||
dependencies { | ||
compileOnly(rootProject.sourceSets["main"].output) | ||
compileOnly(rootProject.sourceSets["shared"].output) | ||
|
||
implementation(files("${java8.metadata.installationPath}/lib/tools.jar")) | ||
|
||
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2") | ||
testRuntimeOnly("org.junit.platform:junit-platform-launcher") | ||
} | ||
|
||
tasks.compileTestJava { | ||
classpath += project(":java-api").tasks.getByName("testJar").outputs.files + | ||
files(rootProject.sourceSets.map { it.compileClasspath }.flatten()) | ||
|
||
javaCompiler = javaToolchains.compilerFor { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
|
||
options.compilerArgs.add("-Xplugin:jvmdg --classVersion 52 --logLevel info") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
132 changes: 132 additions & 0 deletions
132
javac-plugin/src/main/java/xyz/wagyourtail/jvmdg/javac/JvmdgJavacPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package xyz.wagyourtail.jvmdg.javac; | ||
|
||
import com.sun.source.util.Plugin; | ||
import com.sun.source.util.JavacTask; | ||
import com.sun.tools.javac.api.BasicJavacTask; | ||
import com.sun.tools.javac.main.JavaCompiler; | ||
import xyz.wagyourtail.jvmdg.ClassDowngrader; | ||
import xyz.wagyourtail.jvmdg.cli.Flags; | ||
import xyz.wagyourtail.jvmdg.cli.Main; | ||
import xyz.wagyourtail.jvmdg.compile.PathDowngrader; | ||
import xyz.wagyourtail.jvmdg.util.Utils; | ||
|
||
import javax.tools.JavaFileManager; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardLocation; | ||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodType; | ||
import java.lang.reflect.Method; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.*; | ||
|
||
public class JvmdgJavacPlugin implements Plugin, Closeable { | ||
private BasicJavacTask task; | ||
private Flags flags; | ||
|
||
@Override | ||
public String getName() { | ||
return "jvmdg"; | ||
} | ||
|
||
static { | ||
int vmVersion = Utils.classVersionToMajorVersion(Utils.getCurrentClassVersion()); | ||
if(vmVersion > 8) { | ||
try { | ||
//noinspection JavaReflectionMemberAccess | ||
Method getModule = Class.class.getDeclaredMethod("getModule"); | ||
openModule(getModule.invoke(JavacTask.class)); | ||
} catch (Throwable t) { | ||
Utils.sneakyThrow(t); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void init(JavacTask t, String... args) { | ||
this.task = (BasicJavacTask) t; | ||
|
||
JavaCompiler compiler = JavaCompiler.instance(task.getContext()); | ||
compiler.closeables = compiler.closeables.prepend(this); | ||
|
||
this.flags = new Flags(); | ||
|
||
try { | ||
Main.parseArgs(args, flags); | ||
} catch (Throwable e) { | ||
Utils.sneakyThrow(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
runDowngrade(); | ||
} | ||
|
||
@SuppressWarnings("UrlHashCode") | ||
private void runDowngrade() throws IOException { | ||
final JavaFileManager fileManager = task.getContext().get(JavaFileManager.class); | ||
|
||
File root = new File( | ||
fileManager.getJavaFileForOutput( | ||
StandardLocation.CLASS_OUTPUT, | ||
"", JavaFileObject.Kind.CLASS, null | ||
).toUri() | ||
).getParentFile(); | ||
|
||
Set<URL> classpathURLs = new HashSet<>(); | ||
|
||
// the set argument must be mutable, since GradleStandardJavaFileManager calls remove | ||
for(JavaFileObject jfo : fileManager.list(StandardLocation.CLASS_PATH, "", | ||
new HashSet<>(Collections.singletonList(JavaFileObject.Kind.CLASS)), true)) { | ||
classpathURLs.add(jfo.toUri().toURL()); | ||
} | ||
|
||
Path tempOutput = Files.createTempDirectory("downgrade"); | ||
tempOutput.toFile().deleteOnExit(); | ||
|
||
try(ClassDowngrader cd = ClassDowngrader.downgradeTo(flags)) { | ||
cd.logger.debug("classpath: " + classpathURLs); | ||
|
||
PathDowngrader.downgradePaths( | ||
cd, | ||
Collections.singletonList(root.toPath()), | ||
Collections.singletonList(tempOutput), | ||
classpathURLs | ||
); | ||
} | ||
|
||
Files.walk(tempOutput) | ||
.filter(Files::isRegularFile) | ||
.forEach(p -> { | ||
try { | ||
Path target = root.toPath().resolve(tempOutput.relativize(p)); | ||
Files.createDirectories(target.getParent()); | ||
Files.move(p, target, StandardCopyOption.REPLACE_EXISTING); | ||
} catch (Throwable t) { | ||
Utils.sneakyThrow(t); | ||
} | ||
}); | ||
} | ||
|
||
public static void openModule(Object o) throws Throwable { | ||
Class<?> moduleClass = o.getClass(); | ||
if(!moduleClass.getName().equals("java.lang.Module")) { | ||
throw new IllegalArgumentException("Not a module: " + o); | ||
} | ||
MethodHandle implAddOpens = Utils.getImplLookup().findVirtual(moduleClass, "implAddOpens", | ||
MethodType.methodType(void.class, String.class)); | ||
|
||
@SuppressWarnings("unchecked") | ||
Set<String> packages = (Set<String>) moduleClass.getDeclaredMethod("getPackages").invoke(o); | ||
|
||
for(String pn : packages) { | ||
implAddOpens.invoke(o, pn); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
javac-plugin/src/main/resources/META-INF/services/com.sun.source.util.Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
xyz.wagyourtail.jvmdg.javac.JvmdgJavacPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class TheTest { | ||
@Test | ||
public void test() { | ||
// if this class loads, the plugin is working | ||
|
||
// just make sure we're running on Java 8 | ||
assertTrue(System.getProperty("java.version").startsWith("1.8")); | ||
|
||
String h = """ | ||
Hello World! | ||
"""; | ||
assertEquals("Hello World!\n", h); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
second part could be
rootProject.tasks["jar"]outputs.files
I thinkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also I would've passed the api jar as a flag, instead of directly putting it on the classpath. like I do in the other testing subproject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah the second one is a good idea