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

Merge branch 'master' of github.com:DanilaFe/abacus

This commit is contained in:
Danila Fedorin 2017-07-27 15:28:21 -07:00
commit 029acb7747

View File

@ -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<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();
}
}