All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mygreen.supercsv.builder.DefaultBeanFactory Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package com.github.mygreen.supercsv.builder;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Objects;

import org.supercsv.exception.SuperCsvReflectionException;
import org.supercsv.util.BeanInterfaceProxy;


/**
 * Beanのインスタンスを生成する標準のクラス。
 *
 * @version 2.2
 * @since 2.0
 * @author T.TSUCHIE
 *
 */
public class DefaultBeanFactory implements BeanFactory, Object> {

    @Override
    public Object create(final Class clazz) {
        Objects.requireNonNull(clazz, "clazz should not be null.");

        try {
            if(clazz.isInterface()) {
                return BeanInterfaceProxy.createProxy(clazz);

            } else {

                final Class declaredClass = clazz.getDeclaringClass();

                if(declaredClass != null && !Modifier.isStatic(clazz.getModifiers())) {
                    // 非staticな内部クラスの場合
                    Constructor cons = clazz.getDeclaredConstructor(declaredClass);
                    cons.setAccessible(true);
                    return cons.newInstance((Object)null);
                }

                Constructor cons = clazz.getDeclaredConstructor();
                cons.setAccessible(true);
                return cons.newInstance();

            }
        } catch (ReflectiveOperationException  e) {
            throw new SuperCsvReflectionException(String.format("fail create Bean instance of '%s'", clazz.getName()), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy