Search in sources :

Example 11 with InstanceVariableAttributeAccessor

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;
}
Also used : InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) Method(java.lang.reflect.Method) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) Field(java.lang.reflect.Field) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)

Aggregations

InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)11 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)8 Method (java.lang.reflect.Method)4 Field (java.lang.reflect.Field)3 PrivilegedActionException (java.security.PrivilegedActionException)3 PrivilegedGetDeclaredField (org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField)3 PrivilegedGetDeclaredMethod (org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod)3 EntityManager (jakarta.persistence.EntityManager)2 Query (jakarta.persistence.Query)2 TypedQuery (jakarta.persistence.TypedQuery)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)2 OneToOneMapping (org.eclipse.persistence.mappings.OneToOneMapping)2 Test (org.junit.Test)2 Attribute (jakarta.persistence.metamodel.Attribute)1 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)1 ListAttribute (jakarta.persistence.metamodel.ListAttribute)1 MapAttribute (jakarta.persistence.metamodel.MapAttribute)1