FileDocCategorySizeDatePackage
InstantiationPolicy.javaAPI DocGlassfish v2 API24728Tue May 22 16:54:22 BST 2007oracle.toplink.essentials.internal.descriptors

InstantiationPolicy

public class InstantiationPolicy extends Object implements Serializable, Cloneable
Purpose: Allows customization of how an object is created/instantiated.

So, here is how it works:

If there is no method specified
- all the other settings are ignored and
- the descriptor class's default constructor is invoked.
If a factory is specified
- the factoryClass and factoryClassMethod are ignored and
- the method is invoked on the factory.
If neither a factory nor a factoryClass are specified
- the factoryClassMethod is ignored and
- the method is invoked on the descriptor class (as a static).
If only the factoryClass is specified
- the factory is created by invoking the factoryClass' default (zero-argument) constructor and
- the method is invoked on the resulting factory.
If both the factoryClass and the factoryClassMethod are specified
- the factory is created by invoking the factoryClassMethod on the factoryClass (as a static) and
- the method is invoked on the resulting factory.

The only thing we can't support in the current configuration is invoking a static on some, client-specified, factoryClass to build new instances of the descriptor class; and it's debatable whether that is desirable...

It might be reasonable to rework this into a number of different classes that implement an interface...

Fields Summary
protected String
methodName
The method invoked on either the descriptor class (in which case it is static) or the factory (in which case it is not static) to build a new instance of the descriptor class.
protected transient Method
method
The method is resolved during initialization, and it is not serialized.
protected Class
factoryClass
The class of the factory. The factory is instantiated by either invoking this class's default (zero-argument) constructor or the factoryMethod specified below.
protected String
factoryClassName
protected String
factoryMethodName
Static method invoked on the factoryClass to get the factory instance. If this is null, the factory class's default (zero-argument) constructor is invoked.
protected Object
factory
The object factory. This can be specified directly by the client, or it can be built dynamically using the the factoryClass and, optionally, the factoryMethodName.
protected ClassDescriptor
descriptor
Backpointer to descriptor.
private transient Constructor
defaultConstructor
Must be transient because java.lang.Constructor is not serializable.
Constructors Summary
public InstantiationPolicy()
Default constructor

        super();
    
Methods Summary
protected java.lang.reflect.ConstructorbuildDefaultConstructor()
Build and return the default (zero-argument) constructor for the descriptor class.

        return this.buildDefaultConstructorFor(this.getDescriptor().getJavaClass());
    
protected java.lang.reflect.ConstructorbuildDefaultConstructorFor(java.lang.Class javaClass)
Build and return the default (zero-argument) constructor for the specified class.

        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    return (Constructor)AccessController.doPrivileged(new PrivilegedGetDeclaredConstructorFor(javaClass, new Class[0], true));
                } catch (PrivilegedActionException exception) {
                    throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), exception.getException());
                }
            } else {
                return PrivilegedAccessHelper.getDeclaredConstructorFor(javaClass, new Class[0], true);
            }
        } catch (NoSuchMethodException exception) {
            throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), exception);
        }
    
protected java.lang.ObjectbuildFactory()

        // If there is no factory class specified, there is no factory;
        // we will be using a static method defined by the descriptor class...
        if (this.getFactoryClass() == null) {
            return null;
        }

        // If there is a factory class specified but no factory method name,
        // instantiate the factory using the default constructor
        if (this.getFactoryMethodName() == null) {
            return this.buildFactoryUsingDefaultConstructor();
        }

        // If both the factory class and the factory method name have been specified,
        // instantiate the factory by invoking the static factory method
        return this.buildFactoryUsingStaticMethod();
    
protected java.lang.reflect.ConstructorbuildFactoryDefaultConstructor()
Build and return the default (zero-argument) constructor for the factory class.

        return this.buildDefaultConstructorFor(this.getFactoryClass());
    
