Removed hacky class detection in favor of a more stable library (org.reflections).

This commit is contained in:
Brendan Robert 2015-03-29 00:30:43 -05:00
parent a52dafa8bb
commit 90e09ba53c
6 changed files with 52 additions and 48 deletions

17
pom.xml
View File

@ -11,12 +11,11 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>jace.MainApp</mainClass>
<mainClass>jace.JaceApplication</mainClass>
</properties>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
<name>badvision</name>
</organization>
<build>
@ -130,13 +129,6 @@
</build>
<dependencies>
<!-- <dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>2</version>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
<scope>system</scope>
</dependency>-->
<dependency>
<groupId>javafx-packager</groupId>
<artifactId>javafx-packager</artifactId>
@ -144,5 +136,10 @@
<systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
<scope>system</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9</version>
</dependency>
</dependencies>
</project>

View File

@ -21,11 +21,11 @@ package jace.config;
import jace.core.Utility;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*
@ -43,12 +43,12 @@ public class ClassSelection extends DynamicSelection<Class> {
@Override
public LinkedHashMap<Class, String> getSelections() {
LinkedHashMap<Class, String> selections = new LinkedHashMap<Class, String>();
List<? extends Class> allClasses = (List<? extends Class>) Utility.findAllSubclasses(template);
LinkedHashMap<Class, String> selections = new LinkedHashMap<>();
Set<? extends Class> allClasses = (Set<? extends Class>) Utility.findAllSubclasses(template);
if (!allClasses.contains(null)) {
allClasses.add(null);
}
List<Entry<Class, String>> values = new ArrayList<Map.Entry<Class, String>>();
List<Entry<Class, String>> values = new ArrayList<>();
if (allowNull()) {
values.add(new Entry<Class, String>() {
@ -71,10 +71,12 @@ public class ClassSelection extends DynamicSelection<Class> {
for (final Class c : allClasses) {
Entry<Class, String> entry = new Map.Entry<Class, String>() {
@Override
public Class getKey() {
return c;
}
@Override
public String getValue() {
if (c == null) {
return "**Empty**";
@ -85,6 +87,7 @@ public class ClassSelection extends DynamicSelection<Class> {
return c.getSimpleName();
}
@Override
public String setValue(String value) {
throw new UnsupportedOperationException("Not supported yet.");
}
@ -101,22 +104,20 @@ public class ClassSelection extends DynamicSelection<Class> {
};
values.add(entry);
}
Collections.sort(values, new Comparator<Map.Entry<? extends Class, String>>() {
public int compare(Entry<? extends Class, String> o1, Entry<? extends Class, String> o2) {
if (o1.getKey() == null) {
return -1;
}
if (o2.getKey() == null) {
return 1;
} else {
return (o1.getValue().compareTo(o2.getValue()));
}
Collections.sort(values, (Entry<? extends Class, String> o1, Entry<? extends Class, String> o2) -> {
if (o1.getKey() == null) {
return -1;
}
if (o2.getKey() == null) {
return 1;
} else {
return (o1.getValue().compareTo(o2.getValue()));
}
});
for (Map.Entry<Class, String> entry : values) {
values.stream().forEach((entry) -> {
Class key = entry.getKey();
selections.put(key, entry.getValue());
}
});
return selections;
}

View File

@ -44,4 +44,5 @@ public @interface ConfigurableField {
public String defaultValue() default "";
public String description() default "";
public String category() default "General";
public boolean enablesDevice() default false;
}

View File

@ -39,14 +39,10 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@ -265,10 +261,6 @@ public class Configuration implements Reconfigurable {
@ConfigurableField(name = "Autosave Changes", description = "If unchecked, changes are only saved when the Save button is pressed.")
public static boolean saveAutomatically = false;
static {
buildTree();
}
public static void buildTree() {
BASE = new ConfigNode(new Configuration());
buildTree(BASE, new LinkedHashSet());
@ -308,7 +300,8 @@ public class Configuration implements Reconfigurable {
node.setRawFieldValue(f.getName(), (Serializable) o);
}
continue;
} else if (o == null) {
}
if (o == null) {
continue;
}

View File

@ -34,7 +34,7 @@ public abstract class DynamicSelection<T> implements ISelection<T> {
T currentValue;
@Override
public T getValue() {
if (currentValue != null || !allowNull()) {
if (currentValue != null || allowNull()) {
return currentValue;
} else {
Iterator<? extends T> i = getSelections().keySet().iterator();

View File

@ -27,6 +27,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.reflections.Reflections;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
@ -57,12 +58,15 @@ import javax.swing.JPanel;
import javax.swing.JProgressBar;
/**
* This is a set of helper functions which do not belong anywhere else. Functions vary from introspection, discovery, and string/pattern matching.
* @author Brendan Robert (BLuRry) brendan.robert@gmail.com
* This is a set of helper functions which do not belong anywhere else.
* Functions vary from introspection, discovery, and string/pattern matching.
*
* @author Brendan Robert (BLuRry) brendan.robert@gmail.com
*/
public class Utility {
//--------------- Introspection utilities
/*
private static Set<Class> findClasses(String pckgname, Class clazz) {
Set<Class> output = new HashSet<>();
// Code from JWhich
@ -100,7 +104,6 @@ public class Utility {
try {
// Try to create an instance of the object
String className = pckgname + "." + classname;
// System.out.println("Class: " + className);
Class c = Class.forName(className);
if (clazz.isAssignableFrom(c)) {
output.add(c);
@ -177,26 +180,36 @@ public class Utility {
return output;
}
private static final Map<Class, Collection<Class>> classCache = new HashMap<>();
*/
static Reflections reflections = new Reflections("jace");
public static Set<Class> findAllSubclasses(Class clazz) {
return reflections.getSubTypesOf(clazz);
}
/*
public static List<Class> findAllSubclasses(Class clazz) {
if (classCache.containsKey(clazz)) {
return (List<Class>) classCache.get(clazz);
}
TreeMap<String, Class> allClasses = new TreeMap<>();
List<Class> values = new ArrayList(allClasses.values());
classCache.put(clazz, values);
for (Package p : Package.getPackages()) {
if (p.getName().startsWith("java")
|| p.getName().startsWith("com.sun")
|| p.getName().startsWith("sun")
|| p.getName().startsWith("com.oracle")) {
continue;
}
findClasses(p.getName(), clazz).stream().filter((c) -> !(Modifier.isAbstract(c.getModifiers()))).forEach((c) -> {
allClasses.put(c.getSimpleName(), c);
});
findClasses(p.getName(), clazz)
.stream()
.filter((c) -> !(Modifier.isAbstract(c.getModifiers())))
.forEach((c) -> {
allClasses.put(c.getSimpleName(), c);
});
}
List<Class> values = new ArrayList(allClasses.values());
classCache.put(clazz, values);
return values;
}
*/
//------------------------------ String comparators
/**
@ -299,7 +312,7 @@ public class Utility {
return super.equals(obj);
}
}
};
label.setGraphic(new ImageView(img));
label.setAlignment(Pos.CENTER);
@ -310,7 +323,6 @@ public class Utility {
return label;
}
public static void runModalProcess(String title, final Runnable runnable) {
// final JDialog frame = new JDialog(Emulator.getFrame());
final JProgressBar progressBar = new JProgressBar();
@ -577,4 +589,4 @@ public class Utility {
}
return setChild(object, paths[paths.length - 1], value, hex);
}
}
}