Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit a00f56c

Browse files
committed
Use RuntimeDepCollector in standalone
1 parent 8800dff commit a00f56c

5 files changed

Lines changed: 53 additions & 40 deletions

File tree

runtime-dep/src/main/java/net/raphimc/javadowngrader/runtime/RuntimeRoot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
/**
2828
* This class is used to get the URI of this package.
2929
*/
30-
@Deprecated
30+
//@Deprecated
3131
public class RuntimeRoot {
3232
}

src/main/java/net/raphimc/javadowngrader/transformer/DowngradingTransformer.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828

2929
import java.lang.reflect.Field;
3030
import java.lang.reflect.Modifier;
31-
import java.util.ArrayList;
32-
import java.util.HashMap;
33-
import java.util.Map;
31+
import java.util.*;
3432

3533
public abstract class DowngradingTransformer {
3634

@@ -40,7 +38,7 @@ public abstract class DowngradingTransformer {
4038
private final int targetVersion;
4139

4240
private final Map<String, MethodCallReplacer> methodCallReplacers = new HashMap<>();
43-
private final Map<String, String> classReplacements = new HashMap<>();
41+
private final Map<String, List<String>> classReplacements = new HashMap<>();
4442

4543
public DowngradingTransformer(final int sourceVersion, final int targetVersion) {
4644
this.sourceVersion = sourceVersion;
@@ -60,11 +58,25 @@ protected void addMethodCallReplacer(final int opcode, final String owner, final
6058
}
6159

6260
protected void addClassReplacement(final String name) {
63-
this.classReplacements.put(name, Constants.JAVADOWNGRADER_RUNTIME_PACKAGE + name);
61+
addClassReplacement(name, Constants.JAVADOWNGRADER_RUNTIME_PACKAGE + name);
62+
}
63+
64+
protected void addClassReplacement(final String name, String[] extraDeps) {
65+
for (int i = 0; i < extraDeps.length; i++) {
66+
extraDeps[i] = Constants.JAVADOWNGRADER_RUNTIME_PACKAGE + extraDeps[i];
67+
}
68+
addClassReplacement(name, Constants.JAVADOWNGRADER_RUNTIME_PACKAGE + name, extraDeps);
6469
}
6570

6671
protected void addClassReplacement(final String oldName, final String newName) {
67-
this.classReplacements.put(oldName, newName);
72+
this.classReplacements.put(oldName, Collections.singletonList(newName));
73+
}
74+
75+
protected void addClassReplacement(final String oldName, final String newName, String... extraDeps) {
76+
final List<String> classes = new ArrayList<>(1 + extraDeps.length);
77+
classes.add(newName);
78+
Collections.addAll(classes, extraDeps);
79+
this.classReplacements.put(oldName, classes);
6880
}
6981

7082
public void transform(final ClassNode classNode) {
@@ -146,12 +158,12 @@ public void transform(final ClassNode classNode, final RuntimeDepCollector depCo
146158
final ClassRemapper classRemapper = new ClassRemapper(remappedNode, new Remapper() {
147159
@Override
148160
public String map(String internalName) {
149-
final String newName = classReplacements.get(internalName);
150-
if (newName != null) {
151-
depCollector.accept(newName);
152-
return newName;
161+
final List<String> classes = classReplacements.get(internalName);
162+
if (classes == null) {
163+
return internalName;
153164
}
154-
return internalName;
165+
classes.forEach(depCollector);
166+
return classes.get(0);
155167
}
156168
});
157169
classNode.accept(classRemapper);

src/main/java/net/raphimc/javadowngrader/transformer/j8/Java9ToJava8.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Java9ToJava8() {
6060
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/util/regex/Matcher", "appendReplacement", "(Ljava/lang/StringBuilder;Ljava/lang/String;)Ljava/util/regex/Matcher;", new MatcherAppendReplacementMCR());
6161
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/util/regex/Matcher", "appendTail", "(Ljava/lang/StringBuilder;)Ljava/lang/StringBuilder;", new MatcherAppendTailMCR());
6262

63-
this.addClassReplacement("java/lang/StackWalker");
63+
this.addClassReplacement("java/lang/StackWalker", new String[] {"java/lang/StackWalker$Option"});
6464
this.addClassReplacement("java/lang/StackWalker$Option");
6565

6666
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getModule", "()Ljava/lang/Module;", new ClassGetModuleMCR());
@@ -77,7 +77,7 @@ public Java9ToJava8() {
7777
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/util/OptionalDouble", "stream", new OptionalStreamMCR("Double", "D"));
7878

7979
this.addMethodCallReplacer(Opcodes.INVOKESTATIC, "java/lang/Runtime", "version", new RuntimeVersionMCR());
80-
this.addClassReplacement("java/lang/Runtime$Version");
80+
this.addClassReplacement("java/lang/Runtime$Version", new String[] {"java/lang/Runtime$VersionPattern"});
8181
}
8282

8383
@Override

standalone/src/main/java/net/raphimc/javadowngrader/standalone/Main.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,27 @@
2626
import net.raphimc.javadowngrader.impl.classtransform.classprovider.LazyFileClassProvider;
2727
import net.raphimc.javadowngrader.impl.classtransform.classprovider.PathClassProvider;
2828
import net.raphimc.javadowngrader.impl.classtransform.util.ClassNameUtil;
29-
import net.raphimc.javadowngrader.impl.classtransform.util.FileSystemUtil;
3029
import net.raphimc.javadowngrader.runtime.RuntimeRoot;
3130
import net.raphimc.javadowngrader.standalone.progress.MultiThreadedProgressBar;
3231
import net.raphimc.javadowngrader.standalone.util.GeneralUtil;
33-
import net.raphimc.javadowngrader.util.Constants;
3432
import org.slf4j.Logger;
3533
import org.slf4j.LoggerFactory;
3634

3735
import java.io.File;
3836
import java.io.IOException;
37+
import java.io.InputStream;
3938
import java.io.UncheckedIOException;
4039
import java.net.URI;
4140
import java.nio.file.FileSystem;
4241
import java.nio.file.FileSystems;
4342
import java.nio.file.Files;
4443
import java.nio.file.Path;
4544
import java.time.Duration;
45+
import java.util.Collection;
4646
import java.util.Collections;
4747
import java.util.List;
4848
import java.util.Locale;
49-
import java.util.concurrent.Callable;
50-
import java.util.concurrent.ExecutorService;
51-
import java.util.concurrent.Executors;
52-
import java.util.concurrent.TimeUnit;
49+
import java.util.concurrent.*;
5350
import java.util.stream.Collectors;
5451
import java.util.stream.Stream;
5552

@@ -179,12 +176,17 @@ private static void doConversion(
179176
try (FileSystem inFs = FileSystems.newFileSystem(inputFile.toPath(), null)) {
180177
final Path inRoot = inFs.getRootDirectories().iterator().next();
181178

179+
final Collection<String> runtimeDeps = Collections.newSetFromMap(new ConcurrentHashMap<>());
182180
final TransformerManager transformerManager = new TransformerManager(
183181
new PathClassProvider(inRoot, new LazyFileClassProvider(libraryPath, new BasicClassProvider()))
184182
);
185-
transformerManager.addBytecodeTransformer(new JavaDowngraderTransformer(
186-
transformerManager, targetVersion.getVersion(), c -> Files.isRegularFile(inRoot.resolve(ClassNameUtil.toClassFilename(c)))
187-
));
183+
transformerManager.addBytecodeTransformer(
184+
JavaDowngraderTransformer.builder(transformerManager)
185+
.targetVersion(targetVersion.getVersion())
186+
.classFilter(c -> Files.isRegularFile(inRoot.resolve(ClassNameUtil.toClassFilename(c))))
187+
.depCollector(runtimeDeps::add)
188+
.build()
189+
);
188190

189191
try (FileSystem outFs = FileSystems.newFileSystem(new URI("jar:" + outputFile.toURI()), Collections.singletonMap("create", "true"))) {
190192
final Path outRoot = outFs.getRootDirectories().iterator().next();
@@ -245,22 +247,21 @@ private static void doConversion(
245247
throw new IllegalStateException("Thread pool didn't shutdown correctly");
246248
}
247249

248-
LOGGER.info("Copying runtime classes");
249-
try (FileSystem runtimeRootFs = FileSystemUtil.getOrCreateFileSystem(RuntimeRoot.class.getResource("").toURI())) {
250-
final Path runtimeRoot = runtimeRootFs.getPath(Constants.JAVADOWNGRADER_RUNTIME_PACKAGE);
251-
try (Stream<Path> stream = Files.walk(runtimeRoot)) {
252-
stream.filter(Files::isRegularFile)
253-
.filter(p -> !p.getFileName().toString().equals(Constants.JAVADOWNGRADER_RUNTIME_ROOT))
254-
.forEach(path -> {
255-
final String relative = ClassNameUtil.slashName(runtimeRoot.relativize(path));
256-
final Path dest = outRoot.resolve(Constants.JAVADOWNGRADER_RUNTIME_PACKAGE + relative);
257-
try {
258-
Files.createDirectories(dest.getParent());
259-
Files.copy(path, dest);
260-
} catch (IOException e) {
261-
throw new UncheckedIOException(e);
262-
}
263-
});
250+
LOGGER.info("Copying {} runtime class(es)", runtimeDeps.size());
251+
for (final String runtimeDep : runtimeDeps) {
252+
final String classPath = runtimeDep.concat(".class");
253+
LOGGER.debug("Copying {}", classPath);
254+
try (InputStream is = RuntimeRoot.class.getResourceAsStream("/" + classPath)) {
255+
if (is == null) {
256+
LOGGER.warn("Runtime class '{}' not found! Skipping.", runtimeDep);
257+
continue;
258+
}
259+
final Path dest = outRoot.resolve(classPath);
260+
final Path parent = dest.getParent();
261+
if (parent != null) {
262+
Files.createDirectories(parent);
263+
}
264+
Files.copy(is, dest);
264265
}
265266
}
266267
LOGGER.info("Writing final JAR");

standalone/src/main/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<Configuration status="WARN">
2+
<Configuration status="INFO">
33
<Appenders>
44
<!-- System out -->
55
<Console name="SysOut" target="SYSTEM_OUT">

0 commit comments

Comments
 (0)