Search in sources :

Example 1 with PrivilegedGetDeclaredField

use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.

the class AttributeImpl method getJavaMember.

/**
 * Return the java.lang.reflect.Member for the represented attribute.
 * In the case of property access the get method will be returned
 *
 * @return corresponding java.lang.reflect.Member
 */
@Override
public Member getJavaMember() {
    AttributeAccessor accessor = getMapping().getAttributeAccessor();
    if (accessor.isMethodAttributeAccessor()) {
        // Method level access here
        Method aMethod = ((MethodAttributeAccessor) accessor).getGetMethod();
        if (null == aMethod) {
            // 316991: If the getMethod is not set - use a reflective call via the getMethodName
            String getMethodName = null;
            try {
                getMethodName = ((MethodAttributeAccessor) mapping.getAttributeAccessor()).getGetMethodName();
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                    aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getManagedTypeImpl().getJavaType(), getMethodName, null));
                } else {
                    aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getManagedTypeImpl().getJavaType(), getMethodName, null);
                }
            // Exceptions are to be ignored for reflective calls - if the methodName is also null - it will catch here
            } catch (PrivilegedActionException pae) {
            // pae.printStackTrace();
            } catch (NoSuchMethodException nsfe) {
            // nsfe.printStackTrace();
            }
        }
        return aMethod;
    }
    // Field level access here
    Member aMember = ((InstanceVariableAttributeAccessor) accessor).getAttributeField();
    // Note: This code does not handle attribute overrides on any entity subclass tree - use descriptor initialization instead
    if (null == aMember) {
        if (this.getManagedTypeImpl().isMappedSuperclass()) {
            // get inheriting subtype member (without handling @override annotations)
            AttributeImpl inheritingTypeMember = ((MappedSuperclassTypeImpl) this.getManagedTypeImpl()).getMemberFromInheritingType(mapping.getAttributeName());
            // 322166: If attribute is defined on this current ManagedType (and not on a superclass) - do not attempt a reflective call on a superclass
            if (null != inheritingTypeMember) {
                // Verify we have an attributeAccessor
                aMember = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
            }
        }
        if (null == aMember) {
            // Check declaredFields in the case where we have no getMethod or getMethodName
            try {
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                    aMember = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getManagedTypeImpl().getJavaType(), mapping.getAttributeName(), false));
                } else {
                    aMember = PrivilegedAccessHelper.getDeclaredField(this.getManagedTypeImpl().getJavaType(), mapping.getAttributeName(), false);
                }
            // Exceptions are to be ignored for reflective calls - if the methodName is also null - it will catch here
            } catch (PrivilegedActionException pae) {
            // pae.printStackTrace();
            } catch (NoSuchFieldException nsfe) {
            // nsfe.printStackTrace();
            }
        }
    }
    // 303063: secondary check for attribute override case - this will show on code coverage
    if (null == aMember) {
        AbstractSessionLog.getLog().log(SessionLog.FINEST, AbstractSessionLog.METAMODEL, "metamodel_attribute_getmember_is_null", this, this.getManagedTypeImpl(), this.getDescriptor());
    }
    return aMember;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) Method(java.lang.reflect.Method) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) Member(java.lang.reflect.Member)

Example 2 with PrivilegedGetDeclaredField

use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.

the class DoPrivilegedTest method testDoPrivileged.

