From 701de050fdad70b98b5275e301aa0d2b00e3caa3 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 28 Jul 2017 11:35:23 -0700 Subject: [PATCH] Remove old ClassFinder and hardcoded folder names, and fix class filter. --- src/org/nwapw/abacus/Abacus.java | 14 +-- src/org/nwapw/abacus/plugin/ClassFinder.java | 116 ++++++++---------- .../nwapw/abacus/plugin/ClassFinderV2.java | 51 -------- .../nwapw/abacus/plugin/PluginManager.java | 2 +- 4 files changed, 58 insertions(+), 125 deletions(-) delete mode 100644 src/org/nwapw/abacus/plugin/ClassFinderV2.java diff --git a/src/org/nwapw/abacus/Abacus.java b/src/org/nwapw/abacus/Abacus.java index 0522874..e64342f 100644 --- a/src/org/nwapw/abacus/Abacus.java +++ b/src/org/nwapw/abacus/Abacus.java @@ -3,7 +3,7 @@ package org.nwapw.abacus; import org.nwapw.abacus.plugin.PluginManager; //import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.window.Window; -import org.nwapw.abacus.plugin.ClassFinderV2; +import org.nwapw.abacus.plugin.ClassFinder; import javax.swing.*; import java.io.File; @@ -26,14 +26,14 @@ public class Abacus { e.printStackTrace(); } manager = new PluginManager(); - ArrayList names = new ArrayList(); + ArrayList names = new ArrayList<>(); try { - ClassFinderV2 classFinder = new ClassFinderV2(); - File pluginFile = new File("C:\\Users\\galbraithja\\Desktop\\.git\\abacus\\src\\org\\nwapw\\abacus\\plugin"); + ClassFinder classFinder = new ClassFinder(); + File pluginFile = new File("plugins"); for(File classes:pluginFile.listFiles()){ if(classes.getName().endsWith(".jar")){ - names.addAll(classFinder.addJar("C:\\Users\\galbraithja\\Desktop\\.git\\abacus\\src\\org\\nwapw\\abacus\\plugin\\Standard.jar")); + names.addAll(classFinder.addJar(classes)); } } for(String name:names){ @@ -44,9 +44,7 @@ public class Abacus { manager.addClass(classGet); } - } catch (IOException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { + } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } mainUi = new Window(manager); diff --git a/src/org/nwapw/abacus/plugin/ClassFinder.java b/src/org/nwapw/abacus/plugin/ClassFinder.java index c914f3d..7e530d8 100644 --- a/src/org/nwapw/abacus/plugin/ClassFinder.java +++ b/src/org/nwapw/abacus/plugin/ClassFinder.java @@ -1,65 +1,51 @@ -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 +package org.nwapw.abacus.plugin; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class ClassFinder { + ArrayList classes; + URL[] urls; + public ClassFinder(){ + classes = new ArrayList(); + } + public ArrayList addJar(String path) throws IOException, ClassNotFoundException { + //urls = new URL[]{new URL("jar:file:" + path + "!/")}; + return addJar(new File(path)); + } + public ArrayList addJar(File jarLocation) throws IOException, ClassNotFoundException { + String path = jarLocation.getPath(); + urls = new URL[]{new URL("jar:file:" + path + "!/")}; + URLClassLoader classLoader = URLClassLoader.newInstance(urls); + JarFile jarFolder = new JarFile(jarLocation); + Enumeration jarList = jarFolder.entries(); + ArrayList classNames = new ArrayList(); + while(jarList.hasMoreElements()){ + JarEntry tempJar = (JarEntry)jarList.nextElement(); + if(tempJar.getName().endsWith(".class")){ + //System.out.println(tempJar.getName()); + classNames.add(tempJar.getName()); + classes.add(classLoader.loadClass(tempJar.getName().replace('/','.').substring(0,tempJar.getName().length()-6))); + } + } + return classNames; + } + public ArrayList getClasses(){ + return classes; + } + public Class getClass(int number){ + return classes.get(number); + } + public void delClasses(){ + classes=new ArrayList(); + } + public int classCount(){ + return classes.size(); + } +} diff --git a/src/org/nwapw/abacus/plugin/ClassFinderV2.java b/src/org/nwapw/abacus/plugin/ClassFinderV2.java deleted file mode 100644 index 3cdba13..0000000 --- a/src/org/nwapw/abacus/plugin/ClassFinderV2.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.nwapw.abacus.plugin; - -import java.io.File; -import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -public class ClassFinderV2 { - ArrayList classes; - URL[] urls; - public ClassFinderV2(){ - classes = new ArrayList(); - } - public ArrayList addJar(String path) throws IOException, ClassNotFoundException { - //urls = new URL[]{new URL("jar:file:" + path + "!/")}; - return addJar(new File(path)); - } - public ArrayList addJar(File jarLocation) throws IOException, ClassNotFoundException { - String path = jarLocation.getPath(); - urls = new URL[]{new URL("jar:file:" + path + "!/")}; - URLClassLoader classLoader = URLClassLoader.newInstance(urls); - JarFile jarFolder = new JarFile(jarLocation); - Enumeration jarList = jarFolder.entries(); - ArrayList classNames = new ArrayList(); - while(jarList.hasMoreElements()){ - JarEntry tempJar = (JarEntry)jarList.nextElement(); - if(tempJar.getName().endsWith(".class")){ - //System.out.println(tempJar.getName()); - classNames.add(tempJar.getName()); - classes.add(classLoader.loadClass(tempJar.getName().replace('/','.').substring(0,tempJar.getName().length()-6))); - } - } - return classNames; - } - public ArrayList getClasses(){ - return classes; - } - public Class getClass(int number){ - return classes.get(number); - } - public void delClasses(){ - classes=new ArrayList(); - } - public int classCount(){ - return classes.size(); - } -} diff --git a/src/org/nwapw/abacus/plugin/PluginManager.java b/src/org/nwapw/abacus/plugin/PluginManager.java index ff3b784..62b8bb1 100644 --- a/src/org/nwapw/abacus/plugin/PluginManager.java +++ b/src/org/nwapw/abacus/plugin/PluginManager.java @@ -113,7 +113,7 @@ public class PluginManager { * @param newClass the new class to instantiate. */ public void addClass(Class newClass){ - if(!Plugin.class.isAssignableFrom(newClass)) return; + if(!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return; try { addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this)); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {