Search in sources :

Example 1 with ConstructorAccessor

use of sun.reflect.ConstructorAccessor in project sql2o by aaberg.

the class MethodAccessorsGenerator method newConstructor.

@Override
public ObjectConstructor newConstructor(final Class<?> cls) {
    for (Class<?> cls0 = cls; cls != Object.class; cls0 = cls0.getSuperclass()) {
        try {
            Constructor<?> ctor = cls0.getDeclaredConstructor();
            final ConstructorAccessor constructorAccessor = (cls0 == cls) ? newConstructorAccessor(ctor) : newConstructorAccessor(ctor, cls);
            return new ObjectConstructor() {

                @Override
                public Object newInstance() {
                    try {
                        return constructorAccessor.newInstance(null);
                    } catch (InstantiationException | InvocationTargetException e) {
                        throw new Sql2oException("Could not create a new instance of class " + cls, e);
                    }
                }
            };
        } catch (NoSuchMethodException e) {
        // ignore
        }
    }
    return UnsafeFieldSetterFactory.getConstructor(cls);
}
Also used : Sql2oException(org.sql2o.Sql2oException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstructorAccessor(sun.reflect.ConstructorAccessor)

Example 2 with ConstructorAccessor

use of sun.reflect.ConstructorAccessor in project jdk8u_jdk by JetBrains.

the class Constructor method newInstance.

/**
     * Uses the constructor represented by this {@code Constructor} object to
     * create and initialize a new instance of the constructor's
     * declaring class, with the specified initialization parameters.
     * Individual parameters are automatically unwrapped to match
     * primitive formal parameters, and both primitive and reference
     * parameters are subject to method invocation conversions as necessary.
     *
     * <p>If the number of formal parameters required by the underlying constructor
     * is 0, the supplied {@code initargs} array may be of length 0 or null.
     *
     * <p>If the constructor's declaring class is an inner class in a
     * non-static context, the first argument to the constructor needs
     * to be the enclosing instance; see section 15.9.3 of
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * <p>If the required access and argument checks succeed and the
     * instantiation will proceed, the constructor's declaring class
     * is initialized if it has not already been initialized.
     *
     * <p>If the constructor completes normally, returns the newly
     * created and initialized instance.
     *
     * @param initargs array of objects to be passed as arguments to
     * the constructor call; values of primitive types are wrapped in
     * a wrapper object of the appropriate type (e.g. a {@code float}
     * in a {@link java.lang.Float Float})
     *
     * @return a new object created by calling the constructor
     * this object represents
     *
     * @exception IllegalAccessException    if this {@code Constructor} object
     *              is enforcing Java language access control and the underlying
     *              constructor is inaccessible.
     * @exception IllegalArgumentException  if the number of actual
     *              and formal parameters differ; if an unwrapping
     *              conversion for primitive arguments fails; or if,
     *              after possible unwrapping, a parameter value
     *              cannot be converted to the corresponding formal
     *              parameter type by a method invocation conversion; if
     *              this constructor pertains to an enum type.
     * @exception InstantiationException    if the class that declares the
     *              underlying constructor represents an abstract class.
     * @exception InvocationTargetException if the underlying constructor
     *              throws an exception.
     * @exception ExceptionInInitializerError if the initialization provoked
     *              by this method fails.
     */
@CallerSensitive
public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, null, modifiers);
        }
    }
    if ((clazz.getModifiers() & Modifier.ENUM) != 0)
        throw new IllegalArgumentException("Cannot reflectively create enum objects");
    // read volatile
    ConstructorAccessor ca = constructorAccessor;
    if (ca == null) {
        ca = acquireConstructorAccessor();
    }
    @SuppressWarnings("unchecked") T inst = (T) ca.newInstance(initargs);
    return inst;
}
Also used : ConstructorAccessor(sun.reflect.ConstructorAccessor) CallerSensitive(sun.reflect.CallerSensitive)

Example 3 with ConstructorAccessor

use of sun.reflect.ConstructorAccessor in project jdk8u_jdk by JetBrains.

the class Constructor method acquireConstructorAccessor.

// NOTE that there is no synchronization used here. It is correct
// (though not efficient) to generate more than one
// ConstructorAccessor for a given Constructor. However, avoiding
// synchronization will probably make the implementation more
// scalable.
private ConstructorAccessor acquireConstructorAccessor() {
    // First check to see if one has been created yet, and take it
    // if so.
    ConstructorAccessor tmp = null;
    if (root != null)
        tmp = root.getConstructorAccessor();
    if (tmp != null) {
        constructorAccessor = tmp;
    } else {
        // Otherwise fabricate one and propagate it up to the root
        tmp = reflectionFactory.newConstructorAccessor(this);
        setConstructorAccessor(tmp);
    }
    return tmp;
}
Also used : ConstructorAccessor(sun.reflect.ConstructorAccessor)

Aggregations

ConstructorAccessor (sun.reflect.ConstructorAccessor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Sql2oException (org.sql2o.Sql2oException)1 CallerSensitive (sun.reflect.CallerSensitive)1