public void testDoPrivileged() {
    String className = "org.eclipse.persistence.Version";
    String fieldName = "product";
    String fieldValue = "TopLink Blah";
    String methodName = "setProduct";
    Class<?> clazz = null;
    Version version = null;
    Method method = null;
    try {
        clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(className));
        Class<?>[] methodParameterTypes = { AccessController.doPrivileged(new PrivilegedClassForName<>("java.lang.String")) };
        ClassLoader clazzloader = AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(clazz));
        ClassLoader classloader = AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread()));
        AccessController.doPrivileged(new PrivilegedClassForName<>(className, true, clazzloader));
        version = (Version) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(clazz));
        Field[] fields = AccessController.doPrivileged(new PrivilegedGetFields(clazz));
        Field field = AccessController.doPrivileged(new PrivilegedGetDeclaredField(clazz, fieldName, true));
        try {
            int intValueFromField = (Integer) AccessController.doPrivileged(new PrivilegedGetValueFromField(field, version));
        } catch (Exception e) {
        }
        AccessController.doPrivileged(new PrivilegedGetValueFromField(field, version));
        try {
            AccessController.doPrivileged(new PrivilegedSetValueInField(field, version, fieldValue));
        } catch (Exception e) {
        }
        String lineSeparator = PrivilegedAccessHelper.getLineSeparator();
        method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, methodName, methodParameterTypes, true));
        AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(method));
        AccessController.doPrivileged(new PrivilegedGetMethodReturnType(method));
        Object[] parameters = { "TopLink Blah." };
        AccessController.doPrivileged(new PrivilegedMethodInvoker(method, version, parameters));
        Constructor<?> constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(clazz, null, true));
        Constructor declaredConstructor = AccessController.doPrivileged(new PrivilegedGetDeclaredConstructorFor(clazz, null, true));
        AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, null));
    // PrivilegedAccessController.loadDeploymentXML(new XMLSessionConfigLoader(),new SessionManager(), clazzloader, false, false);
    } catch (Exception e) {
        throw (new TestProblemException("An exception has been caught."));
    }
}
Also used : PrivilegedGetDeclaredConstructorFor(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredConstructorFor) PrivilegedGetValueFromField(org.eclipse.persistence.internal.security.PrivilegedGetValueFromField) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) PrivilegedSetValueInField(org.eclipse.persistence.internal.security.PrivilegedSetValueInField) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) Version(org.eclipse.persistence.Version) PrivilegedGetValueFromField(org.eclipse.persistence.internal.security.PrivilegedGetValueFromField) PrivilegedGetContextClassLoader(org.eclipse.persistence.internal.security.PrivilegedGetContextClassLoader) PrivilegedGetMethodParameterTypes(org.eclipse.persistence.internal.security.PrivilegedGetMethodParameterTypes) PrivilegedInvokeConstructor(org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor) PrivilegedGetMethod(org.eclipse.persistence.internal.security.PrivilegedGetMethod) PrivilegedGetContextClassLoader(org.eclipse.persistence.internal.security.PrivilegedGetContextClassLoader) PrivilegedGetFields(org.eclipse.persistence.internal.security.PrivilegedGetFields) PrivilegedGetMethod(org.eclipse.persistence.internal.security.PrivilegedGetMethod) PrivilegedSetValueInField(org.eclipse.persistence.internal.security.PrivilegedSetValueInField) PrivilegedGetMethodReturnType(org.eclipse.persistence.internal.security.PrivilegedGetMethodReturnType) PrivilegedMethodInvoker(org.eclipse.persistence.internal.security.PrivilegedMethodInvoker) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) PrivilegedGetClassLoaderForClass(org.eclipse.persistence.internal.security.PrivilegedGetClassLoaderForClass) PrivilegedInvokeConstructor(org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor) PrivilegedGetClassLoaderForClass(org.eclipse.persistence.internal.security.PrivilegedGetClassLoaderForClass) PrivilegedClassForName(org.eclipse.persistence.internal.security.PrivilegedClassForName)

Example 3 with PrivilegedGetDeclaredField

use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.

the class ManagedTypeImpl method initialize.

/**
 * INTERNAL:
 * Initialize the members of this ManagedType based on the mappings defined on the descriptor.
 * We process the appropriate Map, List, Set, Collection or Object/primitive types.<p>
 * Initialization should occur after all types in the metamodel have been created already.
 */
