RecordMethodFacatory.java

  1. package com.gh.mygreen.xlsmapper.fieldprocessor;

  2. import java.lang.reflect.Method;
  3. import java.util.Optional;

  4. import com.gh.mygreen.xlsmapper.Configuration;
  5. import com.gh.mygreen.xlsmapper.annotation.XlsIgnorable;
  6. import com.gh.mygreen.xlsmapper.annotation.XlsListener;
  7. import com.gh.mygreen.xlsmapper.annotation.XlsPostLoad;
  8. import com.gh.mygreen.xlsmapper.annotation.XlsPostSave;
  9. import com.gh.mygreen.xlsmapper.annotation.XlsPreLoad;
  10. import com.gh.mygreen.xlsmapper.annotation.XlsPreSave;
  11. import com.gh.mygreen.xlsmapper.util.ArgUtils;
  12. import com.gh.mygreen.xlsmapper.util.Utils;
  13. import com.gh.mygreen.xlsmapper.xml.AnnotationReader;

  14. /**
  15.  * {@link RecordMethodCache}のインスタンスを作成するファクトリクラス。
  16.  *
  17.  * @since 2.0
  18.  * @author T.TSUCHIE
  19.  *
  20.  */
  21. public class RecordMethodFacatory {

  22.     private final AnnotationReader annoReader;

  23.     private final Configuration config;

  24.     /**
  25.      * コンストラクタ
  26.      * @param annoReader XMLで定義したアノテーション情報を提供するクラス。
  27.      * @param config システム設定
  28.      * @throws IllegalArgumentException {@literal annoReader == null or config == null.}
  29.      */
  30.     public RecordMethodFacatory(final AnnotationReader annoReader, final Configuration config) {
  31.         ArgUtils.notNull(annoReader, "annoReader");
  32.         ArgUtils.notNull(config, "config");

  33.         this.annoReader = annoReader;
  34.         this.config = config;
  35.     }

  36.     /**
  37.      * レコードクラスを元に、{@link RecordMethodCache}のインスタンスを組み立てる。
  38.      *
  39.      * @param recordClass レコードクラス
  40.      * @param processCase 現在の処理ケース
  41.      * @return {@link RecordMethodCache}のインスタンス
  42.      * @throws IllegalArgumentException {@literal recordClass == null}
  43.      */
  44.     public RecordMethodCache create(final Class<?> recordClass, final ProcessCase processCase) {

  45.         ArgUtils.notNull(recordClass, "recordClass");

  46.         final RecordMethodCache recordMethod = new RecordMethodCache();

  47.         setupIgnoreableMethod(recordMethod, recordClass, processCase);
  48.         setupListenerCallbackMethods(recordMethod, recordClass);
  49.         setupRecordCallbackMethods(recordMethod, recordClass);

  50.         return recordMethod;
  51.     }

  52.     /**
  53.      * レコードの値を無視すると判定するためのメソッドの抽出
  54.      * @param recordMethod メソッドの格納先
  55.      * @param recordClass レコードクラス
  56.      * @param processCase 現在の処理ケース
  57.      */
  58.     private void setupIgnoreableMethod(final RecordMethodCache recordMethod,
  59.             final Class<?> recordClass, final ProcessCase processCase) {

  60.         for(Method method : recordClass.getMethods()) {
  61.             method.setAccessible(true);

  62.             XlsIgnorable ignoreAnno = annoReader.getAnnotation(method, XlsIgnorable.class);
  63.             if(ignoreAnno == null) {
  64.                 continue;
  65.             }

  66.             if(!Utils.isProcessCase(processCase, ignoreAnno.cases())) {
  67.                 continue;
  68.             }

  69.             if(method.getParameterCount() > 0) {
  70.                 continue;
  71.             }

  72.             if(!method.getReturnType().equals(Boolean.TYPE)) {
  73.                 continue;
  74.             }

  75.             recordMethod.ignoreableMethod = Optional.of(method);
  76.             return;
  77.         }
  78.     }

  79.     /**
  80.      * リスナークラスに定義されているコールバックメソッドの抽出
  81.      *
  82.      * @param recordMethod メソッドの格納先
  83.      * @param recordClass レコードクラス
  84.      */
  85.     private void setupListenerCallbackMethods(final RecordMethodCache recordMethod, final Class<?> recordClass) {

  86.         // リスナーオブジェクトに定義されたコールバックメソッドの抽出
  87.         final XlsListener listenerAnno = annoReader.getAnnotation(recordClass, XlsListener.class);
  88.         if(listenerAnno == null) {
  89.             return;
  90.         }

  91.         final Class<?>[] listenerClasses = listenerAnno.value();

  92.         for(Class<?> listenerClass : listenerClasses) {

  93.             ListenerClassCache listenerCache = new ListenerClassCache(config.createBean(listenerClass));
  94.             recordMethod.lisnterClasses.add(listenerCache);

  95.             for(Method method : listenerClass.getMethods()) {
  96.                 method.setAccessible(true);

  97.                 if(annoReader.hasAnnotation(method, XlsPreLoad.class)) {
  98.                     listenerCache.preLoadMethods.add(method);

  99.                 } else if(annoReader.hasAnnotation(method, XlsPostLoad.class)) {
  100.                     listenerCache.postLoadMethods.add(method);

  101.                 } else if(annoReader.hasAnnotation(method, XlsPreSave.class)) {
  102.                     listenerCache.preSaveMethods.add(method);

  103.                 } else if(annoReader.hasAnnotation(method, XlsPostSave.class)) {
  104.                     listenerCache.postSaveMethods.add(method);

  105.                 }

  106.             }
  107.         }

  108.     }

  109.     /**
  110.      * レコードクラスに定義されているコールバックメソッドの抽出
  111.      *
  112.      * @param recordMethod メソッドの格納先
  113.      * @param recordClass レコードクラス
  114.      */
  115.     private void setupRecordCallbackMethods(final RecordMethodCache recordMethod, final Class<?> recordClass) {

  116.         for(Method method : recordClass.getMethods()) {
  117.             method.setAccessible(true);

  118.             if(annoReader.hasAnnotation(method, XlsPreLoad.class)) {
  119.                 recordMethod.preLoadMethods.add(method);

  120.             } else if(annoReader.hasAnnotation(method, XlsPostLoad.class)) {
  121.                 recordMethod.postLoadMethods.add(method);

  122.             } else if(annoReader.hasAnnotation(method, XlsPreSave.class)) {
  123.                 recordMethod.preSaveMethods.add(method);

  124.             } else if(annoReader.hasAnnotation(method, XlsPostSave.class)) {
  125.                 recordMethod.postSaveMethods.add(method);

  126.             }

  127.         }

  128.     }

  129. }