protected java.lang.ObjectbuildFactoryUsingDefaultConstructor()
Build and return the factory, using its default constructor.

        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    return AccessController.doPrivileged(new PrivilegedInvokeConstructor(this.buildFactoryDefaultConstructor(), (Object[])null));
                } catch (PrivilegedActionException exception) {
                    Exception throwableException = exception.getException();
                    if (throwableException instanceof InvocationTargetException){
                        throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException);
                    } else if (throwableException instanceof IllegalAccessException){
                        throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException);                    
                    } else {
                        throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException);                       
                    }
                 }
            } else {
                return PrivilegedAccessHelper.invokeConstructor(this.buildFactoryDefaultConstructor(), (Object[])null);
            }
        } catch (InvocationTargetException exception) {
            throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception);
        } catch (IllegalAccessException exception) {
            throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception);
        } catch (InstantiationException exception) {
            throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception);
        } catch (NoSuchMethodError exception) {
            // This exception is not documented but gets thrown.
            throw DescriptorException.noSuchMethodWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception);
        } catch (NullPointerException exception) {
            // Some JVMs will throw a NULL pointer exception here
            throw DescriptorException.nullPointerWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception);
        }
    
protected java.lang.ObjectbuildFactoryUsingStaticMethod()
Build and return the factory, using the specified static method.

        Method factoryMethod = this.buildMethod(this.getFactoryClass(), this.getFactoryMethodName(), new Class[0]);

        try {
            // it should be static and zero-argument...
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    return AccessController.doPrivileged(new PrivilegedMethodInvoker(factoryMethod, null, null));
                } catch (PrivilegedActionException exception) {
                    Exception throwableException = exception.getException();
                    if (throwableException instanceof IllegalAccessException) {
                        throw DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), throwableException);
                    } else {
                        throw DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), throwableException);
                    }

                }
            } else {
                return PrivilegedAccessHelper.invokeMethod(factoryMethod, null, null);
            }
        } catch (IllegalAccessException exception) {
            throw DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception);
        } catch (InvocationTargetException exception) {
            throw DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception);
        } catch (NullPointerException exception) {
            // Some JVMs will throw a NULL pointer exception here
            throw DescriptorException.nullPointerWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception);
        }
    
protected java.lang.reflect.MethodbuildMethod(java.lang.Class methodClass, java.lang.String methodName, java.lang.Class[] methodParameterTypes)
Build the specified method.

        try {
            return Helper.getDeclaredMethod(methodClass, methodName, methodParameterTypes);
        } catch (NoSuchMethodException exception) {
            throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(methodClass.getName() + "." + methodName, this.getDescriptor(), exception);
        } catch (SecurityException exception) {
            throw DescriptorException.securityWhileInitializingInstantiationPolicy(methodClass.getName() + "." + methodName, this.getDescriptor(), exception);
        }
    
public java.lang.ObjectbuildNewInstance()
Build and return a new instance, using the appropriate mechanism.

        if (this.isUsingDefaultConstructor()) {
            return this.buildNewInstanceUsingDefaultConstructor();
        } else {
            return this.buildNewInstanceUsingFactory();
        }
    
protected java.lang.ObjectbuildNewInstanceUsingDefaultConstructor()
Build and return a new instance, using the default (zero-argument) constructor.

        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    return AccessController.doPrivileged(new PrivilegedInvokeConstructor(this.getDefaultConstructor(), (Object[])null));
                } catch (PrivilegedActionException exception) {
                    Exception throwableException = exception.getException();
                    if (throwableException instanceof InvocationTargetException){
                        throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException);
                    } else if (throwableException instanceof IllegalAccessException){
                        throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException);                    
                    } else {
                        throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException);                       
                    }
                 }
            } else {
                return PrivilegedAccessHelper.invokeConstructor(this.getDefaultConstructor(), (Object[])null);
            }
        } catch (InvocationTargetException exception) {
            throw DescriptorException.targetInvocationWhileConstructorInstantiation(this.getDescriptor(), exception);
        } catch (IllegalAccessException exception) {
            throw DescriptorException.illegalAccessWhileConstructorInstantiation(this.getDescriptor(), exception);
        } catch (InstantiationException exception) {
            throw DescriptorException.instantiationWhileConstructorInstantiation(this.getDescriptor(), exception);
        } catch (NoSuchMethodError exception) {
            // This exception is not documented but gets thrown.
            throw DescriptorException.noSuchMethodWhileConstructorInstantiation(this.getDescriptor(), exception);
        } catch (NullPointerException exception) {
            // Some JVMs will throw a NULL pointer exception here
            throw DescriptorException.nullPointerWhileConstructorInstantiation(this.getDescriptor(), exception);
        }
    
