OSDN Git Service

Auto-save
authorTakeyuki NAGAO <nagaotakeyuki@gmail.com>
Mon, 26 Mar 2012 07:55:53 +0000 (16:55 +0900)
committerTakeyuki NAGAO <nagaotakeyuki@gmail.com>
Mon, 26 Mar 2012 07:55:53 +0000 (16:55 +0900)
src/jp/sourceforge/dvibrowser/dvi2epub/cmd/CommandUtils.java
src/jp/sourceforge/dvibrowser/dvi2epub/cmd/OptionAdapter.java
src/jp/sourceforge/dvibrowser/dvi2epub/cmd/OptionMapper.java
src/jp/sourceforge/dvibrowser/dvi2epub/opt/DefaultOptionMapper.java

index 2cfefc5..4df2cc8 100644 (file)
@@ -1,5 +1,6 @@
 package jp.sourceforge.dvibrowser.dvi2epub.cmd;
 
+
 public class CommandUtils {
        private CommandUtils() {}
 
@@ -43,4 +44,5 @@ public class CommandUtils {
                        return null;
                return value;
        }
+       
 }
index 97e383e..63ef3d2 100644 (file)
@@ -18,34 +18,15 @@ final class OptionAdapter extends MemberWalkerAdapter {
 
        public void processMethod(Object o, Method method)
                        throws MemberWalkerException {
-               Annotation[] annotations = method.getDeclaredAnnotations();
                try {
-                       for (Annotation a : annotations) {
-                               String shortName = null, longName = null;
-                               
-                               try {
-                                       Method m = a.getClass().getDeclaredMethod("shortName",
-                                                               new Class<?>[] {});
-                                       shortName = (String) m.invoke(a);
-                               } catch (NoSuchMethodException ex) {
-                                       // Ignored.
-                               }
-                               try {
-                                       Method m = a.getClass().getDeclaredMethod("longName",
-                                                       new Class<?>[] {});
-                                       longName = (String) m.invoke(a);
-                               } catch (NoSuchMethodException ex) {
-                                       // Ignored.
-                               }
-
-                               if (nameHits(state.getOptionName(), shortName, longName)) {
-                                       Dispatcher<Object[]> d = new Dispatcher<Object []>(getMapper(), "map", a);
-                                       if (d.dispatch()) {
-                                               Object [] args = d.getResult();
-//                                             System.out.println("Dispatch result: " + Arrays.toString(args));
-                                               method.invoke(o, args);
-                                               setDone(true);
-                                       }
+                       Annotation a = mapper.getAnnotationForOption(method, state.getOptionName());
+                       if (a != null) {
+                               Dispatcher<Object[]> d = new Dispatcher<Object[]>(mapper, "map", a);
+                               if (d.dispatch()) {
+                                       Object[] args = d.getResult();
+                                       method.invoke(o, args);
+                                       setDone(true);
+                                       return;
                                }
                        }
                } catch (Exception ex) {
@@ -60,12 +41,4 @@ final class OptionAdapter extends MemberWalkerAdapter {
                return mapper;
        }
 
-       private boolean nameHits(String name, String shortName, String longName) {
-               if (shortName != null && shortName.equals(name)) {
-                       return true;
-               } else if (longName != null && longName.equals(name)) {
-                       return true;
-               }
-               return false;
-       }
 }
\ No newline at end of file
index bafa228..0f1d1e0 100644 (file)
@@ -1,5 +1,9 @@
 package jp.sourceforge.dvibrowser.dvi2epub.cmd;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
 public interface OptionMapper {
        ParserState getState();
+       Annotation getAnnotationForOption(Method method, String optionName);
 }
index 871be8b..e834c9d 100644 (file)
@@ -1,5 +1,9 @@
 package jp.sourceforge.dvibrowser.dvi2epub.opt;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
 import jp.sourceforge.dvibrowser.dvi2epub.cmd.CommandUtils;
 import jp.sourceforge.dvibrowser.dvi2epub.cmd.OptionMapper;
 import jp.sourceforge.dvibrowser.dvi2epub.cmd.ParserState;
@@ -33,6 +37,54 @@ implements OptionMapper
                return new Object[] { value };
        }
        
+       public Annotation getAnnotationForOption(Method method, String optionName) {
+               Annotation[] annotations = method.getDeclaredAnnotations();
+               for (Annotation a : annotations) {
+                       String shortName = null, longName = null;
+
+                       try {
+                               Method m = a.getClass().getDeclaredMethod("shortName",
+                                               new Class<?>[] {});
+                               shortName = (String) m.invoke(a);
+                       } catch (NoSuchMethodException ex) {
+                               // Ignored.
+                       } catch (IllegalArgumentException e) {
+                               // Ignored.
+                       } catch (IllegalAccessException e) {
+                               // Ignored.
+                       } catch (InvocationTargetException e) {
+                               // Ignored.
+                       }
+                       try {
+                               Method m = a.getClass().getDeclaredMethod("longName",
+                                               new Class<?>[] {});
+                               longName = (String) m.invoke(a);
+                       } catch (NoSuchMethodException ex) {
+                               // Ignored.
+                       } catch (IllegalArgumentException e) {
+                               // Ignored.
+                       } catch (IllegalAccessException e) {
+                               // Ignored.
+                       } catch (InvocationTargetException e) {
+                               // Ignored.
+                       }
+
+                       if (nameHits(optionName, shortName, longName)) {
+                               return a;
+                       }
+               }
+               return null;
+       }
+       
+       private static boolean nameHits(String name, String shortName, String longName) {
+               if (shortName != null && shortName.equals(name)) {
+                       return true;
+               } else if (longName != null && longName.equals(name)) {
+                       return true;
+               }
+               return false;
+       }
+       
        public ParserState getState() {
                return state;
        }