Skip to content

Commit

Permalink
Add support for user-injected classes to project searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitay Joffe committed Oct 24, 2013
1 parent b5eb509 commit 62abe68
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/org/ggp/base/util/reflection/ProjectSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Stack;

import com.google.common.collect.Lists;
import org.ggp.base.player.gamer.Gamer;
import org.ggp.base.util.configuration.ProjectConfiguration;

Expand All @@ -17,44 +18,55 @@ public static void main(String[] args)
{
System.out.println(getAllClassesThatAre(Gamer.class));
}
public static List<Class<?>> getAllClassesThatAre(Class<?> ofThisType)

public static List<Class<?>> getAllClassesThatAre(Class<?> ofThisType)
{
return getAllClassesThatAre(ofThisType, true);
}

public static List<Class<?>> getAllClassesThatAre(Class<?> ofThisType, boolean mustBeConcrete)
{
List<Class<?>> rval = new ArrayList<Class<?>>();
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<String> allClasses = findAllClasses();


private static void findClassesInList(List<String> listToSearch, Class<?> ofThisType,
boolean mustBeConcrete, List<Class<?>> rval) {
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);
}
}

private static List<String> allClasses = findAllClasses();
private static List<String> injectedClasses = Lists.newArrayList();

public static <T> void injectClass(Class<T> klass) {
injectedClasses.add(klass.getCanonicalName());
}

private static List<String> findAllClasses()
{
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith(".");
}
};

List<String> rval = new ArrayList<String>();
Stack<File> toProcess = new Stack<File>();
Stack<File> toProcess = new Stack<File>();
for(String classDirName : ProjectConfiguration.classRoots)
toProcess.add(new File(classDirName));
while(!toProcess.empty())
Expand All @@ -67,7 +79,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, '.'), "");
Expand All @@ -79,7 +91,7 @@ public boolean accept(File dir, String name) {
}
}
}

return rval;
}
}
}

0 comments on commit 62abe68

Please sign in to comment.