From 90c2fbd3866f087d9519479977438151f9f2365f Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Tue, 21 Nov 2017 00:34:03 +0100 Subject: [PATCH] SeSimClassLoader almost complete --- nbproject/project.properties | 2 +- src/gui/Globals.java | 4 +- src/sesim/AutoTraderLoader.java | 4 +- src/sesim/IndicatorLoader.java | 32 +----------- src/sesim/SeSimClassLoader.java | 92 +++++++++++++++++++++++---------- 5 files changed, 71 insertions(+), 63 deletions(-) diff --git a/nbproject/project.properties b/nbproject/project.properties index 0324795..1c38dd5 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -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.in.editor=false annotation.processing.processors.list= diff --git a/src/gui/Globals.java b/src/gui/Globals.java index ed55760..93b591a 100644 --- a/src/gui/Globals.java +++ b/src/gui/Globals.java @@ -150,9 +150,9 @@ public class Globals { tloader = new AutoTraderLoader(default_pathlist); - IndicatorLoader il = new IndicatorLoader<>(Indicator.class); + SeSimClassLoader il = new SeSimClassLoader<>(Indicator.class); il.setDefaultPathList(default_pathlist); - il.getInstalled(); + ArrayList>ires = il.getInstalled(); } diff --git a/src/sesim/AutoTraderLoader.java b/src/sesim/AutoTraderLoader.java index 76d76bb..ca81be6 100644 --- a/src/sesim/AutoTraderLoader.java +++ b/src/sesim/AutoTraderLoader.java @@ -39,7 +39,7 @@ public class AutoTraderLoader extends SeSimClassLoader { private ArrayList> traders_cache = null; public AutoTraderLoader(ArrayList pathlist) { - super(pathlist); + super(AutoTraderInterface.class, pathlist); } private ClassLoader cl; @@ -78,7 +78,7 @@ public class AutoTraderLoader extends SeSimClassLoader { ArrayList> trl; ArrayList> result = new ArrayList<>(); - trl = getInstalledClasses(new ArrayList(), AutoTraderInterface.class); + trl = getInstalledClasses(new ArrayList()); for (Class c : trl) { result.add((Class) c); } diff --git a/src/sesim/IndicatorLoader.java b/src/sesim/IndicatorLoader.java index 27102d7..649055b 100644 --- a/src/sesim/IndicatorLoader.java +++ b/src/sesim/IndicatorLoader.java @@ -33,37 +33,9 @@ import java.util.ArrayList; */ public class IndicatorLoader extends SeSimClassLoader { - ArrayList> cache; - final Class class_type; - - - public IndicatorLoader(Class class_type){ - this.class_type=class_type; + public IndicatorLoader(Class class_type) { + super(class_type); } - /** - * Get a list of all traders found in class path - * - * @return List of traders - */ - public ArrayList> getInstalled() { - - if (cache != null) { - return cache; - } - - Class tube ; - - ArrayList> trl; - ArrayList> result = new ArrayList<>(); - trl = getInstalledClasses(new ArrayList(), class_type); - for (Class c : trl) { - result.add((Class) c); - } - - cache = result; - return cache; - - } } diff --git a/src/sesim/SeSimClassLoader.java b/src/sesim/SeSimClassLoader.java index 562f358..33e2b1e 100644 --- a/src/sesim/SeSimClassLoader.java +++ b/src/sesim/SeSimClassLoader.java @@ -41,9 +41,30 @@ import java.util.logging.Level; * * @author 7u83 <7u83@mail.ru> */ -public class SeSimClassLoader { +public class SeSimClassLoader { protected ArrayList default_pathlist; + private ArrayList> cache; + final Class class_type; + + /** + * Create a SeSimClassLoader object with an empty default path + * @param class_type + */ + public SeSimClassLoader(Class 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 class_type, ArrayList pathlist) { + this.class_type = class_type; + setDefaultPathList(pathlist); + } /** * Set the path list where to search for traders @@ -55,22 +76,6 @@ public class SeSimClassLoader { } - /** - * 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 pathlist) { - setDefaultPathList(pathlist); - } - /** * Get a list of all files in a given directory and its sub-directories. * @@ -114,8 +119,8 @@ public class SeSimClassLoader { } /** - * Check if a given class provides a certain interface and also if the - * class is not abstract, so it could be instanciated. + * Check if a given class provides a certain interface and also if the class + * is not abstract, so it could be instanciated. * * @param cls Class to check * @param iface Interface which the class should provide @@ -138,7 +143,7 @@ public class SeSimClassLoader { return false; } - private Class loadClass(String directory, String class_name, Class iface) { + private Class loadClass(String directory, String class_name) { if (class_name == null) { return null; @@ -163,8 +168,8 @@ public class SeSimClassLoader { return null; } - if (iface != null) { - if (!hasInterface(cls, iface)) { + if (class_type != null) { + if (!hasInterface(cls, class_type)) { return null; } } @@ -173,7 +178,7 @@ public class SeSimClassLoader { return null; } - return cls; + return (Class)cls; } catch (ClassNotFoundException ex) { return null; @@ -187,11 +192,16 @@ public class SeSimClassLoader { * @param iface * @return */ - public ArrayList> getInstalledClasses(ArrayList additional_pathlist, Class iface) { + public ArrayList> getInstalledClasses(ArrayList additional_pathlist){ - ArrayList> result = new ArrayList<>(); + if (cache != null) { + return cache; + } + + ArrayList> result = new ArrayList<>(); ArrayList pathlist = new ArrayList<>(); + pathlist.addAll(default_pathlist); pathlist.addAll(additional_pathlist); @@ -209,7 +219,7 @@ public class SeSimClassLoader { class_name = fn.substring(path.length()); class_name = class_name.substring(1, class_name.length() - 6).replace('/', '.'); - Class c = loadClass(path, class_name, iface); + Class c = loadClass(path, class_name); if (null == c) { continue; } @@ -235,7 +245,7 @@ public class SeSimClassLoader { class_name = class_name.substring(0, class_name.length() - 6).replace('/', '.'); - Class c = loadClass(path, class_name, iface); + Class c = loadClass(path, class_name); if (null == c) { continue; } @@ -248,8 +258,9 @@ public class SeSimClassLoader { } finally { try { - if (jarstream != null) + if (jarstream != null) { jarstream.close(); + } } catch (IOException ex) { java.util.logging.Logger.getLogger(AutoTraderLoader.class.getName()).log(Level.SEVERE, null, ex); } @@ -263,4 +274,29 @@ public class SeSimClassLoader { return result; } + /** + * Get a list of all traders found in class path + * + * @return List of traders + */ + public ArrayList> getInstalled() { + + if (cache != null) { + return cache; + } + + Class tube; + + ArrayList> trl; + ArrayList> result = new ArrayList<>(); + trl = getInstalledClasses(new ArrayList()); //, class_type); + for (Class c : trl) { + result.add((Class) c); + } + + cache = result; + return cache; + + } + }