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

org.sql2o.reflection.ReflectionObjectConstructorFactory Maven / Gradle / Ivy

There is a newer version: 0.2.6
Show newest version
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);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy