use of org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor 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;
}
use of org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor 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);
}
use of org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor 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();
}
use of org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor in project eclipselink by eclipse-ee4j.
the class SimpleSerializeFetchGroupTests method attrAndVHContainSameObjectAfterGetRealAttributeValue.
@Test
public void attrAndVHContainSameObjectAfterGetRealAttributeValue() throws Exception {
String errorMsg = "";
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
Query query = em.createQuery("SELECT e FROM Employee e WHERE e.address IS NOT NULL and e.manager IS NOT NULL");
List<Employee> employees = query.getResultList();
OneToOneMapping addressMapping = (OneToOneMapping) employeeDescriptor.getMappingForAttributeName("address");
OneToOneMapping managerMapping = (OneToOneMapping) employeeDescriptor.getMappingForAttributeName("manager");
InstanceVariableAttributeAccessor addressAccessor = new InstanceVariableAttributeAccessor();
addressAccessor.setAttributeName("address");
addressAccessor.initializeAttributes(Employee.class);
InstanceVariableAttributeAccessor managerAccessor = new InstanceVariableAttributeAccessor();
managerAccessor.setAttributeName("manager");
managerAccessor.initializeAttributes(Employee.class);
AbstractSession session = (AbstractSession) ((EntityManagerImpl) em.getDelegate()).getActiveSession();
for (Employee emp : employees) {
String localErrorMsg = "";
Address addressVH = (Address) addressMapping.getRealAttributeValueFromObject(emp, session);
Address addressAttr = (Address) addressAccessor.getAttributeValueFromObject(emp);
if (addressVH != addressAttr) {
localErrorMsg += "\n\taddressVH = " + addressVH + " != addressAttr = " + addressAttr;
}
Employee managerVH = (Employee) managerMapping.getRealAttributeValueFromObject(emp, session);
Employee managerAttr = (Employee) managerAccessor.getAttributeValueFromObject(emp);
if (managerVH != managerAttr) {
localErrorMsg += "\n\tmanagerVH = " + managerVH + " != managerAttr = " + managerAttr;
}
if (localErrorMsg.length() > 0) {
localErrorMsg = emp.toString() + localErrorMsg + "\n";
errorMsg += localErrorMsg;
}
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
if (errorMsg.length() > 0) {
errorMsg = '\n' + errorMsg;
fail(errorMsg);
}
}
use of org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor in project eclipselink by eclipse-ee4j.
the class XMLVariableXPathCollectionMapping method initialize.
@Override
public void initialize(AbstractSession session) throws DescriptorException {
super.initialize(session);
if (variableAttributeAccessor == null) {
if (variableAttributeName != null) {
this.variableAttributeAccessor = new InstanceVariableAttributeAccessor();
this.variableAttributeAccessor.setAttributeName(variableAttributeName);
} else if (variableGetMethodName != null) {
this.variableAttributeAccessor = new MethodAttributeAccessor();
this.variableAttributeAccessor.setAttributeName("VARIABLE");
((MethodAttributeAccessor) this.variableAttributeAccessor).setGetMethodName(variableGetMethodName);
if (variableSetMethodName == null) {
this.variableAttributeAccessor.setIsWriteOnly(true);
} else {
((MethodAttributeAccessor) this.variableAttributeAccessor).setSetMethodName(variableSetMethodName);
}
}
}
this.variableAttributeAccessor.initializeAttributes(this.getReferenceClass());
}
Aggregations