protected void initialize() {
    // See MappedSuperclassType.getMemberFromInheritingType()
    if (null != this.members) {
        // this is already initialized
        return;
    }
    this.members = new HashMap<String, Attribute<X, ?>>();
    // Get and process all mappings on the relationalDescriptor
    for (DatabaseMapping mapping : getDescriptor().getMappings()) {
        AttributeImpl<X, ?> member = null;
        // Tie into the collection hierarchy at a lower level
        if (mapping instanceof CollectionMapping) {
            // Handle 1:m, n:m collection mappings
            CollectionMapping colMapping = (CollectionMapping) mapping;
            ContainerPolicy collectionContainerPolicy = colMapping.getContainerPolicy();
            if (collectionContainerPolicy.isMapPolicy()) {
                // Handle the 3 Map type mappings (policy.isMappedKeyMapPolicy()) is handled by isMapPolicy())
                member = new MapAttributeImpl(this, colMapping, true);
            // check mapping.attributeAcessor.attributeField.type=Collection
            } else if (collectionContainerPolicy.isListPolicy()) {
                // This seems very over complex...
                /**
                 * Handle lazy Collections and Lists and the fact that both return an IndirectList policy.
                 * We check the type on the attributeField of the attributeAccessor on the mapping
                 */
                Class<?> aType = null;
                // 325699: AttributeAccessor is subclassed by both IntanceVariableAttributeAccessor (JPA) and ValuesAccessor (Dynamic JPA)
                if (colMapping.getAttributeAccessor() instanceof ValuesAccessor) {
                    member = new ListAttributeImpl(this, colMapping);
                } else if (colMapping.getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) {
                    Field aField = ((InstanceVariableAttributeAccessor) colMapping.getAttributeAccessor()).getAttributeField();
                    // MappedSuperclasses need special handling to get their type from an inheriting subclass
                    if (null == aField) {
                        // MappedSuperclass field will not be set
                        if (this.isMappedSuperclass()) {
                            // get inheriting subtype member (without handling @override annotations)
                            MappedSuperclassTypeImpl aMappedSuperclass = ((MappedSuperclassTypeImpl) this);
                            AttributeImpl inheritingTypeMember = aMappedSuperclass.getMemberFromInheritingType(colMapping.getAttributeName());
                            // 322166: If attribute is defined on this current ManagedType (and not on a superclass) - do not attempt a reflective call on a superclass
                            if (null != inheritingTypeMember) {
                                // Verify we have an attributeAccessor
                                aField = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
                            }
                        }
                    }
                    // 322166: The attribute may be defined on the current ManagedType - not inherited
                    if (null == aField) {
                        // Check attributeName when the field is null
                        aType = this.getTypeClassFromAttributeOrMethodLevelAccessor(mapping);
                    } else {
                        aType = aField.getType();
                    }
                    // This attribute is declared as List
                    if ((aType != null) && List.class.isAssignableFrom(aType)) {
                        member = new ListAttributeImpl(this, colMapping, true);
                    } else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
                        // This attribute is therefore declared as Collection
                        member = new CollectionAttributeImpl(this, colMapping, true);
                    } else {
                        member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                    }
                } else {
                    // handle variations of missing get/set methods - only for Collection vs List
                    if (colMapping.getAttributeAccessor() instanceof MethodAttributeAccessor) {
                        /**
                         * The following call will perform a getMethod call for us.
                         * If no getMethod exists, we will secondarily check the getMethodName below.
                         */
                        aType = colMapping.getAttributeAccessor().getAttributeClass();
                        if ((aType != null) && List.class.isAssignableFrom(aType)) {
                            member = new ListAttributeImpl(this, colMapping, true);
                        } else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
                            member = new CollectionAttributeImpl(this, colMapping, true);
                        } else {
                            /**
                             * In this block we have the following scenario:
                             * 1) The access type is "field"
                             * 2) The get method is not set on the entity
                             * 3) 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
                            String getMethodName = ((MethodAttributeAccessor) colMapping.getAttributeAccessor()).getGetMethodName();
                            if (null == getMethodName) {
                                // Check declaredFields in the case where we have no getMethod or getMethodName
                                try {
                                    Field field = null;
                                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                                        try {
                                            field = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getJavaType(), colMapping.getAttributeName(), false));
                                        } catch (PrivilegedActionException exception) {
                                            member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                        }
                                    } else {
                                        field = PrivilegedAccessHelper.getDeclaredField(this.getJavaType(), colMapping.getAttributeName(), false);
                                    }
                                    if (null == field) {
                                        member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                    } else {
                                        aType = field.getType();
                                        if ((aType != null) && List.class.isAssignableFrom(aType)) {
                                            member = new ListAttributeImpl(this, colMapping, true);
                                        } else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
                                            member = new CollectionAttributeImpl(this, colMapping, true);
                                        } else {
                                            member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                        }
                                    }
                                } catch (Exception e) {
                                    member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                }
                            } else {
                                /**
                                 * 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.
                                 */
                                try {
                                    Method aMethod = null;
                                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                                        aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getJavaType(), getMethodName, null));
                                    } else {
                                        aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getJavaType(), getMethodName, null);
                                    }
                                    if (null == aMethod) {
                                        member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                    } else {
                                        aType = aMethod.getReturnType();
                                        if ((aType != null) && List.class.isAssignableFrom(aType)) {
                                            member = new ListAttributeImpl(this, colMapping, true);
                                        } else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
                                            member = new CollectionAttributeImpl(this, colMapping, true);
                                        } else {
                                            member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                        }
                                    }
                                } catch (Exception e) {
                                    member = initializePluralAttributeTypeNotFound(this, colMapping, true);
                                }
                            }
                        }
                    }
                }
            } else {
                // Handle non-lazy Collection or Set type mappings (IndirectSet.isAssignableFrom(Set.class) == false)
                if ((collectionContainerPolicy.getContainerClass() != null) && Set.class.isAssignableFrom(collectionContainerPolicy.getContainerClass())) {
                    member = new SetAttributeImpl(this, colMapping, true);
                } else {
                    // Check for non-lazy Collection policy possibly instantiated to a Set or List (both of which is ignored)
                    if (collectionContainerPolicy.isCollectionPolicy()) {
                        member = new CollectionAttributeImpl(this, colMapping, true);
                    } else {
                        // Handle Collection type mappings as a default (we should never get here)
                        // TODO: System.out.println("_Warning: defaulting to non-Set specific Collection type on " + colMapping);
                        member = new CollectionAttributeImpl(this, colMapping);
                    }
                }
            }
        } else {
            // Handle 1:1 single object and direct mappings including EnumSet
            member = new SingularAttributeImpl(this, mapping, true);
        }
        // 303063: secondary check for a null value put - should never happen but this will show on code coverage
        if (null == member) {
            AbstractSessionLog.getLog().log(SessionLog.FINEST, "metamodel_attribute_getmember_is_null", mapping.getAttributeName(), this, descriptor);
        }
        this.members.put(mapping.getAttributeName(), member);
    }
}
Also used : CollectionAttribute(jakarta.persistence.metamodel.CollectionAttribute) PluralAttribute(jakarta.persistence.metamodel.PluralAttribute) Attribute(jakarta.persistence.metamodel.Attribute) MapAttribute(jakarta.persistence.metamodel.MapAttribute) SingularAttribute(jakarta.persistence.metamodel.SingularAttribute) SetAttribute(jakarta.persistence.metamodel.SetAttribute) ListAttribute(jakarta.persistence.metamodel.ListAttribute) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) 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) List(java.util.List) PrivilegedActionException(java.security.PrivilegedActionException) ValuesAccessor(org.eclipse.persistence.internal.dynamic.ValuesAccessor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) PrivilegedGetDeclaredMethod(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod) Method(java.lang.reflect.Method) PrivilegedActionException(java.security.PrivilegedActionException) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Collection(java.util.Collection) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) CollectionMapping(org.eclipse.persistence.mappings.CollectionMapping)

