From 61e097fd64e8306b2ff2ae49f27030a68c614879 Mon Sep 17 00:00:00 2001 From: Ramid Khan Date: Wed, 22 Nov 2023 11:47:00 +1100 Subject: [PATCH] update mapping-io --- build.gradle | 8 +-- buildSrc/build.gradle | 2 +- .../github/astrarre/sfu/impl/DescVisitor.java | 2 +- .../astrarre/sfu/impl/JavaFileParser.java | 69 ------------------- .../sfu/impl/RangeCollectingVisitor.java | 2 +- .../io/github/astrarre/sfu/impl/Remapper.java | 2 +- .../io/github/astrarre/sfu/impl/SFUImpl.java | 27 ++++---- .../astrarre/sfu/test/RemappingTests.java | 63 +++++++++-------- 8 files changed, 57 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/io/github/astrarre/sfu/impl/JavaFileParser.java diff --git a/build.gradle b/build.gradle index ed811f3..57da4cb 100644 --- a/build.gradle +++ b/build.gradle @@ -18,14 +18,14 @@ repositories { } dependencies { - implementation("net.fabricmc:mapping-io:0.3.0") + implementation("net.fabricmc:mapping-io:0.5.0") implementation(files(RepackagedJavacProvider.createJavacJar(buildDir))) implementation("org.ow2.asm:asm-util:9.6") - testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") - testRuntimeOnly("org.junit.platform:junit-platform-launcher") + testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0") + testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.0") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.10.0") } java { diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index de18303..18d0ddd 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -1,5 +1,5 @@ plugins { - id "java" + id("java") } repositories { diff --git a/src/main/java/io/github/astrarre/sfu/impl/DescVisitor.java b/src/main/java/io/github/astrarre/sfu/impl/DescVisitor.java index 4123e30..8bf7570 100644 --- a/src/main/java/io/github/astrarre/sfu/impl/DescVisitor.java +++ b/src/main/java/io/github/astrarre/sfu/impl/DescVisitor.java @@ -87,7 +87,7 @@ public Void visitTypeVariable(TypeVariable t, Void unused) { @Override public Void visitWildcard(WildcardType t, Void unused) { - var type = t.getExtendsBound(); + TypeMirror type = t.getExtendsBound(); if (type != null) { this.visit(type); } else { diff --git a/src/main/java/io/github/astrarre/sfu/impl/JavaFileParser.java b/src/main/java/io/github/astrarre/sfu/impl/JavaFileParser.java deleted file mode 100644 index 85339f2..0000000 --- a/src/main/java/io/github/astrarre/sfu/impl/JavaFileParser.java +++ /dev/null @@ -1,69 +0,0 @@ -package io.github.astrarre.sfu.impl; - -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import net.fabricmc.mappingio.format.Tiny1Reader; -import net.fabricmc.mappingio.tree.MemoryMappingTree; -import sfu_rpkg.com.sun.source.tree.CompilationUnitTree; -import sfu_rpkg.com.sun.source.util.JavacTask; -import sfu_rpkg.com.sun.source.util.Trees; -import sfu_rpkg.com.sun.tools.javac.api.JavacTool; -import sfu_rpkg.com.sun.tools.javac.file.JavacFileManager; -import sfu_rpkg.com.sun.tools.javac.util.Context; - -public class JavaFileParser { - - private final JavacFileManager jcFileManager; - private final JavaCompiler javac; - - public JavaFileParser() { - Context context = new Context(); - jcFileManager = new JavacFileManager(context, true, Charset.defaultCharset()); - javac = JavacTool.create(); - } - - public static void main(String[] args) throws IOException { - JavaFileParser jfp = new JavaFileParser(); - jfp.parseJavaSourceFile(Path.of("Test.java")); - } - - public void parseJavaSourceFile(Path filePath) throws IOException { - StringBuilder builder = new StringBuilder(Files.readString(filePath)); - - Iterable javaFiles = jcFileManager.getJavaFileObjects(filePath); - JavacTask jcTask = (JavacTask) javac.getTask(null, - jcFileManager, - null, - List.of(), - null, - javaFiles); - Trees trees = Trees.instance(jcTask); - - try { - /* Iterate the java compiler parse out task. */ - Iterable codeResult = jcTask.parse(); - jcTask.analyze(); - - for (CompilationUnitTree codeTree : codeResult) { - /* Parse out one java file source code. */ - var jsv = new RangeCollectingVisitor(trees); - codeTree.accept(jsv, null); - MemoryMappingTree tree = new MemoryMappingTree(); - Tiny1Reader.read(Files.newBufferedReader(Path.of("test.tiny")), tree); - Remapper remapper = new Remapper(builder, jsv.members, jsv.types, tree, tree.getNamespaceId("from"), - tree.getNamespaceId("to")); - remapper.apply(); - } - - /* Get the parsed out method list. */ - // retMethodList = jsv.getMethodList(); - } catch (IOException e) { - e.printStackTrace(); - } - } -} diff --git a/src/main/java/io/github/astrarre/sfu/impl/RangeCollectingVisitor.java b/src/main/java/io/github/astrarre/sfu/impl/RangeCollectingVisitor.java index 89a8c31..79481b6 100644 --- a/src/main/java/io/github/astrarre/sfu/impl/RangeCollectingVisitor.java +++ b/src/main/java/io/github/astrarre/sfu/impl/RangeCollectingVisitor.java @@ -237,7 +237,7 @@ void addTypeRange(Tree tree) { // todo find "roots" (eg. array element types, ea return; JCTree internal = (JCTree) tree; // todo test for full qualification - var e = new TypeRange(internal.getStartPosition(), internal.getEndPosition(this.root.endPositions), + TypeRange e = new TypeRange(internal.getStartPosition(), internal.getEndPosition(this.root.endPositions), this.util.getDesc(tree), false); this.types.add(e); } diff --git a/src/main/java/io/github/astrarre/sfu/impl/Remapper.java b/src/main/java/io/github/astrarre/sfu/impl/Remapper.java index c7b8d19..459852e 100644 --- a/src/main/java/io/github/astrarre/sfu/impl/Remapper.java +++ b/src/main/java/io/github/astrarre/sfu/impl/Remapper.java @@ -33,7 +33,7 @@ public Remapper(StringBuilder builder, public void apply() { for (RangeCollectingVisitor.MemberRange member : this.members) { String name = member.name(); - var mapping = member.isMethod() + MappingTreeView.MemberMappingView mapping = member.isMethod() ? this.view.getMethod(member.owner(), name, member.desc(), this.srcNamespace) : this.view.getField(member.owner(), name, member.desc(), this.srcNamespace); diff --git a/src/main/java/io/github/astrarre/sfu/impl/SFUImpl.java b/src/main/java/io/github/astrarre/sfu/impl/SFUImpl.java index 0e6cb81..75fe270 100644 --- a/src/main/java/io/github/astrarre/sfu/impl/SFUImpl.java +++ b/src/main/java/io/github/astrarre/sfu/impl/SFUImpl.java @@ -10,6 +10,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.stream.StreamSupport; import javax.lang.model.element.Modifier; import javax.lang.model.element.NestingKind; import javax.tools.*; @@ -92,27 +93,27 @@ public void process() throws IOException { diagnostics, List.of("-proc:none"), null, - inputs.entrySet().stream().map(path -> { - Iterator iterable = fileManager - .getJavaFileObjectsFromPaths(List.of(path.getKey())).iterator(); - JavaFileObject object = iterable.next(); - assert !iterable.hasNext(); - return new WJavaFileObject(object, path.getValue()); - }).toList()); + inputs.entrySet().stream() + .flatMap(path -> StreamSupport + .stream(fileManager.getJavaFileObjectsFromPaths(List.of(path.getKey())).spliterator(), + false) + .map(it -> new WrappedJavaFileObject(it, path.getValue()))) + .toList()); ClientCodeWrapper wrapper = ClientCodeWrapper.instance(task.getContext()); Trees trees = Trees.instance(task); - Iterable codeResult = task.parse(); + Iterable units = task.parse(); task.analyze(); - for (CompilationUnitTree codeTree : codeResult) { - var collector = new RangeCollectingVisitor(trees); + for (CompilationUnitTree codeTree : units) { + RangeCollectingVisitor collector = new RangeCollectingVisitor(trees); codeTree.accept(collector, null); - WJavaFileObject sourceFile; + WrappedJavaFileObject sourceFile; try { - sourceFile = (WJavaFileObject) (JavaFileObject) UNWRAP.invokeExact(wrapper, codeTree.getSourceFile()); + sourceFile = (WrappedJavaFileObject) (JavaFileObject) UNWRAP.invokeExact(wrapper, + codeTree.getSourceFile()); } catch (Throwable e) { throw new RuntimeException(e); } @@ -126,7 +127,7 @@ public void process() throws IOException { } } - record WJavaFileObject(JavaFileObject object, Path output) implements JavaFileObject { + record WrappedJavaFileObject(JavaFileObject object, Path output) implements JavaFileObject { @Override public Kind getKind() { return object.getKind(); diff --git a/src/test/java/io/github/astrarre/sfu/test/RemappingTests.java b/src/test/java/io/github/astrarre/sfu/test/RemappingTests.java index ddea4c3..fd2caab 100644 --- a/src/test/java/io/github/astrarre/sfu/test/RemappingTests.java +++ b/src/test/java/io/github/astrarre/sfu/test/RemappingTests.java @@ -8,46 +8,53 @@ import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import net.fabricmc.mappingio.format.Tiny2Reader; +import net.fabricmc.mappingio.MappingReader; +import net.fabricmc.mappingio.format.MappingFormat; import net.fabricmc.mappingio.tree.MemoryMappingTree; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class RemappingTests { - @Test - public void remap() throws IOException { - Path root = Path.of("src/test/resources"); - Path originals = root.resolve("original"); - Path remapped = root.resolve("remapped"); - Path mappings = root.resolve("mappings"); - Path tempRoot = Files.createTempDirectory("mercury-test"); + Path tempRoot = Files.createTempDirectory("mercury-test"); + Path root = Path.of("src/test/resources"); + Path originals = root.resolve("original"); + Path remapped = root.resolve("remapped"); + Path mappings = root.resolve("mappings"); - for (Path original : Files.list(originals).toList()) { - Path name = originals.relativize(original); - Path output = tempRoot.resolve(name); - Path test = remapped.resolve(name); + public RemappingTests() throws IOException { + } - MemoryMappingTree tree = new MemoryMappingTree(); + @ParameterizedTest(name = ParameterizedTest.ARGUMENTS_PLACEHOLDER) + @ValueSource(strings = { + "fields", + "verbatim", + }) + public void remap(String testCase) throws IOException { + Path original = originals.resolve(testCase); + Path output = tempRoot.resolve(testCase); + Path test = remapped.resolve(testCase); - try (Reader reader = Files.newBufferedReader(mappings.resolve(name + ".tiny"))) { - Tiny2Reader.read(reader, tree); - } + MemoryMappingTree tree = new MemoryMappingTree(); - SourceFixerUpper sfu = SourceFixerUpper.create() - .mappings(tree, tree.getNamespaceId("a"), tree.getNamespaceId("b")); + try (Reader reader = Files.newBufferedReader(mappings.resolve(testCase + ".tiny"))) { + MappingReader.read(reader, MappingFormat.TINY_2_FILE, tree); + } - Files.walk(original).forEach(path -> { - if (Files.isRegularFile(path)) { - sfu.input(path, output.resolve(original.relativize(path))); - } - }); + SourceFixerUpper sfu = SourceFixerUpper.create() + .mappings(tree, tree.getNamespaceId("a"), tree.getNamespaceId("b")); - sfu.process(); + Files.walk(original).forEach(path -> { + if (Files.isRegularFile(path)) { + sfu.input(path, output.resolve(original.relativize(path))); + } + }); - verifyDirsAreEqual(output, test, false); - verifyDirsAreEqual(test, output, true); - } + sfu.process(); + + verifyDirsAreEqual(output, test, false); + verifyDirsAreEqual(test, output, true); } private static void verifyDirsAreEqual(Path one, Path other, boolean flip) throws IOException {