From 97bf92375a8eed8f6b529eab8baa28e4900e4091 Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sun, 8 Dec 2013 00:43:20 -0500 Subject: [PATCH] Add class enumerator for future class stuff --- .../common/classloading/ClassEnumerator.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 common/mekanism/common/classloading/ClassEnumerator.java diff --git a/common/mekanism/common/classloading/ClassEnumerator.java b/common/mekanism/common/classloading/ClassEnumerator.java new file mode 100644 index 000000000..6cc2143be --- /dev/null +++ b/common/mekanism/common/classloading/ClassEnumerator.java @@ -0,0 +1,115 @@ +package mekanism.common.classloading; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class ClassEnumerator +{ + private static Class loadClass(String className) + { + try { + return Class.forName(className); + } catch(ClassNotFoundException e) { + throw new RuntimeException("Unexpected ClassNotFoundException loading class '" + className + "'"); + } + } + + private static void processDir(File directory, String pkgname, ArrayList> classes) + { + String[] files = directory.list(); + + for(int i = 0; i < files.length; i++) + { + String fileName = files[i]; + String className = null; + + if(fileName.endsWith(".class")) + { + className = pkgname + '.' + fileName.substring(0, fileName.length() - 6); + } + + if(className != null) + { + classes.add(loadClass(className)); + } + + File subdir = new File(directory, fileName); + + if(subdir.isDirectory()) + { + processDir(subdir, pkgname + '.' + fileName, classes); + } + } + } + + private static void processJar(URL resource, String pkgname, ArrayList> classes) + { + String relPath = pkgname.replace('.', '/'); + String resPath = resource.getPath(); + String jarPath = resPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", ""); + + JarFile jarFile; + + try { + jarFile = new JarFile(jarPath); + } catch(IOException e) { + throw new RuntimeException("Unexpected IOException reading JAR File '" + jarPath + "'", e); + } + + Enumeration entries = jarFile.entries(); + + while(entries.hasMoreElements()) + { + JarEntry entry = entries.nextElement(); + String entryName = entry.getName(); + String className = null; + + if(entryName.endsWith(".class") && entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) + { + className = entryName.replace('/', '.').replace('\\', '.').replace(".class", ""); + } + + if(className != null) + { + classes.add(loadClass(className)); + } + } + } + + public static ArrayList> getClassesForPackage(Package pkg) + { + ArrayList> classes = new ArrayList>(); + + try { + String pkgname = pkg.getName(); + String relPath = pkgname.replace('.', '/'); + + URL resource = ClassLoader.getSystemClassLoader().getResource(relPath); + + if(resource == null) + { + throw new RuntimeException("Unexpected problem: No resource for " + relPath); + } + + resource.getPath(); + + if(resource.toString().startsWith("jar:")) + { + processJar(resource, pkgname, classes); + } + else { + processDir(new File(resource.getPath()), pkgname, classes); + } + } catch(Exception e) { + System.err.println("[Mekanism] Error while loading classes in package " + pkg); + e.printStackTrace(); + } + + return classes; + } +} \ No newline at end of file