mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-23 07:50:09 -08:00
Change ClassFinder code to be static.
This commit is contained in:
parent
5a8e13fbef
commit
0592200961
|
@ -6,7 +6,6 @@ import org.nwapw.abacus.window.Window;
|
||||||
import org.nwapw.abacus.plugin.ClassFinder;
|
import org.nwapw.abacus.plugin.ClassFinder;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
@ -26,24 +25,9 @@ public class Abacus {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
manager = new PluginManager();
|
manager = new PluginManager();
|
||||||
ArrayList<String> names = new ArrayList<>();
|
|
||||||
try {
|
try {
|
||||||
|
ClassFinder.loadJars("plugins")
|
||||||
ClassFinder classFinder = new ClassFinder();
|
.forEach(plugin -> manager.addClass(plugin));
|
||||||
File pluginFile = new File("plugins");
|
|
||||||
for(File classes:pluginFile.listFiles()){
|
|
||||||
if(classes.getName().endsWith(".jar")){
|
|
||||||
names.addAll(classFinder.addJar(classes));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(String name:names){
|
|
||||||
System.out.println(name);
|
|
||||||
}
|
|
||||||
ArrayList<Class> classes = classFinder.getClasses();
|
|
||||||
for(Class classGet:classes){
|
|
||||||
manager.addClass(classGet);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,48 +4,56 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that loads plugin classes from their jars.
|
||||||
|
*/
|
||||||
public class ClassFinder {
|
public class ClassFinder {
|
||||||
ArrayList<Class> classes;
|
|
||||||
URL[] urls;
|
public static ArrayList<Class<?>> loadJars(String filePath) throws IOException, ClassNotFoundException {
|
||||||
public ClassFinder(){
|
return loadJars(new File(filePath));
|
||||||
classes = new ArrayList();
|
|
||||||
}
|
}
|
||||||
public ArrayList<String> addJar(String path) throws IOException, ClassNotFoundException {
|
|
||||||
//urls = new URL[]{new URL("jar:file:" + path + "!/")};
|
public static ArrayList<Class<?>> loadJars(File pluginFolderPath) throws IOException, ClassNotFoundException {
|
||||||
return addJar(new File(path));
|
ArrayList<Class<?>> toReturn = new ArrayList<>();
|
||||||
|
if(!pluginFolderPath.exists()) return toReturn;
|
||||||
|
ArrayList<File> files = Files.walk(pluginFolderPath.toPath())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.filter(f -> f.getName().endsWith(".jar"))
|
||||||
|
.collect(Collectors.toCollection(ArrayList::new));
|
||||||
|
for (File file : files){
|
||||||
|
toReturn.addAll(loadJar(file));
|
||||||
|
}
|
||||||
|
return toReturn;
|
||||||
}
|
}
|
||||||
public ArrayList<String> addJar(File jarLocation) throws IOException, ClassNotFoundException {
|
|
||||||
|
public static ArrayList<Class<?>> loadJar(String path) throws IOException, ClassNotFoundException {
|
||||||
|
return loadJar(new File(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<Class<?>> loadJar(File jarLocation) throws IOException, ClassNotFoundException {
|
||||||
|
ArrayList<Class<?>> loadedClasses = new ArrayList<>();
|
||||||
String path = jarLocation.getPath();
|
String path = jarLocation.getPath();
|
||||||
urls = new URL[]{new URL("jar:file:" + path + "!/")};
|
URL[] urls = new URL[]{new URL("jar:file:" + path + "!/")};
|
||||||
|
|
||||||
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
|
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
|
||||||
JarFile jarFolder = new JarFile(jarLocation);
|
JarFile jarFolder = new JarFile(jarLocation);
|
||||||
Enumeration jarList = jarFolder.entries();
|
Enumeration jarEntityList = jarFolder.entries();
|
||||||
ArrayList<String> classNames = new ArrayList();
|
|
||||||
while(jarList.hasMoreElements()){
|
while (jarEntityList.hasMoreElements()) {
|
||||||
JarEntry tempJar = (JarEntry)jarList.nextElement();
|
JarEntry jarEntity = (JarEntry) jarEntityList.nextElement();
|
||||||
if(tempJar.getName().endsWith(".class")){
|
if (jarEntity.getName().endsWith(".class")) {
|
||||||
//System.out.println(tempJar.getName());
|
loadedClasses.add(classLoader.loadClass(jarEntity.getName().replace('/', '.').substring(0, jarEntity.getName().length() - 6)));
|
||||||
classNames.add(tempJar.getName());
|
|
||||||
classes.add(classLoader.loadClass(tempJar.getName().replace('/','.').substring(0,tempJar.getName().length()-6)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return classNames;
|
return loadedClasses;
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user