Browse Source

adding new Commandline processor without Reflections

Wilfried Klaas 6 years ago
parent
commit
0af47bf717

+ 2 - 7
pom.xml

@@ -91,7 +91,7 @@
 					<skipTests>true</skipTests>
 				</configuration>
 			</plugin>
-			<plugin>
+<!-- 			<plugin>
 				<groupId>org.apache.maven.plugins</groupId>
 				<artifactId>maven-gpg-plugin</artifactId>
 				<version>1.6</version>
@@ -116,7 +116,7 @@
 					<autoReleaseAfterClose>true</autoReleaseAfterClose>
 				</configuration>
 			</plugin>
-		</plugins>
+ -->		</plugins>
 	</build>
 	<dependencies>
 		<dependency>
@@ -142,10 +142,5 @@
 			<artifactId>javax.activation</artifactId>
 			<version>1.2.0</version>
 		</dependency>
-		<dependency>
-			<groupId>org.reflections</groupId>
-			<artifactId>reflections</artifactId>
-			<version>0.9.11</version>
-		</dependency>
 	</dependencies>
 </project>

+ 30 - 40
src/main/java/de/mcs/utils/jsap/CommandlineProcessor.java

@@ -21,6 +21,7 @@
  */
 package de.mcs.utils.jsap;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -28,12 +29,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
-
-import org.reflections.Reflections;
-import org.reflections.scanners.FieldAnnotationsScanner;
-import org.reflections.scanners.MethodAnnotationsScanner;
-import org.reflections.scanners.TypeAnnotationsScanner;
 
 import com.martiansoftware.jsap.FlaggedOption;
 import com.martiansoftware.jsap.JSAP;
@@ -53,7 +48,6 @@ public class CommandlineProcessor {
   private static JSAPResult commandLineArgs;
 
   private static Map<String, Method> parameterMethods;
-  private static Reflections reflections;
   private static Command helpContext;
 
   public static void processCommandline(Class class1, String[] args) {
@@ -152,20 +146,21 @@ public class CommandlineProcessor {
    * This function parses the commandline parameters. If the parameter -h or
    * --help is avalible, generate a little help text.
    * 
-   * @param class1
+   * @param mainClass
    * 
    * @param args
    *          Commadline arguments
    */
-  public static void parseParameters(Class class1, final String[] args) {
-    reflections = new Reflections("", new TypeAnnotationsScanner(), new MethodAnnotationsScanner(),
-        new FieldAnnotationsScanner());
+  public static void parseParameters(Class mainClass, final String[] args) {
 
     parser = new JSAP();
     try {
-      registerHelpContext(class1);
+      // registering the main help context, if availble
+      registerHelpContext(mainClass);
+
       // registering the parameters with default values
-      registerDefaultParameter();
+      registerDefaultParameter(mainClass);
+
       // parsing the commadline
       commandLineArgs = parser.parse(args);
     } catch (JSAPException e) {
@@ -176,8 +171,8 @@ public class CommandlineProcessor {
     }
   }
 
-  private static void registerHelpContext(Class class1) {
-    Command annotation = (Command) class1.getAnnotation(Command.class);
+  private static void registerHelpContext(Class mainClass) {
+    Command annotation = (Command) mainClass.getAnnotation(Command.class);
     if (annotation != null) {
       helpContext = annotation;
     }
@@ -186,20 +181,17 @@ public class CommandlineProcessor {
   /**
    * registering the parameters to the JSAP Parser.
    * 
+   * @param mainClass
+   * 
    * @throws JSAPException
    *           Something is going wrong
    */
-  private static void registerDefaultParameter() throws JSAPException {
+  private static void registerDefaultParameter(Class mainClass) throws JSAPException {
     Map<Integer, UnflaggedOption> unflaggedOptions = new HashMap<>();
 
-    // MethodAnnotationsScanner
-    Set<Method> methodes = reflections.getMethodsAnnotatedWith(SwitchOption.class);
+    List<Method> methodes = getMethodsAnnotatedWith(mainClass, SwitchOption.class);
     for (Method method : methodes) {
       SwitchOption annotation = method.getAnnotation(SwitchOption.class);
-      // System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s,
-      // req: %s\r\n", annotation.shortKey(),
-      // annotation.longKey(), annotation.name(), annotation.help(),
-      // annotation.defaultValue(), annotation.required());
 
       com.martiansoftware.jsap.Switch swtOption = new com.martiansoftware.jsap.Switch(annotation.name(),
           annotation.shortKey(), annotation.longKey());
@@ -211,20 +203,9 @@ public class CommandlineProcessor {
       parameterMethods.put(annotation.name(), method);
     }
 
-    // // FieldAnnotationsScanner
-    // Set<Field> fields = reflections.getFieldsAnnotatedWith(Switch.class);
-    // for (Field field : fields) {
-    // System.out.println(field.getName());
-    // }
-
-    // MethodAnnotationsScanner
-    methodes = reflections.getMethodsAnnotatedWith(StringOption.class);
+    methodes = getMethodsAnnotatedWith(mainClass, StringOption.class);
     for (Method method : methodes) {
       StringOption annotation = method.getAnnotation(StringOption.class);
-      // System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s,
-      // req: %s\r\n", annotation.shortKey(),
-      // annotation.longKey(), annotation.name(), annotation.help(),
-      // annotation.defaultValue(), annotation.required());
 
       StringStringParser stringParser = StringStringParser.getParser();
       if (annotation.index() > 0) {
@@ -241,14 +222,9 @@ public class CommandlineProcessor {
       }
     }
 
-    // MethodAnnotationsScanner
-    methodes = reflections.getMethodsAnnotatedWith(FileOption.class);
+    methodes = getMethodsAnnotatedWith(mainClass, FileOption.class);
     for (Method method : methodes) {
       FileOption annotation = method.getAnnotation(FileOption.class);
-      // System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s,
-      // req: %s\r\n", annotation.shortKey(),
-      // annotation.longKey(), annotation.name(), annotation.help(),
-      // annotation.defaultValue(), annotation.required());
 
       FileStringParser fileStringParser = FileStringParser.getParser();
       fileStringParser.setMustBeDirectory(annotation.mustBeDirectory());
@@ -268,6 +244,7 @@ public class CommandlineProcessor {
       }
     }
 
+    // sorting unflagged options
     List<Integer> indexes = new ArrayList<>();
     unflaggedOptions.keySet().forEach(e -> indexes.add(e));
     indexes.sort((x, y) -> {
@@ -275,8 +252,21 @@ public class CommandlineProcessor {
       return (diff == 0) ? 1 : diff;
     });
 
+    // registering the unflagged options in the right order
     for (Integer e : indexes) {
       parser.registerParameter(unflaggedOptions.get(e));
     }
   }
+
+  private static List<Method> getMethodsAnnotatedWith(Class class1, Class annotationClass) {
+    List<Method> list = new ArrayList<>();
+    Method[] methods = class1.getMethods();
+    for (Method method : methods) {
+      Annotation[] annotations = method.getAnnotationsByType(annotationClass);
+      if (annotations.length > 0) {
+        list.add(method);
+      }
+    }
+    return list;
+  }
 }

+ 8 - 0
src/main/java/de/mcs/utils/jsap/package-info.java

@@ -0,0 +1,8 @@
+/**
+ * 
+ */
+/**
+ * @author w.klaas
+ *
+ */
+package de.mcs.utils.jsap;