From 465e8c30935d214a5eb91cc844ecf8cef3cf78b5 Mon Sep 17 00:00:00 2001 From: Loki Date: Thu, 20 Jan 2011 16:37:12 +0200 Subject: [PATCH] fix Issue 20 --- Mage/src/mage/cards/ExpansionSet.java | 80 ++++++++++++++------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/Mage/src/mage/cards/ExpansionSet.java b/Mage/src/mage/cards/ExpansionSet.java index 8d03e2f407..7a91d4d370 100644 --- a/Mage/src/mage/cards/ExpansionSet.java +++ b/Mage/src/mage/cards/ExpansionSet.java @@ -164,44 +164,50 @@ public abstract class ExpansionSet implements Serializable { return null; } - protected ArrayList getCardClassesForPackage(String packageName) { - ArrayList classes = new ArrayList(); - // Get a File object for the package - File directory = null; - String fullPath; - String relPath = packageName.replace('.', '/'); - URL resource = ClassLoader.getSystemClassLoader().getResource(relPath); - if (resource == null) { - throw new RuntimeException("No resource for " + relPath); - } - fullPath = resource.getFile(); - directory = new File(fullPath); + private ArrayList getCardClassesForPackage(String packageName) { + ClassLoader classLoader = this.getClass().getClassLoader(); + assert classLoader != null; + String path = packageName.replace('.', '/'); + Enumeration resources = null; + try { + resources = classLoader.getResources(path); + } catch (IOException e) { + e.printStackTrace(); + } + List dirs = new ArrayList(); + while (resources.hasMoreElements()) { + URL resource = resources.nextElement(); + dirs.add(new File(resource.getFile())); + } + ArrayList classes = new ArrayList(); + for (File directory : dirs) { + try { + classes.addAll(findClasses(directory, packageName)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + return classes; + } - try { - String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "").replaceAll("%20", " "); - JarFile jarFile = new JarFile(jarPath); - Enumeration entries = jarFile.entries(); - while(entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); - String entryName = entry.getName(); - if(entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) { - String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", ""); - try { - Class clazz = Class.forName(className); - if (CardImpl.class.isAssignableFrom(clazz)) { - classes.add(clazz); - } - } - catch (ClassNotFoundException e) { - throw new RuntimeException("ClassNotFoundException loading " + className); - } - } - } - } catch (IOException e) { - throw new RuntimeException(packageName + " (" + directory + ") does not appear to be a valid package", e); - } - return classes; - } + private static List findClasses(File directory, String packageName) throws ClassNotFoundException { + List classes = new ArrayList(); + if (!directory.exists()) { + return classes; + } + File[] files = directory.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + assert !file.getName().contains("."); + classes.addAll(findClasses(file, packageName + "." + file.getName())); + } else if (file.getName().endsWith(".class")) { + Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)); + if (CardImpl.class.isAssignableFrom(c)) + classes.add(c); + } + } + return classes; + } private Map> getCardsByRarity() { Map> cardsByRarity = new HashMap>();