From 7b3df49422968e9ac284304874cbdfe436894d84 Mon Sep 17 00:00:00 2001 From: rileyJones Date: Thu, 27 Jul 2017 14:33:08 -0700 Subject: [PATCH] Jar Plugin Loader --- src/org/nwapw/abacus/plugin/ClassFinder.java | 65 ++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/org/nwapw/abacus/plugin/ClassFinder.java diff --git a/src/org/nwapw/abacus/plugin/ClassFinder.java b/src/org/nwapw/abacus/plugin/ClassFinder.java new file mode 100644 index 0000000..c914f3d --- /dev/null +++ b/src/org/nwapw/abacus/plugin/ClassFinder.java @@ -0,0 +1,65 @@ +package org.nwapw.abacus.plugin; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.zip.ZipInputStream; + +public class ClassFinder extends ClassLoader{ + + ArrayList classes; + + public ClassFinder(){ + super(ClassFinder.class.getClassLoader()); + classes=new ArrayList(); + } + public Class loadClass(String className) throws ClassNotFoundException{ + return findClass(className); + } + public ArrayList loadClass(File jarLocation) throws ClassNotFoundException, IOException{ + return addJar(jarLocation); + } + public ArrayList addJar(File jarLocation) throws IOException { + JarFile jarFolder = new JarFile(jarLocation); + Enumeration jarList = jarFolder.entries(); + HashMap classSize = new HashMap(); + HashMap classContent = new HashMap(); + ArrayList classNames = new ArrayList(); + JarEntry tempJar; + ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarLocation))); + while(jarList.hasMoreElements()){ + tempJar = (JarEntry)jarList.nextElement(); + zipStream.getNextEntry(); + if(!tempJar.isDirectory()) { + if (tempJar.getName().substring(tempJar.getName().indexOf('.')).equals(".class") && (tempJar.getName().length() < 9 || !tempJar.getName().substring(0, 9).equals("META-INF/"))) { + int size = (int)tempJar.getSize(); + classSize.put(tempJar.getName(),new Integer((int)tempJar.getSize())); + byte[] bytes = new byte[size]; + zipStream.read(bytes,0,size); + classContent.put(tempJar.getName(),bytes); + classNames.add(tempJar.getName()); + } + } + } + jarFolder.close(); + for(String name:classNames) { + classes.add(super.defineClass(name, (byte[]) classContent.get(name), 0, (int) classSize.get(name))); + } + return classNames; + } + public ArrayList getClasses(){ + return classes; + } + public Class getClass(int number){ + return classes.get(number); + } + public void delClasses(){ + classes=new ArrayList(); + } +} \ No newline at end of file