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

View File

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

View File

@ -44,4 +44,5 @@ public @interface ConfigurableField {
public String defaultValue() default ""; public String defaultValue() default "";
public String description() default ""; public String description() default "";
public String category() default "General"; 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.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.ListIterator;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; 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.") @ConfigurableField(name = "Autosave Changes", description = "If unchecked, changes are only saved when the Save button is pressed.")
public static boolean saveAutomatically = false; public static boolean saveAutomatically = false;
static {
buildTree();
}
public static void buildTree() { public static void buildTree() {
BASE = new ConfigNode(new Configuration()); BASE = new ConfigNode(new Configuration());
buildTree(BASE, new LinkedHashSet()); buildTree(BASE, new LinkedHashSet());
@ -308,7 +300,8 @@ public class Configuration implements Reconfigurable {
node.setRawFieldValue(f.getName(), (Serializable) o); node.setRawFieldValue(f.getName(), (Serializable) o);
} }
continue; continue;
} else if (o == null) { }
if (o == null) {
continue; continue;
} }

View File

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