1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Remove old ClassFinder and hardcoded folder names, and fix class filter.

This commit is contained in:
Danila Fedorin 2017-07-28 11:35:23 -07:00
parent 53065f8374
commit 701de050fd
4 changed files with 58 additions and 125 deletions

View File

@ -3,7 +3,7 @@ package org.nwapw.abacus;
import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.plugin.PluginManager;
//import org.nwapw.abacus.plugin.StandardPlugin; //import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.window.Window; import org.nwapw.abacus.window.Window;
import org.nwapw.abacus.plugin.ClassFinderV2; import org.nwapw.abacus.plugin.ClassFinder;
import javax.swing.*; import javax.swing.*;
import java.io.File; import java.io.File;
@ -26,14 +26,14 @@ public class Abacus {
e.printStackTrace(); e.printStackTrace();
} }
manager = new PluginManager(); manager = new PluginManager();
ArrayList<String> names = new ArrayList(); ArrayList<String> names = new ArrayList<>();
try { try {
ClassFinderV2 classFinder = new ClassFinderV2(); ClassFinder classFinder = new ClassFinder();
File pluginFile = new File("C:\\Users\\galbraithja\\Desktop\\.git\\abacus\\src\\org\\nwapw\\abacus\\plugin"); File pluginFile = new File("plugins");
for(File classes:pluginFile.listFiles()){ for(File classes:pluginFile.listFiles()){
if(classes.getName().endsWith(".jar")){ 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){ for(String name:names){
@ -44,9 +44,7 @@ public class Abacus {
manager.addClass(classGet); manager.addClass(classGet);
} }
} catch (IOException e) { } catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
mainUi = new Window(manager); mainUi = new Window(manager);

View File

@ -1,56 +1,39 @@
package org.nwapw.abacus.plugin; package org.nwapw.abacus.plugin;
import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.zip.ZipInputStream;
public class ClassFinder extends ClassLoader{
public class ClassFinder {
ArrayList<Class> classes; ArrayList<Class> classes;
URL[] urls;
public ClassFinder(){ public ClassFinder(){
super(ClassFinder.class.getClassLoader()); classes = new ArrayList();
classes=new ArrayList();
} }
public Class loadClass(String className) throws ClassNotFoundException{ public ArrayList<String> addJar(String path) throws IOException, ClassNotFoundException {
return findClass(className); //urls = new URL[]{new URL("jar:file:" + path + "!/")};
return addJar(new File(path));
} }
public ArrayList<String> loadClass(File jarLocation) throws ClassNotFoundException, IOException{ public ArrayList<String> addJar(File jarLocation) throws IOException, ClassNotFoundException {
return addJar(jarLocation); String path = jarLocation.getPath();
} urls = new URL[]{new URL("jar:file:" + path + "!/")};
public ArrayList<String> addJar(File jarLocation) throws IOException { URLClassLoader classLoader = URLClassLoader.newInstance(urls);
JarFile jarFolder = new JarFile(jarLocation); JarFile jarFolder = new JarFile(jarLocation);
Enumeration jarList = jarFolder.entries(); Enumeration jarList = jarFolder.entries();
HashMap classSize = new HashMap();
HashMap classContent = new HashMap();
ArrayList<String> classNames = new ArrayList(); ArrayList<String> classNames = new ArrayList();
JarEntry tempJar;
ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarLocation)));
while(jarList.hasMoreElements()){ while(jarList.hasMoreElements()){
tempJar = (JarEntry)jarList.nextElement(); JarEntry tempJar = (JarEntry)jarList.nextElement();
zipStream.getNextEntry(); if(tempJar.getName().endsWith(".class")){
if(!tempJar.isDirectory()) { //System.out.println(tempJar.getName());
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()); classNames.add(tempJar.getName());
classes.add(classLoader.loadClass(tempJar.getName().replace('/','.').substring(0,tempJar.getName().length()-6)));
} }
} }
}
jarFolder.close();
for(String name:classNames) {
classes.add(super.defineClass(name, (byte[]) classContent.get(name), 0, (int) classSize.get(name)));
}
return classNames; return classNames;
} }
public ArrayList<Class> getClasses(){ public ArrayList<Class> getClasses(){
@ -62,4 +45,7 @@ public class ClassFinder extends ClassLoader{
public void delClasses(){ public void delClasses(){
classes=new ArrayList(); classes=new ArrayList();
} }
public int classCount(){
return classes.size();
}
} }

View File

@ -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<Class> classes;
URL[] urls;
public ClassFinderV2(){
classes = new ArrayList();
}
public ArrayList<String> addJar(String path) throws IOException, ClassNotFoundException {
//urls = new URL[]{new URL("jar:file:" + path + "!/")};
return addJar(new File(path));
}
public ArrayList<String> 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<String> 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<Class> getClasses(){
return classes;
}
public Class getClass(int number){
return classes.get(number);
}
public void delClasses(){
classes=new ArrayList();
}
public int classCount(){
return classes.size();
}
}

View File

@ -113,7 +113,7 @@ public class PluginManager {
* @param newClass the new class to instantiate. * @param newClass the new class to instantiate.
*/ */
public void addClass(Class<?> newClass){ public void addClass(Class<?> newClass){
if(!Plugin.class.isAssignableFrom(newClass)) return; if(!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return;
try { try {
addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this)); addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {