Sfoglia il codice sorgente

Instantiate PAL class dynamically depending on parameter

Baglio Tabifata 4 anni fa
parent
commit
cc74e510ae
1 ha cambiato i file con 15 aggiunte e 22 eliminazioni
  1. 15 22
      src/main/java/info/hkzlab/dupal/analyzer/App.java

+ 15 - 22
src/main/java/info/hkzlab/dupal/analyzer/App.java

@@ -1,5 +1,7 @@
 package info.hkzlab.dupal.analyzer;
 
+import java.lang.reflect.InvocationTargetException;
+
 import org.slf4j.*;
 
 import info.hkzlab.dupal.analyzer.board.boardio.*;
@@ -18,10 +20,10 @@ public class App {
     public static void main(String[] args) throws Exception {
         System.out.println("DuPAL Analyzer " + version);
 
-        if(args.length < 3) {
-            logger.error("Wrong number of arguments passed.\n"+
-                        "dupal_analyzer <serial_port> <pal_type> <output_dir> [hex_output_mask]\n" +
-                        "Where <pal_type> can be: 16R8, 16R6, 16R4, 16L8\n");
+        if (args.length < 3) {
+            logger.error("Wrong number of arguments passed.\n"
+                    + "dupal_analyzer <serial_port> <pal_type> <output_dir> [hex_output_mask]\n"
+                    + "Where <pal_type> can be: 16R8, 16R6, 16R4, 16L8\n");
 
             return;
         }
@@ -31,7 +33,7 @@ public class App {
         DuPALManager dpm = new DuPALManager(serialDevice);
         DuPALAnalyzer dpan = new DuPALAnalyzer(dpm, pspecs, outMask, outDir);
 
-        if(!dpm.enterRemoteMode()) {
+        if (!dpm.enterRemoteMode()) {
             System.out.println("Unable to put DuPAL board in REMOTE MODE!");
             System.exit(-1);
         }
@@ -41,23 +43,14 @@ public class App {
 
     private static void parseArgs(String[] args) {
         serialDevice = args[0];
-        
-        switch(args[1].toUpperCase()) {
-            case "16R8":
-                pspecs = new PAL16R8Specs();
-                break;
-            case "16R6":
-                pspecs = new PAL16R6Specs();
-                break;
-            case "16R4":
-                pspecs = new PAL16R4Specs();
-                break;
-            case "16L8":
-                pspecs = new PAL16L8Specs();
-                break;
-            default:
-                logger.error("Invalid PAL type selected.");
-                System.exit(-1);
+
+        try {
+            Class<?> specsClass = Class.forName("info.hkzlab.dupal.analyzer.devices.PAL" + args[1].toUpperCase() + "Specs");
+            pspecs = (PALSpecs) specsClass.getConstructor().newInstance(new Object[]{});
+        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
+                | InvocationTargetException | NoSuchMethodException | SecurityException e) {
+            logger.error("Invalid PAL type selected.");
+            System.exit(-1);
         }
 
         outDir = args[2];