protected java.lang.ObjectbuildNewInstanceUsingFactory()
Build and return a new instance, using the factory. The factory can be null, in which case the method is a static method defined by the descriptor class.

        try {
            // If the method is static, the first argument is ignored and can be null
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getMethod(), this.getFactory(), new Object[0]));
                } catch (PrivilegedActionException exception) {
                    Exception throwableException = exception.getException();
                    if (throwableException instanceof IllegalAccessException) {
                        throw DescriptorException.illegalAccessWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), throwableException);
                    } else {
                        throw DescriptorException.targetInvocationWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), throwableException);
                    }
                }
            } else {
                return PrivilegedAccessHelper.invokeMethod(this.getMethod(), this.getFactory(), new Object[0]);
            }
        } catch (IllegalAccessException exception) {
            throw DescriptorException.illegalAccessWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception);
        } catch (InvocationTargetException exception) {
            throw DescriptorException.targetInvocationWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception);
        } catch (NullPointerException exception) {
            // Some JVMs will throw a NULL pointer exception here
            throw DescriptorException.nullPointerWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception);
        }
    
public java.lang.Objectclone()
INTERNAL: Clones the InstantiationPolicy

        try {
            // clones itself
            return super.clone();
        } catch (Exception exception) {
            ;
        }
        return null;
    
public voidconvertClassNamesToClasses(java.lang.ClassLoader classLoader)
INTERNAL: Convert all the class-name-based settings in this InstantiationPolicy to actual class-based settings. This method is used when converting a project that has been built with class names to a project with classes.

param
classLoader

        if (factoryClassName == null){
            return;
        }
        Class factoryClass = null;
        try{
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    factoryClass = (Class)AccessController.doPrivileged(new PrivilegedClassForName(factoryClassName, true, classLoader));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exception.getException());
                }
            } else {
                factoryClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(factoryClassName, true, classLoader);
            }
        } catch (ClassNotFoundException exc){
            throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exc);
        }
        setFactoryClass(factoryClass);
    
protected java.lang.reflect.ConstructorgetDefaultConstructor()
Return the default (zero-argument) constructor for the descriptor class.

        // Lazy initialize, because the constructor cannot be serialized
        if (defaultConstructor == null) {
            this.setDefaultConstructor(this.buildDefaultConstructor());
        }
        return defaultConstructor;
    
protected oracle.toplink.essentials.descriptors.ClassDescriptorgetDescriptor()

        return descriptor;
    
public java.lang.ObjectgetFactory()

        return factory;
    
public java.lang.ClassgetFactoryClass()

        return factoryClass;
    
public java.lang.StringgetFactoryClassName()

        if ((factoryClassName == null) && (factoryClass != null)) {
            factoryClassName = factoryClass.getName();
        }
        return factoryClassName;
    
public java.lang.StringgetFactoryMethodName()

        return factoryMethodName;
    
protected java.lang.reflect.MethodgetMethod()

        return method;
    
public java.lang.StringgetMethodName()

        return methodName;
    
