Search in sources :

Example 1 with MethodAttributeAccessor

use of org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor 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 MethodAttributeAccessor

use of org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateDescriptor.

public void generateDescriptor(JavaClass javaClass, CoreProject project) {
    String jClassName = javaClass.getQualifiedName();
    TypeInfo info = typeInfo.get(jClassName);
    if (info.isTransient()) {
        return;
    }
    NamespaceInfo namespaceInfo = this.packageToPackageInfoMappings.get(javaClass.getPackageName()).getNamespaceInfo();
    String packageNamespace = namespaceInfo.getNamespace();
    String elementName;
    String namespace;
    if (javaClass.getSuperclass() != null && javaClass.getSuperclass().getName().equals("jakarta.xml.bind.JAXBElement")) {
        generateDescriptorForJAXBElementSubclass(javaClass, project, getNamespaceResolverForDescriptor(namespaceInfo));
        return;
    }
    Descriptor descriptor = new XMLDescriptor();
    org.eclipse.persistence.jaxb.xmlmodel.XmlRootElement rootElem = info.getXmlRootElement();
    if (rootElem == null) {
        try {
            elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
        } catch (Exception ex) {
            throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
        }
        namespace = packageNamespace;
    } else {
        elementName = rootElem.getName();
        if (elementName.equals(XMLProcessor.DEFAULT)) {
            try {
                elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
            } catch (Exception ex) {
                throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
            }
        }
        namespace = rootElem.getNamespace();
    }
    descriptor.setJavaClassName(jClassName);
    if (info.getFactoryMethodName() != null) {
        descriptor.getInstantiationPolicy().useFactoryInstantiationPolicy(info.getObjectFactoryClassName(), info.getFactoryMethodName());
    }
    if (namespace.equals(XMLProcessor.DEFAULT)) {
        namespace = namespaceInfo.getNamespace();
    }
    NamespaceResolver resolverForDescriptor = getNamespaceResolverForDescriptor(namespaceInfo);
    JavaClass manyValueJavaClass = helper.getJavaClass(ManyValue.class);
    if (!manyValueJavaClass.isAssignableFrom(javaClass)) {
        if (isDefaultNamespaceAllowed && namespace.length() != 0 && globalNamespaceResolver.getDefaultNamespaceURI() == null && !resolverForDescriptor.getPrefixesToNamespaces().containsValue(namespace)) {
            globalNamespaceResolver.setDefaultNamespaceURI(namespace);
            resolverForDescriptor.setDefaultNamespaceURI(namespace);
        }
        if (rootElem == null) {
            descriptor.setDefaultRootElement("");
        } else {
            if (namespace.length() == 0) {
                descriptor.setDefaultRootElement(elementName);
            } else {
                descriptor.setDefaultRootElement(getQualifiedString(getPrefixForNamespace(namespace, resolverForDescriptor), elementName));
            }
        }
    }
    descriptor.setNamespaceResolver(resolverForDescriptor);
    setSchemaContext(descriptor, info);
    // set the ClassExtractor class name if necessary
    if (info.isSetClassExtractorName()) {
        descriptor.getInheritancePolicy().setClassExtractorName(info.getClassExtractorName());
    }
    // set any user-defined properties
    if (info.getUserProperties() != null) {
        descriptor.setProperties(info.getUserProperties());
    }
    if (info.isLocationAware()) {
        Property locProp = null;
        Iterator<Property> i = info.getPropertyList().iterator();
        while (i.hasNext()) {
            Property p = i.next();
            if (p.getType().getName().equals(Constants.LOCATOR_CLASS_NAME)) {
                locProp = p;
            }
        }
        if (locProp != null && locProp.isTransient()) {
            // don't make a mapping
            if (locProp.isMethodProperty()) {
                MethodAttributeAccessor aa = new MethodAttributeAccessor();
                aa.setAttributeName(locProp.getPropertyName());
                aa.setSetMethodName(locProp.getSetMethodName());
                aa.setGetMethodName(locProp.getGetMethodName());
                descriptor.setLocationAccessor(aa);
            } else {
                // instance variable property
                InstanceVariableAttributeAccessor aa = new InstanceVariableAttributeAccessor();
                aa.setAttributeName(locProp.getPropertyName());
                descriptor.setLocationAccessor(aa);
            }
        }
    }
    if (!info.getObjectGraphs().isEmpty()) {
        // these will be populated later to allow for linking
        for (XmlNamedObjectGraph next : info.getObjectGraphs()) {
            AttributeGroup attributeGroup = new AttributeGroup(next.getName(), info.getJavaClassName(), false);
            ((XMLDescriptor) descriptor).addAttributeGroup(attributeGroup);
        // process subclass graphs for inheritance
        // for(NamedSubgraph nextSubclass:next.getNamedSubclassGraph()) {
        // attributeGroup.insertSubClass(new AttributeGroup(next.getName(), nextSubclass.getType()));
        // }
        }
    }
    project.addDescriptor((CoreDescriptor) descriptor);
    info.setDescriptor(descriptor);
}
Also used : InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) XmlNamedObjectGraph(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedObjectGraph) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)

Example 3 with MethodAttributeAccessor

use of org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor in project eclipselink by eclipse-ee4j.

the class TypeResolver method calculateMappingType.

