/** * Set get and set method after creating these methods by using * get and set method names */ public void initializeAttributes(Class theJavaClass) throws DescriptorException { 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; } }
/** * Return the return type of the method accessor. */ public Class getAttributeClass() { if (getGetMethod() == null) { return null; } return getGetMethodReturnType(); }
public Class getSetMethodParameterType() { 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]; } }
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); PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters); throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception); } catch (IllegalArgumentException exception) { 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) { PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, newParameters); throw DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception); } catch (InvocationTargetException exception) { throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception); } catch (NullPointerException exception) {
/** * INTERNAL: * To validate mappings decleration */ public void validateBeforeInitialization(AbstractSession session) throws DescriptorException { super.validateBeforeInitialization(session); if (getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) { Class attributeType = ((InstanceVariableAttributeAccessor)getAttributeAccessor()).getAttributeType(); getIndirectionPolicy().validateDeclaredAttributeType(attributeType, session.getIntegrityChecker()); } else if (getAttributeAccessor() instanceof MethodAttributeAccessor) { Class returnType = ((MethodAttributeAccessor)getAttributeAccessor()).getGetMethodReturnType(); getIndirectionPolicy().validateGetMethodReturnType(returnType, session.getIntegrityChecker()); Class parameterType = ((MethodAttributeAccessor)getAttributeAccessor()).getSetMethodParameterType(); getIndirectionPolicy().validateSetMethodParameterType(parameterType, session.getIntegrityChecker()); } }
/** * Gets the value of an instance variable in the object. */ public Object getAttributeValueFromObject(Object anObject) throws DescriptorException { 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); } }
/** * PUBLIC: * This method is invoked reflectively on the reference object to return the value of the * attribute in the object. This method sets the name of the getMethodName. */ public void setGetMethodName(String methodName) { if (methodName == null) { return; } // This is done because setting attribute name by defaults create InstanceVariableAttributeAccessor if (getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) { String attributeName = this.attributeAccessor.getAttributeName(); setAttributeAccessor(new MethodAttributeAccessor()); getAttributeAccessor().setAttributeName(attributeName); } ((MethodAttributeAccessor)getAttributeAccessor()).setGetMethodName(methodName); }
/** * PUBLIC: * This method is invoked reflectively on the reference object to get the value of the attribute. * The method defined on the object should actually return the value that needs to be set in the * attribute accessor. */ public void setSetMethodName(String methodName) { if (methodName == null) { return; } // This is done because setting attribute name by defaults create InstanceVariableAttributeAccessor if (!(getAttributeAccessor() instanceof MethodAttributeAccessor)) { String attributeName = this.attributeAccessor.getAttributeName(); setAttributeAccessor(new MethodAttributeAccessor()); getAttributeAccessor().setAttributeName(attributeName); } ((MethodAttributeAccessor)getAttributeAccessor()).setSetMethodName(methodName); }
public Class getGetMethodReturnType() { 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()); } }
/** * PUBLIC: * This method is invoked reflectively on the reference object to return the value of the * attribute in the object. This method returns the name of the getMethodName or null if not using method access. */ public String getGetMethodName() { if (!(getAttributeAccessor() instanceof MethodAttributeAccessor)) { return null; } return ((MethodAttributeAccessor)getAttributeAccessor()).getGetMethodName(); }
/** * PUBLIC: * This method is invoked reflectively on the reference object to set the value of the * attribute in the object. This method returns the name of the setMethodName or null if not using method access. */ public String getSetMethodName() { if (!(getAttributeAccessor() instanceof MethodAttributeAccessor)) { return null; } return ((MethodAttributeAccessor)getAttributeAccessor()).getSetMethodName(); }
/** * INTERNAL: * To validate mappings declaration */ public void validateBeforeInitialization(AbstractSession session) throws DescriptorException { super.validateBeforeInitialization(session); getIndirectionPolicy().validateContainerPolicy(session.getIntegrityChecker()); if (getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) { Class attributeType = ((InstanceVariableAttributeAccessor)getAttributeAccessor()).getAttributeType(); getIndirectionPolicy().validateDeclaredAttributeTypeForCollection(attributeType, session.getIntegrityChecker()); } else if (getAttributeAccessor() instanceof MethodAttributeAccessor) { Class returnType = ((MethodAttributeAccessor)getAttributeAccessor()).getGetMethodReturnType(); getIndirectionPolicy().validateGetMethodReturnTypeForCollection(returnType, session.getIntegrityChecker()); Class parameterType = ((MethodAttributeAccessor)getAttributeAccessor()).getSetMethodParameterType(); getIndirectionPolicy().validateSetMethodParameterTypeForCollection(parameterType, session.getIntegrityChecker()); } }