Example 4 with PrivilegedGetDeclaredField

use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField 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)

Example 5 with PrivilegedGetDeclaredField

use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.

the class EntityManagerSetupImpl method initializeCanonicalMetamodel.

/**
 * INTERNAL:
 * Initialize the Canonical Metamodel classes generated by EclipseLink
 * @since Java Persistence 2.0
 */
protected void initializeCanonicalMetamodel(Metamodel model) {
    // 338837: verify that the collection is not empty - this would mean entities did not make it into the search path
    if (null == model.getManagedTypes() || model.getManagedTypes().isEmpty()) {
        getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_type_collection_empty");
    }
    for (ManagedType manType : model.getManagedTypes()) {
        boolean classInitialized = false;
        String className = MetadataHelper.getQualifiedCanonicalName(manType.getJavaType().getName(), getSession());
        try {
            Class<?> clazz = this.getSession().getDatasourcePlatform().convertObject(className, ClassConstants.CLASS);
            classInitialized = true;
            this.getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_found", className);
            String fieldName = "";
            for (Object attribute : manType.getDeclaredAttributes()) {
                try {
                    fieldName = ((Attribute) attribute).getName();
                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
                        AccessController.doPrivileged(new PrivilegedGetDeclaredField(clazz, fieldName, false)).set(clazz, attribute);
                    } else {
                        PrivilegedAccessHelper.getDeclaredField(clazz, fieldName, false).set(clazz, attribute);
                    }
                } catch (NoSuchFieldException nsfe) {
                // Ignore fields missing in canonical model (dclarke bug 346106)
                } catch (Exception e) {
                    ValidationException v = ValidationException.invalidFieldForClass(fieldName, clazz);
                    v.setInternalException(e);
                    throw v;
                }
            }
        } catch (ConversionException exception) {
        }
        if (!classInitialized) {
            getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_not_found", className);
        }
    }
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) PrivilegedGetDeclaredField(org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField) ManagedType(jakarta.persistence.metamodel.ManagedType) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) OptimisticLockException(jakarta.persistence.OptimisticLockException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) RemoteException(java.rmi.RemoteException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityManagerSetupException(org.eclipse.persistence.exceptions.EntityManagerSetupException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConversionException(org.eclipse.persistence.exceptions.ConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PersistenceException(jakarta.persistence.PersistenceException) MalformedURLException(java.net.MalformedURLException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)

Aggregations

PrivilegedGetDeclaredField (org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField)5 PrivilegedActionException (java.security.PrivilegedActionException)4 Method (java.lang.reflect.Method)3 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)3 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)3 PrivilegedGetDeclaredMethod (org.eclipse.persistence.internal.security.PrivilegedGetDeclaredMethod)3 Field (java.lang.reflect.Field)2 OptimisticLockException (jakarta.persistence.OptimisticLockException)1 PersistenceException (jakarta.persistence.PersistenceException)1 Attribute (jakarta.persistence.metamodel.Attribute)1 CollectionAttribute (jakarta.persistence.metamodel.CollectionAttribute)1 ListAttribute (jakarta.persistence.metamodel.ListAttribute)1 ManagedType (jakarta.persistence.metamodel.ManagedType)1 MapAttribute (jakarta.persistence.metamodel.MapAttribute)1 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)1 SetAttribute (jakarta.persistence.metamodel.SetAttribute)1 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1