Skip to content

Commit

Permalink
Merge pull request ggp-org#13 from nitay/inject
Browse files Browse the repository at this point in the history
Add support for user-injected classes to project searching.
  • Loading branch information
samschreiber committed Oct 25, 2013
2 parents b4e6eb4 + f58a569 commit 7d6ee3e
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions src/org/ggp/base/util/reflection/ProjectSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,44 +16,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 List<String> findAllClasses()

private static void findClassesInList(Set<String> classesToSearch, Class<?> ofThisType,
boolean mustBeConcrete, List<Class<?>> 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<String> allClasses = findAllClasses();
private static Set<String> injectedClasses = Sets.newHashSet();

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

private static Set<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>();

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

return rval;
}
}
}

0 comments on commit 7d6ee3e

Please sign in to comment.