SeSimClassLoader almost complete

This commit is contained in:
7u83 2017-11-21 00:34:03 +01:00
parent 3f07b9e002
commit 90c2fbd386
5 changed files with 71 additions and 63 deletions

View File

@ -1,4 +1,4 @@
#Mon, 20 Nov 2017 09:48:54 +0100 #Mon, 20 Nov 2017 23:21:04 +0100
annotation.processing.enabled=true annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false annotation.processing.enabled.in.editor=false
annotation.processing.processors.list= annotation.processing.processors.list=

View File

@ -150,9 +150,9 @@ public class Globals {
tloader = new AutoTraderLoader(default_pathlist); tloader = new AutoTraderLoader(default_pathlist);
IndicatorLoader<Indicator> il = new IndicatorLoader<>(Indicator.class); SeSimClassLoader<Indicator> il = new SeSimClassLoader<>(Indicator.class);
il.setDefaultPathList(default_pathlist); il.setDefaultPathList(default_pathlist);
il.getInstalled(); ArrayList<Class<Indicator>>ires = il.getInstalled();
} }

View File

@ -39,7 +39,7 @@ public class AutoTraderLoader extends SeSimClassLoader<AutoTraderInterface> {
private ArrayList<Class<AutoTraderInterface>> traders_cache = null; private ArrayList<Class<AutoTraderInterface>> traders_cache = null;
public AutoTraderLoader(ArrayList<String> pathlist) { public AutoTraderLoader(ArrayList<String> pathlist) {
super(pathlist); super(AutoTraderInterface.class, pathlist);
} }
private ClassLoader cl; private ClassLoader cl;
@ -78,7 +78,7 @@ public class AutoTraderLoader extends SeSimClassLoader<AutoTraderInterface> {
ArrayList<Class<?>> trl; ArrayList<Class<?>> trl;
ArrayList<Class<AutoTraderInterface>> result = new ArrayList<>(); ArrayList<Class<AutoTraderInterface>> result = new ArrayList<>();
trl = getInstalledClasses(new ArrayList(), AutoTraderInterface.class); trl = getInstalledClasses(new ArrayList());
for (Class<?> c : trl) { for (Class<?> c : trl) {
result.add((Class<AutoTraderInterface>) c); result.add((Class<AutoTraderInterface>) c);
} }

View File

@ -33,37 +33,9 @@ import java.util.ArrayList;
*/ */
public class IndicatorLoader<T> extends SeSimClassLoader { public class IndicatorLoader<T> extends SeSimClassLoader {
ArrayList<Class<T>> cache; public IndicatorLoader(Class class_type) {
final Class<T> class_type; super(class_type);
public IndicatorLoader(Class<T> class_type){
this.class_type=class_type;
} }
/**
* Get a list of all traders found in class path
*
* @return List of traders
*/
public ArrayList<Class<T>> getInstalled() {
if (cache != null) {
return cache;
}
Class<?> tube ;
ArrayList<Class<?>> trl;
ArrayList<Class<T>> result = new ArrayList<>();
trl = getInstalledClasses(new ArrayList(), class_type);
for (Class<?> c : trl) {
result.add((Class<T>) c);
}
cache = result;
return cache;
}
} }

View File

@ -41,9 +41,30 @@ import java.util.logging.Level;
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class SeSimClassLoader <T> { public class SeSimClassLoader<T> {
protected ArrayList<String> default_pathlist; protected ArrayList<String> default_pathlist;
private ArrayList<Class<T>> cache;
final Class<T> class_type;
/**
* Create a SeSimClassLoader object with an empty default path
* @param class_type
*/
public SeSimClassLoader(Class<T> class_type) {
this(class_type, new ArrayList<>());
}
/**
* Create a SeSimClassLoader object with fiven default path
*
* @param class_type
* @param pathlist Default path to search classes for
*/
public SeSimClassLoader(Class<T> class_type, ArrayList<String> pathlist) {
this.class_type = class_type;
setDefaultPathList(pathlist);
}
/** /**
* Set the path list where to search for traders * Set the path list where to search for traders
@ -55,22 +76,6 @@ public class SeSimClassLoader <T> {
} }
/**
* Create a SeSimClassLoader object with an empty default path
*/
public SeSimClassLoader() {
this(new ArrayList<>());
}
/**
* Create a SeSimClassLoader object with fiven default path
*
* @param pathlist Default path to search classes for
*/
public SeSimClassLoader(ArrayList<String> pathlist) {
setDefaultPathList(pathlist);
}
/** /**
* Get a list of all files in a given directory and its sub-directories. * Get a list of all files in a given directory and its sub-directories.
* *
@ -114,8 +119,8 @@ public class SeSimClassLoader <T> {
} }
/** /**
* Check if a given class provides a certain interface and also if the * Check if a given class provides a certain interface and also if the class
* class is not abstract, so it could be instanciated. * is not abstract, so it could be instanciated.
* *
* @param cls Class to check * @param cls Class to check
* @param iface Interface which the class should provide * @param iface Interface which the class should provide
@ -138,7 +143,7 @@ public class SeSimClassLoader <T> {
return false; return false;
} }
private Class<?> loadClass(String directory, String class_name, Class<?> iface) { private Class<T> loadClass(String directory, String class_name) {
if (class_name == null) { if (class_name == null) {
return null; return null;
@ -163,8 +168,8 @@ public class SeSimClassLoader <T> {
return null; return null;
} }
if (iface != null) { if (class_type != null) {
if (!hasInterface(cls, iface)) { if (!hasInterface(cls, class_type)) {
return null; return null;
} }
} }
@ -173,7 +178,7 @@ public class SeSimClassLoader <T> {
return null; return null;
} }
return cls; return (Class<T>)cls;
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
return null; return null;
@ -187,11 +192,16 @@ public class SeSimClassLoader <T> {
* @param iface * @param iface
* @return * @return
*/ */
public ArrayList<Class<?>> getInstalledClasses(ArrayList<String> additional_pathlist, Class<?> iface) { public ArrayList<Class<T>> getInstalledClasses(ArrayList<String> additional_pathlist){
ArrayList<Class<?>> result = new ArrayList<>(); if (cache != null) {
return cache;
}
ArrayList<Class<T>> result = new ArrayList<>();
ArrayList<String> pathlist = new ArrayList<>(); ArrayList<String> pathlist = new ArrayList<>();
pathlist.addAll(default_pathlist); pathlist.addAll(default_pathlist);
pathlist.addAll(additional_pathlist); pathlist.addAll(additional_pathlist);
@ -209,7 +219,7 @@ public class SeSimClassLoader <T> {
class_name = fn.substring(path.length()); class_name = fn.substring(path.length());
class_name = class_name.substring(1, class_name.length() - 6).replace('/', '.'); class_name = class_name.substring(1, class_name.length() - 6).replace('/', '.');
Class<?> c = loadClass(path, class_name, iface); Class<T> c = loadClass(path, class_name);
if (null == c) { if (null == c) {
continue; continue;
} }
@ -235,7 +245,7 @@ public class SeSimClassLoader <T> {
class_name = class_name.substring(0, class_name.length() - 6).replace('/', '.'); class_name = class_name.substring(0, class_name.length() - 6).replace('/', '.');
Class<?> c = loadClass(path, class_name, iface); Class<T> c = loadClass(path, class_name);
if (null == c) { if (null == c) {
continue; continue;
} }
@ -248,8 +258,9 @@ public class SeSimClassLoader <T> {
} finally { } finally {
try { try {
if (jarstream != null) if (jarstream != null) {
jarstream.close(); jarstream.close();
}
} catch (IOException ex) { } catch (IOException ex) {
java.util.logging.Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex);
} }
@ -263,4 +274,29 @@ public class SeSimClassLoader <T> {
return result; return result;
} }
/**
* Get a list of all traders found in class path
*
* @return List of traders
*/
public ArrayList<Class<T>> getInstalled() {
if (cache != null) {
return cache;
}
Class<?> tube;
ArrayList<Class<?>> trl;
ArrayList<Class<T>> result = new ArrayList<>();
trl = getInstalledClasses(new ArrayList()); //, class_type);
for (Class<?> c : trl) {
result.add((Class<T>) c);
}
cache = result;
return cache;
}
} }