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

MethodAttributeAccessor

public class MethodAttributeAccessor extends AttributeAccessor

Purpose: A wrapper class for handling cases when the domain object attributes are to be accessed thru the accessor methods. This could happen if the variables are not defined public in the domain object.

author
Sati
since
TOPLink/Java 1.0

Fields Summary
protected String
setMethodName
protected String
getMethodName
protected transient Method
setMethod
protected transient Method
getMethod
Constructors Summary
Methods Summary
public java.lang.ClassgetAttributeClass()
Return the return type of the method accessor.

        if (getGetMethod() == null) {
            return null;
        }

        return getGetMethodReturnType();
    
public java.lang.ObjectgetAttributeValueFromObject(java.lang.Object anObject)
Gets the value of an instance variable in the object.

        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    return AccessController.doPrivileged(new PrivilegedMethodInvoker(getGetMethod(), anObject, (Object[])null));
                } catch (PrivilegedActionException exception) {
                    Exception throwableException = exception.getException();
                    if (throwableException instanceof IllegalAccessException) {
                        throw DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), throwableException);
                    } else {
                        throw DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), throwableException);
                     }
                }
            } else {
                return PrivilegedAccessHelper.invokeMethod(getGetMethod(), anObject, (Object[])null);
            }
        } catch (IllegalArgumentException exception) {
            throw DescriptorException.illegalArgumentWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
        } catch (IllegalAccessException exception) {
            throw DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
        } catch (InvocationTargetException exception) {
            throw DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
        } catch (NullPointerException exception) {
            // Some JVM's throw this exception for some very odd reason
            throw DescriptorException.nullPointerWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
        }
    
protected java.lang.reflect.MethodgetGetMethod()
Return the accessor method for the attribute accessor.

        return getMethod;
    
public java.lang.StringgetGetMethodName()
Return the name of theh accessor method for the attribute accessor.

        return getMethodName;
    
public java.lang.ClassgetGetMethodReturnType()

        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
            try {
                return (Class)AccessController.doPrivileged(new PrivilegedGetMethodReturnType(getGetMethod()));
            } catch (PrivilegedActionException exception) {
                // we should not get here since this call does not throw any checked exceptions
                return null;
            }
        } else {
            return PrivilegedAccessHelper.getMethodReturnType(getGetMethod());
        }
    
protected java.lang.reflect.MethodgetSetMethod()
Return the set method for the attribute accessor.

        return setMethod;
    
public java.lang.StringgetSetMethodName()
Return the name of the set method for the attribute accessor.

        return setMethodName;
    
public java.lang.ClassgetSetMethodParameterType()

        if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
            try {
                return ((Class[])AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(getSetMethod())))[0];
            } catch (PrivilegedActionException exception) {
                // we should not get here since this call does not throw any checked exceptions
                return null;
            }
        } else {
            return PrivilegedAccessHelper.getMethodParameterTypes(getSetMethod())[0];
        }
    
public voidinitializeAttributes(java.lang.Class theJavaClass)
Set get and set method after creating these methods by using get and set method names

        if (getAttributeName() == null) {
            throw DescriptorException.attributeNameNotSpecified();
        }
        try {
            setGetMethod(Helper.getDeclaredMethod(theJavaClass, getGetMethodName(), (Class[])null));

            // The parameter type for the set method must always be the return type of the get method.
            Class[] parameterTypes = new Class[1];
            parameterTypes[0] = getGetMethodReturnType();
            setSetMethod(Helper.getDeclaredMethod(theJavaClass, getSetMethodName(), parameterTypes));
        } catch (NoSuchMethodException ex) {
            DescriptorException descriptorException = DescriptorException.noSuchMethodWhileInitializingAttributesInMethodAccessor(getSetMethodName(), getGetMethodName(), theJavaClass.getName());
            descriptorException.setInternalException(ex);
            throw descriptorException;
        } catch (SecurityException exception) {
            DescriptorException descriptorException = DescriptorException.securityWhileInitializingAttributesInMethodAccessor(getSetMethodName(), getGetMethodName(), theJavaClass.getName());
            descriptorException.setInternalException(exception);
            throw descriptorException;
        }
    
public booleanisMethodAttributeAccessor()

        return true;
    
public voidsetAttributeValueInObject(java.lang.Object domainObject, java.lang.Object attributeValue)
Sets the value of the instance variable in the object to the value.

        Object[] parameters = new Object[1];
        parameters[0] = attributeValue;
        try {
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters));
                } catch (PrivilegedActionException exception) {
                    Exception throwableException = exception.getException();
                    if (throwableException instanceof IllegalAccessException) {
                        throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, throwableException);
                    } else {
                        throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, throwableException);
                     }
                }
            } else {
                PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters);
            }
        } catch (IllegalAccessException exception) {
            throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
        } catch (IllegalArgumentException exception) {
            // TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
            // Allow XML change set to merge correctly since new value in XML change set is always String
            try {
                if (attributeValue instanceof String) {
                    Object newValue = ConversionManager.getDefaultManager().convertObject(attributeValue, getAttributeClass());
                    Object[] newParameters = new Object[1];
                    newParameters[0] = newValue;
                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                        try {
                            AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, newParameters));
                        } catch (PrivilegedActionException exc) {
                            // Do nothing and move on to throw the original exception
                        }
                    } else {
                        PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, newParameters);
                    }
                    return;
                }
            } catch (Exception e) {
                // Do nothing and move on to throw the original exception
            }
            throw DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
        } catch (InvocationTargetException exception) {
            throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
        } catch (NullPointerException exception) {
            try {
                // TODO: This code should be removed, it should not be required and may cause unwanted sideeffects.
                // cr 3737  If a null pointer was thrown because toplink attempted to set a null referece into a
                // primitive create a primitive of value 0 to set in the object.
                // Is this really the best place for this? is this not why we have null-value and conversion-manager?
                Class fieldClass = getSetMethodParameterType();

                //Found when fixing Bug2910086
                if (fieldClass.isPrimitive() && (attributeValue == null)) {
                    parameters[0] = ConversionManager.getDefaultManager().convertObject(new Integer(0), fieldClass);
                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                        try {
                            AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters));
                        } catch (PrivilegedActionException exc) {
                            Exception throwableException = exc.getException();
                            if (throwableException instanceof IllegalAccessException) {
                                throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, throwableException);
                            } else {
                                throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, throwableException);
                             }
                        }
                    } else {
                        PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters);
                    }
                } else {
                    // Some JVM's throw this exception for some very odd reason
                    throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
                }
            } catch (IllegalAccessException accessException) {
                throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
            } catch (InvocationTargetException invocationException) {
                throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
            }
        }
    
protected voidsetGetMethod(java.lang.reflect.Method getMethod)
Set the accessor method for the attribute accessor.

        this.getMethod = getMethod;
    
public voidsetGetMethodName(java.lang.String getMethodName)
Set the name of the accessor method for the attribute accessor.

        this.getMethodName = getMethodName;
    
protected voidsetSetMethod(java.lang.reflect.Method setMethod)
Set the set method for the attribute accessor.

        this.setMethod = setMethod;
    
public voidsetSetMethodName(java.lang.String setMethodName)
Set the name of the set method for the attribute accessor.

        this.setMethodName = setMethodName;