use of org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor in project eclipselink by eclipse-ee4j.
the class ManagedTypeImpl method getTypeClassFromAttributeOrMethodLevelAccessor.
/**
* INTERNAL:
* Get the elementType directly from the class using a reflective method call
* directly on the containing java class associated with this managedType.
*/
protected Class<?> getTypeClassFromAttributeOrMethodLevelAccessor(DatabaseMapping mapping) {
/**
* In this block we have the following scenario:
* 1) The access type is "method" or "field"
* 1a) The get method is set on the entity (method access)
* 1b) The get method is not set on the entity (field access)
* 1c) The get method is named differently than the attribute
*/
// Type may be null when no getMethod exists for the class for a ManyToMany mapping
// Here we check the returnType on the declared method on the class directly
Class<?> aType = null;
Field aField = null;
String getMethodName = null;
// 1) Check access Type
if (mapping.getAttributeAccessor() instanceof MethodAttributeAccessor) {
// isFieldLevelAccess = false;
getMethodName = ((MethodAttributeAccessor) mapping.getAttributeAccessor()).getGetMethodName();
} else if (mapping.getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) {
// isFieldLevelAccess = true;
aField = ((InstanceVariableAttributeAccessor) mapping.getAttributeAccessor()).getAttributeField();
}
// 3) If field level access - perform a getDeclaredField call
if (null == aField && this.getJavaType() != null) {
// Check declaredFields in the case where we have no getMethod or getMethodName
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aField = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getJavaType(), mapping.getAttributeName(), false));
} else {
aField = PrivilegedAccessHelper.getDeclaredField(this.getJavaType(), mapping.getAttributeName(), false);
}
} catch (PrivilegedActionException pae) {
} catch (NoSuchFieldException nsfe) {
}
}
/**
* Field access Handling:
* If a get method name exists, we check the return type on the method directly
* using reflection.
* In all failure cases we default to the List type.
*/
if (null == aField && this.getJavaType() != null && getMethodName != null) {
Method aMethod = null;
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getJavaType(), getMethodName, null));
} else {
aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getJavaType(), getMethodName, null);
}
} catch (PrivilegedActionException pae) {
} catch (NoSuchMethodException nsfe) {
} catch (NullPointerException npe) {
// case: null name arg to Class.searchMethods from getDeclaredMethod if getMethodName is null
// because we do not know the javaType on the Type (descriptor.javaClass was null)
// See bug# 303063
npe.printStackTrace();
}
if (null != aMethod) {
aType = aMethod.getReturnType();
}
}
// MappedSuperclasses need special handling to get their type from an inheriting subclass
if (null == aField && null == aType && this.isMappedSuperclass()) {
// get inheriting subtype member (without handling @override annotations)
MappedSuperclassTypeImpl aMappedSuperclass = ((MappedSuperclassTypeImpl) this);
AttributeImpl inheritingTypeMember = aMappedSuperclass.getMemberFromInheritingType(mapping.getAttributeName());
aField = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
}
// 6) get the type from the resulting field (method level access was handled)
if (null != aField) {
// field access
aType = aField.getType();
}
// 7) catch unsupported element type
if (null == aType) {
aType = MetamodelImpl.DEFAULT_ELEMENT_TYPE_FOR_UNSUPPORTED_MAPPINGS;
}
// 303063: secondary check for case where descriptor has no java class set - should never happen but this will show on code coverage
if (null == this.getJavaType()) {
AbstractSessionLog.getLog().log(SessionLog.FINEST, SessionLog.METAMODEL, "metamodel_relationaldescriptor_javaclass_null_on_managedType", descriptor, this);
}
return aType;
}
Aggregations