public voidinitialize(oracle.toplink.essentials.internal.sessions.AbstractSession session)
If necessary, initialize the factory and the method.

        if (this.isUsingDefaultConstructor()) {
            return;
        }
        try {
            // If the factory has been specified directly, do not overwrite it
            if (this.getFactory() == null) {
                this.setFactory(this.buildFactory());
            }
            this.initializeMethod();
        } catch (DescriptorException ex) {
            session.getIntegrityChecker().handleError(ex);
        }
    
protected voidinitializeMethod()
Initialize the method. It is either a static on the descriptor class, or it is a non-static on the factory.

        Class tempClass;
        if (this.getFactory() == null) {
            tempClass = this.getDescriptor().getJavaClass();
        } else {
            tempClass = this.getFactory().getClass();
        }
        this.setMethod(this.buildMethod(tempClass, this.getMethodName(), new Class[0]));
    
public booleanisUsingDefaultConstructor()
If no method name is specified, they we have to use the default (zero-argument) constructor.

        return this.getMethodName() == null;
    
protected voidsetDefaultConstructor(java.lang.reflect.Constructor defaultConstructor)

        this.defaultConstructor = defaultConstructor;
    
public voidsetDescriptor(oracle.toplink.essentials.descriptors.ClassDescriptor descriptor)

        this.descriptor = descriptor;
    
protected voidsetFactory(java.lang.Object factory)

        this.factory = factory;
    
protected voidsetFactoryClass(java.lang.Class factoryClass)

        this.factoryClass = factoryClass;
    
protected voidsetFactoryClassName(java.lang.String factoryClassName)

        this.factoryClassName = factoryClassName;
    
protected voidsetFactoryMethodName(java.lang.String factoryMethodName)

        this.factoryMethodName = factoryMethodName;
    
protected voidsetMethod(java.lang.reflect.Method method)

        this.method = method;
    
public voidsetMethodName(java.lang.String methodName)

        this.methodName = methodName;
    
public java.lang.StringtoString()

        String mName = null;
        if (this.isUsingDefaultConstructor()) {
            mName = "<CONSTRUCTOR>";
        } else {
            mName = this.getMethodName();
        }
        return Helper.getShortClassName(this) + "(" + mName + ")";
    
public voiduseDefaultConstructorInstantiationPolicy()

        setMethodName(null);
        setFactory(null);
        setFactoryClass(null);
        setFactoryClassName(null);
        setFactoryMethodName(null);
    
public voiduseFactoryInstantiationPolicy(java.lang.Class factoryClass, java.lang.String methodName)

        setMethodName(methodName);
        setFactory(null);
        setFactoryClass(factoryClass);
        setFactoryClassName(factoryClass.getName());
        setFactoryMethodName(null);
    
public voiduseFactoryInstantiationPolicy(java.lang.Class factoryClass, java.lang.String methodName, java.lang.String factoryMethodName)

        setMethodName(methodName);
        setFactory(null);
        setFactoryClass(factoryClass);
        setFactoryClassName(factoryClass.getName());
        setFactoryMethodName(factoryMethodName);
    
public voiduseFactoryInstantiationPolicy(java.lang.String factoryClassName, java.lang.String methodName)

        setMethodName(methodName);
        setFactory(null);
        setFactoryClass(null);
        setFactoryClassName(factoryClassName);
        setFactoryMethodName(null);
    
public voiduseFactoryInstantiationPolicy(java.lang.String factoryClassName, java.lang.String methodName, java.lang.String factoryMethodName)

        setMethodName(methodName);
        setFactory(null);
        setFactoryClass(null);
        setFactoryClassName(factoryClassName);
        setFactoryMethodName(factoryMethodName);
    
public voiduseFactoryInstantiationPolicy(java.lang.Object factory, java.lang.String methodName)

        setMethodName(methodName);
        setFactory(factory);
        setFactoryClass(null);
        setFactoryClassName(null);
        setFactoryMethodName(null);
    
public voiduseMethodInstantiationPolicy(java.lang.String staticMethodName)

        setMethodName(staticMethodName);
        setFactory(null);
        setFactoryClass(null);
        setFactoryClassName(null);
        setFactoryMethodName(null);