diff --git a/src/org/ggp/base/util/reflection/ProjectSearcher.java b/src/org/ggp/base/util/reflection/ProjectSearcher.java index 77c9e040a..eed8a93db 100644 --- a/src/org/ggp/base/util/reflection/ProjectSearcher.java +++ b/src/org/ggp/base/util/reflection/ProjectSearcher.java @@ -3,11 +3,10 @@ import java.io.File; import java.io.FilenameFilter; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Stack; +import java.util.*; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import org.ggp.base.player.gamer.Gamer; import org.ggp.base.util.configuration.ProjectConfiguration; @@ -17,44 +16,55 @@ public static void main(String[] args) { System.out.println(getAllClassesThatAre(Gamer.class)); } - - public static List> getAllClassesThatAre(Class ofThisType) + + public static List> getAllClassesThatAre(Class ofThisType) { return getAllClassesThatAre(ofThisType, true); } - + public static List> getAllClassesThatAre(Class ofThisType, boolean mustBeConcrete) { List> rval = new ArrayList>(); - for(String name : allClasses) { - if(name.contains("Test_")) - continue; - - Class c = null; - try { - c = Class.forName(name); - } catch (ClassNotFoundException ex) { - throw new RuntimeException(ex); - } - - if(ofThisType.isAssignableFrom(c) && (!mustBeConcrete || !Modifier.isAbstract(c.getModifiers())) ) - rval.add(c); - } - return rval; + findClassesInList(allClasses, ofThisType, mustBeConcrete, rval); + findClassesInList(injectedClasses, ofThisType, mustBeConcrete, rval); + return rval; } - - private static List allClasses = findAllClasses(); - - private static List findAllClasses() + + private static void findClassesInList(Set classesToSearch, Class ofThisType, + boolean mustBeConcrete, List> rval) { + for(String name : classesToSearch) { + if(name.contains("Test_")) + continue; + + Class c = null; + try { + c = Class.forName(name); + } catch (ClassNotFoundException ex) { + throw new RuntimeException(ex); + } + + if(ofThisType.isAssignableFrom(c) && (!mustBeConcrete || !Modifier.isAbstract(c.getModifiers())) ) + rval.add(c); + } + } + + private static Set allClasses = findAllClasses(); + private static Set injectedClasses = Sets.newHashSet(); + + public static void injectClass(Class klass) { + injectedClasses.add(klass.getCanonicalName()); + } + + private static Set findAllClasses() { FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return !name.startsWith("."); } }; - - List rval = new ArrayList(); - Stack toProcess = new Stack(); + + Set rval = Sets.newHashSet(); + Stack toProcess = new Stack(); for(String classDirName : ProjectConfiguration.classRoots) toProcess.add(new File(classDirName)); while(!toProcess.empty()) @@ -67,7 +77,7 @@ public boolean accept(File dir, String name) { else { if(f.getName().endsWith(".class")) - { + { String fullyQualifiedName = f.getPath(); for(String classDirName : ProjectConfiguration.classRoots) { fullyQualifiedName = fullyQualifiedName.replaceAll("^" + classDirName.replace(File.separatorChar, '.'), ""); @@ -79,7 +89,7 @@ public boolean accept(File dir, String name) { } } } - + return rval; } -} +}