Created ReflectionHelper
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 3 Sep 2019 12:06:16 +0000 (14:06 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 3 Sep 2019 12:06:16 +0000 (14:06 +0200)
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/GUIComponentTestbench.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ClassLoaderBasedResourceLoader.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/IndirectGUIComponentCreator.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ReflectionHelper.java [new file with mode: 0644]
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/CodeSnippetSupplier.java

index 7f234a4..f8abe44 100644 (file)
@@ -23,7 +23,8 @@ public class Am2900Machine implements Machine
        {
                this.machineDefinition = am2900MachineDefinition;
                viewModel = new ViewModelModifiable();
-               IndirectGUIComponentCreator.createComponent(viewModel, "resloader:Am2900Loader:jsonres:components/GUIAm2900.json");
+               IndirectGUIComponentCreator.createComponent(viewModel,
+                               "resloader:Am2900Loader:jsonres:net/mograsim/logic/model/am2900/components/GUIAm2900.json");
                LogicModelParameters params = new LogicModelParameters();
                params.gateProcessTime = 50;
                params.wireTravelTime = 10;
index fbf9f2f..cbe6207 100644 (file)
@@ -26,7 +26,8 @@ public class GUIComponentTestbench
        public static void createTestbench(ViewModelModifiable model)
        {
                Am2900Loader.setup();
-               GUIComponent comp = IndirectGUIComponentCreator.createComponent(model, "resloader:Am2900Loader:jsonres:components/GUIAm2900.json");
+               GUIComponent comp = IndirectGUIComponentCreator.createComponent(model,
+                               "resloader:Am2900Loader:jsonres:net/mograsim/logic/model/am2900/components/GUIAm2900.json");
 
                List<String> inputPinNames = new ArrayList<>();
                List<String> outputPinNames = new ArrayList<>();
index d12c4b3..3a2623e 100644 (file)
@@ -14,7 +14,7 @@ public abstract class ClassLoaderBasedResourceLoader implements ResourceLoader
        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException
        {
-               return Class.forName(name, true, getClassLoader());// TODO duplication
+               return ReflectionHelper.tryInvokeStaticInitializer(name, getClassLoader());
        }
 
        public abstract ClassLoader getClassLoader();
index 2ac2c47..681580e 100644 (file)
@@ -15,7 +15,6 @@ import com.google.gson.JsonObject;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.GUIComponent;
 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
-import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
 import net.mograsim.logic.model.util.JsonHandler;
 
 public class IndirectGUIComponentCreator
@@ -213,7 +212,7 @@ public class IndirectGUIComponentCreator
 
        private static void tryLoadResourceLoader(String loaderClassName)
        {
-               CodeSnippetSupplier.tryInvokeStaticInitializer(loaderClassName, "Error loading resoruce loader %s: %s\n");
+               ReflectionHelper.tryInvokeStaticInitializer(loaderClassName, "Error loading resoruce loader %s: %s\n");
        }
 
        public static interface ComponentSupplier
diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ReflectionHelper.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ReflectionHelper.java
new file mode 100644 (file)
index 0000000..1a6a66a
--- /dev/null
@@ -0,0 +1,36 @@
+package net.mograsim.logic.model.serializing;
+
+public class ReflectionHelper
+{
+       private ReflectionHelper()
+       {
+       }
+
+       public static Class<?> tryInvokeStaticInitializer(String className, String errorMessageFormat)
+       {
+               return tryInvokeStaticInitializer(className, errorMessageFormat, ReflectionHelper.class.getClassLoader());
+       }
+
+       public static Class<?> tryInvokeStaticInitializer(String className, String errorMessageFormat, ClassLoader classLoader)
+       {
+               try
+               {
+                       return tryInvokeStaticInitializer(className, classLoader);
+               }
+               catch (ClassNotFoundException e)
+               {
+                       System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());
+                       return null;
+               }
+       }
+
+       public static Class<?> tryInvokeStaticInitializer(String className) throws ClassNotFoundException
+       {
+               return tryInvokeStaticInitializer(className, ReflectionHelper.class.getClassLoader());
+       }
+
+       public static Class<?> tryInvokeStaticInitializer(String className, ClassLoader classLoader) throws ClassNotFoundException
+       {
+               return Class.forName(className, true, classLoader);
+       }
+}
\ No newline at end of file
index 26d5a0d..22ebc09 100644 (file)
@@ -4,6 +4,8 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import net.mograsim.logic.model.serializing.ReflectionHelper;
+
 public class CodeSnippetSupplier<C, S>
 {
        private final Map<String, String> standardSnippetIDClassNames = new HashMap<>();
@@ -65,19 +67,6 @@ public class CodeSnippetSupplier<C, S>
 
        private static void tryLoadSnippetClass(String snippetClassName)
        {
-               tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
+               ReflectionHelper.tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
        }
-
-       public static void tryInvokeStaticInitializer(String className, String errorMessageFormat)
-       {
-               try
-               {
-                       Class.forName(className, true, CodeSnippetSupplier.class.getClassLoader());
-               }
-               catch (ClassNotFoundException e)
-               {
-                       System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());
-               }
-       }
-
 }
\ No newline at end of file