Skip to content

Commit

Permalink
appearently filesystem.getUri also blocks it
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed May 24, 2024
1 parent 8d188fa commit 662ff5f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class JVMDowngraderExtension(val project: Project) {
flags.debugSkipStubs = shadeDebugSkipStubs.toSet()

ClassDowngrader.downgradeTo(flags).use {
ZipDowngrader.downgradeZip(it, flags.findJavaApi(), emptySet(), downgradedPath.toPath())
ZipDowngrader.downgradeZip(it, flags.findJavaApi().toPath(), emptySet(), downgradedPath.toPath())
}
downgradedPath
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import xyz.wagyourtail.jvmdg.ClassDowngrader;
import xyz.wagyourtail.jvmdg.classloader.providers.ClassLoaderResourceProvider;
import xyz.wagyourtail.jvmdg.classloader.providers.FileSystemResourceProvider;
import xyz.wagyourtail.jvmdg.classloader.providers.JarFileResourceProvider;
import xyz.wagyourtail.jvmdg.util.Function;
import xyz.wagyourtail.jvmdg.util.Utils;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.JarFile;

public class DowngradingClassLoader extends ClassLoader implements Closeable {
private final ClassDowngrader holder;
Expand All @@ -23,9 +24,9 @@ public class DowngradingClassLoader extends ClassLoader implements Closeable {

public DowngradingClassLoader(ClassDowngrader downgrader) throws IOException {
super();
Path apiJar = downgrader.flags.findJavaApi();
File apiJar = downgrader.flags.findJavaApi();
if (apiJar != null) {
delegates.add(new FileSystemResourceProvider(Utils.openZipFileSystem(apiJar, false)));
delegates.add(new JarFileResourceProvider(new JarFile(apiJar)));
}
this.holder = downgrader;
if (downgrader.target != Utils.getCurrentClassVersion()) {
Expand All @@ -37,9 +38,9 @@ public DowngradingClassLoader(ClassDowngrader downgrader) throws IOException {

public DowngradingClassLoader(ClassDowngrader downgrader, ClassLoader parent) throws IOException {
super(parent);
Path apiJar = downgrader.flags.findJavaApi();
File apiJar = downgrader.flags.findJavaApi();
if (apiJar != null) {
delegates.add(new FileSystemResourceProvider(Utils.openZipFileSystem(apiJar, false)));
delegates.add(new JarFileResourceProvider(new JarFile(apiJar)));
}
this.holder = downgrader;
if (downgrader.target != Utils.getCurrentClassVersion()) {
Expand All @@ -63,8 +64,8 @@ public void addDelegate(ClassLoader loader) {
delegates.add(new ClassLoaderResourceProvider(loader));
}

public void addDelegate(FileSystem jarFile) {
delegates.add(new FileSystemResourceProvider(jarFile));
public void addDelegate(JarFile jarFile) {
delegates.add(new JarFileResourceProvider(jarFile));
}

public void addDelegate(URL[] urls) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package xyz.wagyourtail.jvmdg.classloader.providers;

import xyz.wagyourtail.jvmdg.classloader.ResourceProvider;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarFileResourceProvider implements ResourceProvider {
private final JarFile jarFile;

public JarFileResourceProvider(JarFile jarFile) {
this.jarFile = jarFile;
}


@Override
public Enumeration<URL> getResources(String name) throws IOException {
final JarEntry entry = jarFile.getJarEntry(name);
if (entry == null) {
return Collections.emptyEnumeration();
}
return Collections.enumeration(Collections.singletonList(new URL("x-buffer", null, -1, name, new URLStreamHandler() {
@Override
protected URLConnection openConnection(final URL u1) {
return new URLConnection(u1) {
@Override
public void connect() {
}

@Override
public InputStream getInputStream() {
try {
return jarFile.getInputStream(entry);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
})));
}

@Override
public void close() throws IOException {
jarFile.close();
}

}
8 changes: 4 additions & 4 deletions src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ private URL getJavaApiFromMaven() throws IOException {
}
}

public Path findJavaApi() {
public File findJavaApi() {
try {
if (api != null) {
return api.toPath();
return api;
}
Constants.DIR.mkdirs();
Path tmp = Constants.DIR.toPath().resolve("jvmdg-api.jar");
File prop = getJavaApiFromSystemProperty();
if (prop != null) {
return prop.toPath();
return prop;
}
URL url = getJavaApiFromShade();
if (url == null && allowMaven) {
Expand All @@ -110,7 +110,7 @@ public Path findJavaApi() {
try (InputStream in = url.openStream()) {
Files.copy(in, tmp, StandardCopyOption.REPLACE_EXISTING);
}
return tmp;
return tmp.toFile();
} else {
// failed to find java api
if (!quiet) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/wagyourtail/jvmdg/compile/ApiShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static Path resolveDowngradedApi(Flags flags, @Nullable File downgradedAp
}

public static void downgradeApi(Flags flags, Path outputLocation) throws IOException {
downgradedApi(flags, flags.findJavaApi(), outputLocation);
downgradedApi(flags, flags.findJavaApi().toPath(), outputLocation);
}

public static Pair<ReferenceGraph, Set<Type>> scanApis(Path apiRoot) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/wagyourtail/jvmdg/runtime/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static String sha1(Path p) {
public static void premain(String args, Instrumentation instrumentation) throws IOException, URISyntaxException, UnmodifiableClassException {
LOGGER.info("Starting JVMDowngrader Bootstrap in agent mode.");
// downgrade api
Path zip = flags.findJavaApi();
Path zip = flags.findJavaApi().toPath();
String zipSha = sha1(zip);
Path tmp = Constants.DIR.toPath().resolve("java-api-downgraded-" + currentVersionDowngrader.target + "-" + zipSha.substring(0, 8) + ".jar");
boolean downgrade = false;
Expand Down

0 comments on commit 662ff5f

Please sign in to comment.