View Javadoc
1   package com.github.mygreen.supercsv.builder;
2   
3   import java.lang.reflect.InvocationTargetException;
4   import java.lang.reflect.Method;
5   import java.util.Objects;
6   
7   import org.supercsv.exception.SuperCsvReflectionException;
8   import org.supercsv.util.CsvContext;
9   
10  import com.github.mygreen.supercsv.validation.CsvBindingErrors;
11  import com.github.mygreen.supercsv.validation.ValidationContext;
12  
13  /**
14   * 
15   *
16   * @since 2.0
17   * @author T.TSUCHIE
18   *
19   */
20  public class CallbackMethod implements Comparable<CallbackMethod> {
21      
22      protected final Method method;
23      
24      public CallbackMethod(final Method method) {
25          Objects.requireNonNull(method);
26          
27          method.setAccessible(true);
28          this.method = method;
29          
30      }
31      
32      /**
33       * メソッドの実行
34       * @param record Beanのオブジェクト
35       * @param csvContext 現在のCSVのレコード情報
36       * @param bindingErrors エラー情報。
37       * @param beanMapping マッピング情報
38       * @throws SuperCsvReflectionException メソッドの実行に失敗した場合。
39       */
40      public void invoke(final Object record, final CsvContext csvContext, final CsvBindingErrors bindingErrors, 
41              final BeanMapping<?> beanMapping) {
42          
43          // 引数の組み立て
44          final Class<?>[] paramTypes = method.getParameterTypes();
45          final Object[] paramValues =  new Object[paramTypes.length];
46          
47          for(int i=0; i < paramTypes.length; i++) {
48              
49              if(CsvContext.class.isAssignableFrom(paramTypes[i])) {
50                  paramValues[i] = csvContext;
51                  
52              } else if(CsvBindingErrors.class.isAssignableFrom(paramTypes[i])) {
53                  paramValues[i] = bindingErrors;
54                  
55              } else if(paramTypes[i].isArray() && Class.class.isAssignableFrom(paramTypes[i].getComponentType())) {
56                  paramValues[i] = beanMapping.getGroups();
57                  
58              } else if(ValidationContext.class.isAssignableFrom(paramTypes[i])) {
59                  paramValues[i] = new ValidationContext<>(csvContext, beanMapping);
60                  
61              } else if(beanMapping.getType().isAssignableFrom(paramTypes[i])) {
62                  paramValues[i] = record;
63                  
64              } else {
65                  paramValues[i] = null;
66              }
67              
68          }
69          
70          execute(record, paramValues);
71          
72      }
73      
74      protected void execute(final Object record, final Object[] paramValues) {
75          try {
76              method.invoke(record, paramValues);
77              
78          } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
79              Throwable t = e.getCause() == null ? e : e.getCause();
80              throw new SuperCsvReflectionException(
81                      String.format("Fail execute method '%s#%s'.", record.getClass().getName(), method.getName()),
82                      t);
83          }
84          
85          
86      }
87      
88      /**
89       * メソッド名の昇順
90       */
91      @Override
92      public int compareTo(final CallbackMethod o) {
93          
94          final String name1 = method.getDeclaringClass().getName() + "#" + method.getName();
95          final String name2 = o.method.getDeclaringClass().getName() + "#" + o.method.getName();
96          
97          return name1.compareTo(name2);
98          
99      }
100     
101 }