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;
}
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);
}
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();
}
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);
}
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;
}
}
Aggregations