1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50: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,65 +1,51 @@
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.IOException;
import java.io.FileInputStream; import java.net.URL;
import java.io.IOException; 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 {
ArrayList<Class> classes;
public class ClassFinder extends ClassLoader{ URL[] urls;
public ClassFinder(){
ArrayList<Class> classes; classes = new ArrayList();
}
public ClassFinder(){ public ArrayList<String> addJar(String path) throws IOException, ClassNotFoundException {
super(ClassFinder.class.getClassLoader()); //urls = new URL[]{new URL("jar:file:" + path + "!/")};
classes=new ArrayList(); return addJar(new File(path));
} }
public Class loadClass(String className) throws ClassNotFoundException{ public ArrayList<String> addJar(File jarLocation) throws IOException, ClassNotFoundException {
return findClass(className); String path = jarLocation.getPath();
} urls = new URL[]{new URL("jar:file:" + path + "!/")};
public ArrayList<String> loadClass(File jarLocation) throws ClassNotFoundException, IOException{ URLClassLoader classLoader = URLClassLoader.newInstance(urls);
return addJar(jarLocation); JarFile jarFolder = new JarFile(jarLocation);
} Enumeration jarList = jarFolder.entries();
public ArrayList<String> addJar(File jarLocation) throws IOException { ArrayList<String> classNames = new ArrayList();
JarFile jarFolder = new JarFile(jarLocation); while(jarList.hasMoreElements()){
Enumeration jarList = jarFolder.entries(); JarEntry tempJar = (JarEntry)jarList.nextElement();
HashMap classSize = new HashMap(); if(tempJar.getName().endsWith(".class")){
HashMap classContent = new HashMap(); //System.out.println(tempJar.getName());
ArrayList<String> classNames = new ArrayList(); classNames.add(tempJar.getName());
JarEntry tempJar; classes.add(classLoader.loadClass(tempJar.getName().replace('/','.').substring(0,tempJar.getName().length()-6)));
ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarLocation))); }
while(jarList.hasMoreElements()){ }
tempJar = (JarEntry)jarList.nextElement(); return classNames;
zipStream.getNextEntry(); }
if(!tempJar.isDirectory()) { public ArrayList<Class> getClasses(){
if (tempJar.getName().substring(tempJar.getName().indexOf('.')).equals(".class") && (tempJar.getName().length() < 9 || !tempJar.getName().substring(0, 9).equals("META-INF/"))) { return classes;
int size = (int)tempJar.getSize(); }
classSize.put(tempJar.getName(),new Integer((int)tempJar.getSize())); public Class getClass(int number){
byte[] bytes = new byte[size]; return classes.get(number);
zipStream.read(bytes,0,size); }
classContent.put(tempJar.getName(),bytes); public void delClasses(){
classNames.add(tempJar.getName()); classes=new ArrayList();
} }
} public int classCount(){
} return classes.size();
jarFolder.close(); }
for(String name:classNames) { }
classes.add(super.defineClass(name, (byte[]) classContent.get(name), 0, (int) classSize.get(name)));
}
return classNames;
}
public ArrayList<Class> getClasses(){
return classes;
}
public Class getClass(int number){
return classes.get(number);
}
public void delClasses(){
classes=new ArrayList();
}
}

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) {