Package org.sql2o.reflection

Source Code of org.sql2o.reflection.ReflectionObjectConstructorFactory

package org.sql2o.reflection;

import org.sql2o.Sql2oException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectionObjectConstructorFactory implements ObjectConstructorFactory {
    public ObjectConstructor newConstructor(final Class<?> clazz) {
        try {
            final Constructor<?> ctor = clazz.getDeclaredConstructor();
            ctor.setAccessible(true);
            return new ObjectConstructor() {
                public Object newInstance() {
                    try {
                        return ctor.newInstance((Object[])null);
                    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                        throw new Sql2oException("Could not create a new instance of class " + clazz, e);
                    }
                }
            };
        } catch (Throwable e) {
            throw new Sql2oException("Could not find parameter-less constructor of class " + clazz, e);
        }
    }
}
TOP

Related Classes of org.sql2o.reflection.ReflectionObjectConstructorFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.