Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Remove Reflections library in favor of Guava
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed Aug 11, 2014
1 parent 607fc18 commit 0a10f85
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
2 changes: 0 additions & 2 deletions syncany-lib/build.gradle
Expand Up @@ -17,8 +17,6 @@ dependencies {
compile "org.simpleframework:simple-xml:2.7.1"
compile "com.google.guava:guava:15.0"
compile "commons-codec:commons-codec:1.8"
compile 'org.reflections:reflections:0.9.8'
compile "org.slf4j:slf4j-api:1.6.0" // for reflections
compile "org.hsqldb:hsqldb:2.3.1"
compile "com.github.zafarkhaja:java-semver:0.7.2"

Expand Down
3 changes: 0 additions & 3 deletions syncany-lib/src/main/java/org/syncany/config/Logging.java
Expand Up @@ -25,8 +25,6 @@
import java.util.logging.LogManager;
import java.util.logging.Logger;

import org.reflections.Reflections;

/**
* The logging class offers convenience functions to initialize and update the
* application's log options.
Expand Down Expand Up @@ -78,7 +76,6 @@ public synchronized static void init() {
}

private static void disableUnwantedLoggers() {
Reflections.log = null;
System.setProperty("hsqldb.reconfig_logging", "false");

if (Logger.getLogger("sun.awt.X11.timeoutTask.XToolkit") != null) {
Expand Down
85 changes: 54 additions & 31 deletions syncany-lib/src/main/java/org/syncany/plugins/Plugins.java
Expand Up @@ -25,9 +25,12 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.reflections.Reflections;
import org.syncany.util.StringUtil;

import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;

/**
* This class loads and manages all the {@link Plugin}s loaded in the classpath.
* It provides two public methods:
Expand All @@ -42,7 +45,10 @@
*/
public class Plugins {
private static final Logger logger = Logger.getLogger(Plugins.class.getSimpleName());
private static final Reflections reflections = new Reflections(Plugin.class.getPackage().getName());

private static final String PLUGIN_PACKAGE_NAME = Plugin.class.getPackage().getName();
private static final String PLUGIN_CLASS_SUFFIX = Plugin.class.getSimpleName();

private static final Map<String, Plugin> plugins = new TreeMap<String, Plugin>();

/**
Expand Down Expand Up @@ -111,42 +117,59 @@ private static void loadPlugin(String pluginId) {
return;
}

// TODO [low] Duplicate code with loadPlugins()
loadPlugins();

for (Class<? extends Plugin> pluginClass : reflections.getSubTypesOf(Plugin.class)) {
boolean canInstantiate = !Modifier.isAbstract(pluginClass.getModifiers());

String camelCaseCandidatePluginId = pluginClass.getSimpleName().replace(Plugin.class.getSimpleName(), "");
String candidatePluginId = StringUtil.toSnakeCase(camelCaseCandidatePluginId);

if (canInstantiate && candidatePluginId.equals(pluginId)) {
try {
Plugin plugin = (Plugin) pluginClass.newInstance();
plugins.put(plugin.getId(), plugin);
}
catch (Exception e) {
logger.log(Level.WARNING, "Could not load plugin (2): " + pluginClass.getName(), e);
}
}
if (plugins.containsKey(pluginId)) {
return;
}
else {
logger.log(Level.WARNING, "Could not load plugin (1): " + pluginId + " (not found or issues with loading)");
}
}

/**
* Loads all plugins in the classpath.
*
* <p>First loads all classes in the 'org.syncany.plugins' package.
* For all classes ending with the 'Plugin' suffix, it tries to load
* them, checks whether they inherit from {@link Plugin} and whether
* they can be instantiated.
*/
private static void loadPlugins() {
for (Class<? extends Plugin> pluginClass : reflections.getSubTypesOf(Plugin.class)) {
boolean canInstantiate = !Modifier.isAbstract(pluginClass.getModifiers());

String camelCasePluginId = pluginClass.getSimpleName().replace(Plugin.class.getSimpleName(), "");
String pluginId = StringUtil.toSnakeCase(camelCasePluginId);

if (canInstantiate && !plugins.containsKey(pluginId)) {
try {
Plugin plugin = (Plugin) pluginClass.newInstance();
plugins.put(plugin.getId(), plugin);
}
catch (Exception e) {
logger.log(Level.WARNING, "Could not load plugin (2): " + pluginClass.getName(), e);
try {
ImmutableSet<ClassInfo> pluginPackageSubclasses = ClassPath
.from(Thread.currentThread().getContextClassLoader())
.getTopLevelClassesRecursive(PLUGIN_PACKAGE_NAME);

for (ClassInfo classInfo : pluginPackageSubclasses) {
boolean classNameEndWithPluginSuffix = classInfo.getName().endsWith(PLUGIN_CLASS_SUFFIX);

if (classNameEndWithPluginSuffix) {
Class<?> pluginClass = classInfo.load();

String camelCasePluginId = pluginClass.getSimpleName().replace(Plugin.class.getSimpleName(), "");
String pluginId = StringUtil.toSnakeCase(camelCasePluginId);

boolean isSubclassOfPlugin = Plugin.class.isAssignableFrom(pluginClass);
boolean canInstantiate = !Modifier.isAbstract(pluginClass.getModifiers());
boolean pluginAlreadyLoaded = plugins.containsKey(pluginId);

if (isSubclassOfPlugin && canInstantiate && !pluginAlreadyLoaded) {
logger.log(Level.INFO, "- " + pluginClass.getName());

try {
Plugin plugin = (Plugin) pluginClass.newInstance();
plugins.put(plugin.getId(), plugin);
}
catch (Exception e) {
logger.log(Level.WARNING, "Could not load plugin (2): " + pluginClass.getName(), e);
}
}
}
}
}
catch (Exception e) {
throw new RuntimeException("Unable to load plugins.", e);
}
}
}

0 comments on commit 0a10f85

Please sign in to comment.