fix Issue 20

This commit is contained in:
Loki 2011-01-20 16:37:12 +02:00
parent ac80cc9fd1
commit 465e8c3093

View file

@ -164,44 +164,50 @@ public abstract class ExpansionSet implements Serializable {
return null; return null;
} }
protected ArrayList<Class> getCardClassesForPackage(String packageName) { private ArrayList<Class> getCardClassesForPackage(String packageName) {
ArrayList<Class> classes = new ArrayList<Class>(); ClassLoader classLoader = this.getClass().getClassLoader();
// Get a File object for the package assert classLoader != null;
File directory = null; String path = packageName.replace('.', '/');
String fullPath; Enumeration<URL> resources = null;
String relPath = packageName.replace('.', '/'); try {
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath); resources = classLoader.getResources(path);
if (resource == null) { } catch (IOException e) {
throw new RuntimeException("No resource for " + relPath); e.printStackTrace();
} }
fullPath = resource.getFile(); List<File> dirs = new ArrayList<File>();
directory = new File(fullPath); while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
dirs.add(new File(resource.getFile()));
}
ArrayList<Class> classes = new ArrayList<Class>();
for (File directory : dirs) {
try {
classes.addAll(findClasses(directory, packageName));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return classes;
}
try { private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "").replaceAll("%20", " "); List<Class> classes = new ArrayList<Class>();
JarFile jarFile = new JarFile(jarPath); if (!directory.exists()) {
Enumeration<JarEntry> entries = jarFile.entries(); return classes;
while(entries.hasMoreElements()) { }
JarEntry entry = entries.nextElement(); File[] files = directory.listFiles();
String entryName = entry.getName(); for (File file : files) {
if(entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) { if (file.isDirectory()) {
String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", ""); assert !file.getName().contains(".");
try { classes.addAll(findClasses(file, packageName + "." + file.getName()));
Class clazz = Class.forName(className); } else if (file.getName().endsWith(".class")) {
if (CardImpl.class.isAssignableFrom(clazz)) { Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
classes.add(clazz); if (CardImpl.class.isAssignableFrom(c))
} classes.add(c);
} }
catch (ClassNotFoundException e) { }
throw new RuntimeException("ClassNotFoundException loading " + className); return classes;
} }
}
}
} catch (IOException e) {
throw new RuntimeException(packageName + " (" + directory + ") does not appear to be a valid package", e);
}
return classes;
}
private Map<Rarity, List<Class>> getCardsByRarity() { private Map<Rarity, List<Class>> getCardsByRarity() {
Map<Rarity, List<Class>> cardsByRarity = new HashMap<Rarity, List<Class>>(); Map<Rarity, List<Class>> cardsByRarity = new HashMap<Rarity, List<Class>>();