/**
 * Returns the type of the given {@link DatabaseMapping}, which is the persistent field type.
 *
 * @param mapping The {@link DatabaseMapping} to retrieve its persistent field type
 * @return The persistent field type
 */
@SuppressWarnings("null")
Class<?> calculateMappingType(DatabaseMapping mapping) {
    // returned when querying it with a Java type
    if (mapping.isAggregateMapping()) {
        ClassDescriptor descriptor = mapping.getReferenceDescriptor();
        if (descriptor != null) {
            return descriptor.getJavaClass();
        }
    }
    // Relationship mapping
    if (mapping.isForeignReferenceMapping()) {
        ClassDescriptor descriptor = mapping.getReferenceDescriptor();
        if (descriptor != null) {
            return descriptor.getJavaClass();
        }
    } else // Collection mapping
    if (mapping.isCollectionMapping()) {
        return mapping.getContainerPolicy().getContainerClass();
    }
    // Property mapping
    AttributeAccessor accessor = mapping.getAttributeAccessor();
    // Attribute
    if (accessor.isInstanceVariableAttributeAccessor()) {
        InstanceVariableAttributeAccessor attributeAccessor = (InstanceVariableAttributeAccessor) accessor;
        Field field = attributeAccessor.getAttributeField();
        if (field == null) {
            try {
                field = mapping.getDescriptor().getJavaClass().getDeclaredField(attributeAccessor.getAttributeName());
            } catch (Exception e) {
            }
        }
        return field.getType();
    }
    // Property
    if (accessor.isMethodAttributeAccessor()) {
        MethodAttributeAccessor methodAccessor = (MethodAttributeAccessor) accessor;
        Method method = methodAccessor.getGetMethod();
        if (method == null) {
            try {
                method = mapping.getDescriptor().getJavaClass().getDeclaredMethod(methodAccessor.getGetMethodName());
            } catch (Exception e) {
            }
        }
        return method.getReturnType();
    }
    // Anything else
    return accessor.getAttributeClass();
}
Also used : Field(java.lang.reflect.Field) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) Method(java.lang.reflect.Method) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)

Example 4 with MethodAttributeAccessor

use of org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor in project eclipselink by eclipse-ee4j.

the class BidirectionalPolicy method setBidirectionalTargetSetMethodName.

/**
 * Sets the name of the method to be used when setting the value of the back pointer
 * on the target object of this mapping. If the specified method doesn't exist
 * on the reference class of this mapping, a DescriptorException will be thrown
 * during initialize.
 *
 * @param methodName - the setter method to be used.
 */
public void setBidirectionalTargetSetMethodName(String methodName) {
    if (methodName == null) {
        return;
    }
    if (this.bidirectionalTargetAccessor == null) {
        this.bidirectionalTargetAccessor = new MethodAttributeAccessor();
    }
    // This is done because setting attribute name by defaults create InstanceVariableAttributeAccessor
    if (!getBidirectionalTargetAccessor().isMethodAttributeAccessor()) {
        String attributeName = this.bidirectionalTargetAccessor.getAttributeName();
        setBidirectionalTargetAccessor(new MethodAttributeAccessor());
        getBidirectionalTargetAccessor().setAttributeName(attributeName);
    }
    ((MethodAttributeAccessor) getBidirectionalTargetAccessor()).setSetMethodName(methodName);
}
Also used : MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)

Example 5 with MethodAttributeAccessor

use of org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor in project eclipselink by eclipse-ee4j.

the class IllegalArgumentWhileSettingValueThruMethodAccessorTest method test.

@Override
public void test() {
    PersonMethodAccess person = new PersonMethodAccess();
    person.setName("Person");
    Address address = new Address();
    try {
        getSession().setIntegrityChecker(new IntegrityChecker());
        getSession().getIntegrityChecker().dontCatchExceptions();
        ((DatabaseSession) getSession()).addDescriptor(descriptor());
        // ((DatabaseSession) getSession()).login();
        UnitOfWork uow = getSession().acquireUnitOfWork();
        uow.registerObject(person);
        uow.commit();
        DatabaseMapping dMapping = descriptor().getMappingForAttributeName("p_name");
        DatabaseMapping idMapping = descriptor().getMappingForAttributeName("p_id");
        dMapping.getAttributeAccessor().initializeAttributes(PersonMethodAccess.class);
        idMapping.getAttributeAccessor().initializeAttributes(PersonMethodAccess.class);
        ((MethodAttributeAccessor) idMapping.getAttributeAccessor()).setGetMethodName("Vesna");
        dMapping.getAttributeAccessor().setAttributeValueInObject(address, dMapping.getAttributeValueFromObject(person));
    // ((DatabaseSession) getSession()).logout();
    } catch (EclipseLinkException exception) {
        caughtException = exception;
    }
}
Also used : IntegrityChecker(org.eclipse.persistence.exceptions.IntegrityChecker) UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)

Aggregations

MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)12 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)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 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)2 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)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 PluralAttribute (jakarta.persistence.metamodel.PluralAttribute)1 SetAttribute (jakarta.persistence.metamodel.SetAttribute)1 SingularAttribute (jakarta.persistence.metamodel.SingularAttribute)1 Member (java.lang.reflect.Member)1 Collection (java.util.Collection)1 List (java.util.List)1