1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-17 16:09:32 -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.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<String> names = new ArrayList();
ArrayList<String> 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);

View File

@ -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<Class> classes;
public ClassFinder(){
super(ClassFinder.class.getClassLoader());
classes=new ArrayList();
}
public Class loadClass(String className) throws ClassNotFoundException{
return findClass(className);
}
public ArrayList<String> loadClass(File jarLocation) throws ClassNotFoundException, IOException{
return addJar(jarLocation);
}
public ArrayList<String> addJar(File jarLocation) throws IOException {
JarFile jarFolder = new JarFile(jarLocation);
Enumeration jarList = jarFolder.entries();
HashMap classSize = new HashMap();
HashMap classContent = new HashMap();
ArrayList<String> 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<Class> getClasses(){
return classes;
}
public Class getClass(int number){
return classes.get(number);
}
public void delClasses(){
classes=new ArrayList();
}
}
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<Class> classes;
URL[] urls;
public ClassFinder(){
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

@ -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.
*/
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) {