From 217a8b4f6f2fa1e7ca7ded3ad837eea13525eb7f Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Thu, 16 Apr 2020 03:00:10 +0200 Subject: [PATCH] Add simple tool for quickly loading all classes to merge --- src/dev/w1zzrd/asm/Injector.java | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/dev/w1zzrd/asm/Injector.java diff --git a/src/dev/w1zzrd/asm/Injector.java b/src/dev/w1zzrd/asm/Injector.java new file mode 100644 index 0000000..e233f41 --- /dev/null +++ b/src/dev/w1zzrd/asm/Injector.java @@ -0,0 +1,39 @@ +package dev.w1zzrd.asm; + +import jdk.internal.org.objectweb.asm.tree.ClassNode; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.Objects; + +public class Injector { + public static void injectAll(ClassLoader loader, Merger merger) throws IOException { + Enumeration resources = loader.getResources(""); + while (resources.hasMoreElements()) + injectDirectory(new File(resources.nextElement().getPath()), merger); + } + + private static void injectDirectory(File file, Merger merger) throws IOException { + if (file.isDirectory()) + for (File child : Objects.requireNonNull(file.listFiles())) + injectDirectory(child, merger); + else injectFile(file, merger); + } + + private static void injectFile(File file, Merger merger) throws IOException { + URL url = null; + try { + url = file.toURI().toURL(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + ClassNode cNode; + + assert url != null; + if(url.getPath().endsWith(".class") && merger.shouldInject(cNode = Merger.getClassNode(url))) + merger.inject(cNode); + } +}