View Javadoc
1   package com.github.mygreen.supercsv.builder;
2   
3   import java.lang.reflect.Constructor;
4   import java.lang.reflect.Modifier;
5   import java.util.Objects;
6   
7   import org.supercsv.exception.SuperCsvReflectionException;
8   import org.supercsv.util.BeanInterfaceProxy;
9   
10  
11  /**
12   * Beanのインスタンスを生成する標準のクラス。
13   *
14   * @version 2.2
15   * @since 2.0
16   * @author T.TSUCHIE
17   *
18   */
19  public class DefaultBeanFactory implements BeanFactory<Class<?>, Object> {
20  
21      @Override
22      public Object create(final Class<?> clazz) {
23          Objects.requireNonNull(clazz, "clazz should not be null.");
24  
25          try {
26              if(clazz.isInterface()) {
27                  return BeanInterfaceProxy.createProxy(clazz);
28  
29              } else {
30  
31                  final Class<?> declaredClass = clazz.getDeclaringClass();
32  
33                  if(declaredClass != null && !Modifier.isStatic(clazz.getModifiers())) {
34                      // 非staticな内部クラスの場合
35                      Constructor<?> cons = clazz.getDeclaredConstructor(declaredClass);
36                      cons.setAccessible(true);
37                      return cons.newInstance((Object)null);
38                  }
39  
40                  Constructor<?> cons = clazz.getDeclaredConstructor();
41                  cons.setAccessible(true);
42                  return cons.newInstance();
43  
44              }
45          } catch (ReflectiveOperationException  e) {
46              throw new SuperCsvReflectionException(String.format("fail create Bean instance of '%s'", clazz.getName()), e);
47          }
48      }
49  }