Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
Don't include mock FF internals in the runtime classpath.
Browse files Browse the repository at this point in the history
This fixes issues with QF over at sl-starplane
  • Loading branch information
Geolykt committed Oct 23, 2022
1 parent b20d381 commit 5457b60
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package io.github.coolcrabs.brachyura.compiler.java;

import org.jetbrains.annotations.NotNull;

public class CompilationFailedException extends RuntimeException {

private static final long serialVersionUID = 7555474203258502895L; // Slbrachyura: Add serialVersionUID

public CompilationFailedException() {
// Default-constructor
}

public CompilationFailedException(@NotNull Throwable t) {
super(t);
}
}
2 changes: 1 addition & 1 deletion build.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
javac Buildscript.java
java Buildscript build
java Buildscript publish
62 changes: 60 additions & 2 deletions buildscript/src/main/java/Buildscript.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.tools.StandardLocation;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.tinylog.Logger;

import io.github.coolcrabs.brachyura.compiler.java.CompilationFailedException;
import io.github.coolcrabs.brachyura.compiler.java.JavaCompilation;
import io.github.coolcrabs.brachyura.compiler.java.JavaCompilationOptions;
import io.github.coolcrabs.brachyura.compiler.java.JavaCompilationResult;
Expand All @@ -42,7 +52,8 @@

public class Buildscript extends BaseJavaProject {
static final String GROUP = "de.geolykt.starloader.brachyura";
private static final String BRACHY_VERSION = "0.94.4";
private static final String BRACHY_VERSION = "0.94.5";
private static final String FERNUTIL_VERSION = "0.2.1";

@NotNull
private final JavaCompilationOptions compileOptions = new JavaCompilationOptions();
Expand Down Expand Up @@ -179,8 +190,21 @@ public String getJarBaseName() {
}

public final Lazy<BJarResult> built = new Lazy<>(this::build);

protected BJarResult build() {
Path buildLibsDir = PathUtil.resolveAndCreateDir(PathUtil.resolveAndCreateDir(getModuleRoot(), "build"), "libs");
// Slbrachyura start: Purge old jars before building jars
try {
List<Path> jars = Files.walk(buildLibsDir, 1)
.filter(p -> p.getFileName().toString().endsWith(".jar"))
.collect(Collectors.toList());
for (Path jar : jars) {
Files.delete(jar);
}
} catch (IOException e) {
e.printStackTrace();
}
// Slbrachyura end
Path testSrc = getModuleRoot().resolve("src").resolve("test").resolve("java");
Path outjar = buildLibsDir.resolve(getJarBaseName() + ".jar");
Path outjarsources = buildLibsDir.resolve(getJarBaseName() + "-sources.jar");
Expand Down Expand Up @@ -318,7 +342,7 @@ public boolean hasTests() {
@Override
@NotNull
MavenId getId() {
return new MavenId(GROUP, "fernutil", "0.2");
return new MavenId(GROUP, "fernutil", FERNUTIL_VERSION);
}

@Override
Expand All @@ -333,6 +357,40 @@ protected List<JavaJarDependency> createDependencies() {
}
return deps;
}

// Slbrachyura start: Don't include mock FF internals in the runtime classpath.
@Override
protected BJarResult build() {
BJarResult result = super.build();
Path jarPath = result.main.jar;
if (Files.exists(jarPath)) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] bytebuffer = new byte[4096];
try (ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(jarPath), StandardCharsets.UTF_8);
ZipOutputStream zipOut = new ZipOutputStream(bytes, StandardCharsets.UTF_8)) {
for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) {
if (entry.getName().startsWith("org/jetbrains/java/decompiler/") || entry.getName().startsWith("/org/jetbrains/java/decompiler/")) {
continue;
}
zipOut.putNextEntry(entry);
for (int read = zipIn.read(bytebuffer); read != -1; read = zipIn.read(bytebuffer)) {
zipOut.write(bytebuffer, 0, read);
}
}
} catch (IOException e) {
throw new CompilationFailedException(e);
}
try (OutputStream out = Files.newOutputStream(jarPath, StandardOpenOption.TRUNCATE_EXISTING)) {
bytes.writeTo(out);
} catch (IOException e) {
throw new CompilationFailedException(e);
}
} else {
throw new IllegalStateException("Build failure? Built jar doesn't exist for some reason.");
}
return result;
}
// Slbrachyura end
};

public final Lazy<List<JavaJarDependency>> asm = new Lazy<>(this::createAsm);
Expand Down
31 changes: 26 additions & 5 deletions fernutil/src/main/java/io/github/coolcrabs/fernutil/TJump.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.github.coolcrabs.fernutil;

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

import org.jetbrains.java.decompiler.main.Fernflower;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;

import io.github.coolcrabs.fernutil.FernUtil.JavadocProvider;
Expand Down Expand Up @@ -43,13 +46,31 @@ public static void decompile(Path inJar, Path outSources, List<Path> cp, Consume
options.put(IFernflowerPreferences.NEW_LINE_SEPARATOR, "\n");
// Threads configured by default in all ff forks
if (fabric) options.put(IFabricJavadocProvider.PROPERTY_NAME, new TJavadocProviderFabric(provider));
Fernflower ff = new Fernflower(bytecodeProvider, resultSaver, options, new TFFLogger());
ff.addSource(inJar.toFile());
Class<?> ffClass = Class.forName("org.jetbrains.java.decompiler.main.Fernflower", true, PackageHack.class.getClassLoader());
MethodType ffCtorType = MethodType.fromMethodDescriptorString("(Lorg/jetbrains/java/decompiler/main/extern/IBytecodeProvider;Lorg/jetbrains/java/decompiler/main/extern/IResultSaver;Ljava/util/Map;Lorg/jetbrains/java/decompiler/main/extern/IFernflowerLogger;)V", ffClass.getClassLoader());
MethodHandle ffCtorHandle = MethodHandles.publicLookup().findConstructor(ffClass, ffCtorType);
Object ff = ffCtorHandle.invoke(bytecodeProvider, resultSaver, options, new TFFLogger());
MethodHandles.publicLookup().bind(ff, "addSource", MethodType.methodType(Void.class, File.class)).invoke(inJar.toFile());
MethodHandle addLibHandle = MethodHandles.publicLookup().bind(ff, "addLibrary", MethodType.methodType(Void.class, File.class));
for (Path p : cp) {
ff.addLibrary(p.toFile());
addLibHandle.invoke(p.toFile());
}
MethodHandles.publicLookup().bind(ff, "decompileContext", MethodType.methodType(Void.class)).invoke();
} catch (Throwable t) {
if (t instanceof Exception) {
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
throw new IllegalStateException(t);
} else {
sneak(t);
}
ff.decompileContext();
}
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneak(Throwable t) throws T {
throw (T) t;
}
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion offline-build.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
javac Buildscript.java
java -Dskiptests=true Buildscript build
java -Dskiptests=true Buildscript publish

0 comments on commit 5457b60

Please sign in to comment.