use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateXMLObjectReferenceMapping.
/**
* Create an XMLObjectReferenceMapping and add it to the descriptor.
*/
public ObjectReferenceMapping generateXMLObjectReferenceMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, JavaClass referenceClass) {
ObjectReferenceMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, UnmarshalRecord, XMLField, XMLRecord> mapping = new XMLObjectReferenceMapping();
initializeXMLMapping((XMLMapping) mapping, property);
mapping.setReferenceClassName(referenceClass.getQualifiedName());
// here we need to setup source/target key field associations
if (property.isSetXmlJoinNodes()) {
for (XmlJoinNode xmlJoinNode : property.getXmlJoinNodes().getXmlJoinNode()) {
validateJoinNode(descriptor.getJavaClassName(), property, xmlJoinNode.getReferencedXmlPath(), referenceClass);
mapping.addSourceToTargetKeyFieldAssociation(xmlJoinNode.getXmlPath(), xmlJoinNode.getReferencedXmlPath());
}
} else {
String tgtXPath = null;
TypeInfo referenceType = typeInfo.get(referenceClass.getQualifiedName());
if (null != referenceType && referenceType.isIDSet()) {
Property prop = referenceType.getIDProperty();
tgtXPath = getXPathForField(prop, namespaceInfo, !prop.isAttribute(), false).getXPath();
}
// if the XPath is set (via xml-path) use it, otherwise figure it out
Field srcXPath;
if (property.getXmlPath() != null) {
srcXPath = new XMLField(property.getXmlPath());
} else {
srcXPath = getXPathForField(property, namespaceInfo, true, false);
}
mapping.addSourceToTargetKeyFieldAssociation(srcXPath.getXPath(), tgtXPath);
}
if (property.getInverseReferencePropertyName() != null) {
mapping.getInverseReferenceMapping().setAttributeName(property.getInverseReferencePropertyName());
JavaClass backPointerPropertyType = null;
if (property.getInverseReferencePropertyGetMethodName() != null && property.getInverseReferencePropertySetMethodName() != null && !property.getInverseReferencePropertyGetMethodName().equals("") && !property.getInverseReferencePropertySetMethodName().equals("")) {
mapping.getInverseReferenceMapping().setGetMethodName(property.getInverseReferencePropertySetMethodName());
mapping.getInverseReferenceMapping().setSetMethodName(property.getInverseReferencePropertySetMethodName());
JavaMethod getMethod = referenceClass.getDeclaredMethod(mapping.getInverseReferenceMapping().getGetMethodName(), new JavaClass[] {});
if (getMethod != null) {
backPointerPropertyType = getMethod.getReturnType();
}
} else {
JavaField backpointerField = referenceClass.getDeclaredField(property.getInverseReferencePropertyName());
if (backpointerField != null) {
backPointerPropertyType = backpointerField.getResolvedType();
}
}
if (helper.isCollectionType(backPointerPropertyType)) {
mapping.getInverseReferenceMapping().setContainerPolicy(ContainerPolicy.buildDefaultPolicy());
}
}
return mapping;
}
use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateXMLCollectionReferenceMapping.
/**
* Create an XMLCollectionReferenceMapping and add it to the descriptor.
*/
public CollectionReferenceMapping generateXMLCollectionReferenceMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, JavaClass referenceClass) {
CollectionReferenceMapping<AbstractSession, AttributeAccessor, ContainerPolicy, ClassDescriptor, DatabaseField, UnmarshalRecord, XMLField, XMLRecord> mapping = new XMLCollectionReferenceMapping();
initializeXMLMapping((XMLMapping) mapping, property);
initializeXMLContainerMapping(mapping, property.getType().isArray());
mapping.setUsesSingleNode(property.isXmlList() || (property.isAttribute() && (property.getXmlPath() == null || !property.getXmlPath().contains("/"))));
String referenceClassName = referenceClass.getQualifiedName();
JavaClass collectionType = property.getType();
if (collectionType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
JavaClass componentType = collectionType.getComponentType();
if (componentType.isArray()) {
Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getName());
referenceClassName = adaptedClass.getName();
accessor.setAdaptedClassName(referenceClassName);
JavaClass baseComponentType = getBaseComponentType(componentType);
if (baseComponentType.isPrimitive()) {
Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
accessor.setComponentClass(primitiveClass);
} else {
accessor.setComponentClassName(baseComponentType.getQualifiedName());
}
} else {
accessor.setComponentClassName(componentType.getQualifiedName());
}
mapping.setAttributeAccessor(accessor);
}
collectionType = containerClassImpl(collectionType);
mapping.useCollectionClassName(collectionType.getRawName());
mapping.setReferenceClassName(referenceClassName);
// here we need to setup source/target key field associations
if (property.isSetXmlJoinNodes()) {
for (XmlJoinNode xmlJoinNode : property.getXmlJoinNodes().getXmlJoinNode()) {
validateJoinNode(descriptor.getJavaClassName(), property, xmlJoinNode.getReferencedXmlPath(), referenceClass);
mapping.addSourceToTargetKeyFieldAssociation(xmlJoinNode.getXmlPath(), xmlJoinNode.getReferencedXmlPath());
}
} else {
// here we need to setup source/target key field associations
TypeInfo referenceType = typeInfo.get(referenceClass.getQualifiedName());
String tgtXPath = null;
if (null != referenceType && referenceType.isIDSet()) {
Property prop = referenceType.getIDProperty();
tgtXPath = getXPathForField(prop, namespaceInfo, !prop.isAttribute(), false).getXPath();
}
// if the XPath is set (via xml-path) use it
Field srcXPath;
if (property.getXmlPath() != null) {
srcXPath = new XMLField(property.getXmlPath());
} else {
srcXPath = getXPathForField(property, namespaceInfo, true, false);
}
mapping.addSourceToTargetKeyFieldAssociation(srcXPath.getXPath(), tgtXPath);
}
if (property.getInverseReferencePropertyName() != null) {
mapping.getInverseReferenceMapping().setAttributeName(property.getInverseReferencePropertyName());
JavaClass backPointerPropertyType = null;
if (property.getInverseReferencePropertyGetMethodName() != null && property.getInverseReferencePropertySetMethodName() != null && !property.getInverseReferencePropertyGetMethodName().equals("") && !property.getInverseReferencePropertySetMethodName().equals("")) {
mapping.getInverseReferenceMapping().setGetMethodName(property.getInverseReferencePropertySetMethodName());
mapping.getInverseReferenceMapping().setSetMethodName(property.getInverseReferencePropertySetMethodName());
JavaMethod getMethod = referenceClass.getDeclaredMethod(mapping.getInverseReferenceMapping().getGetMethodName(), new JavaClass[] {});
if (getMethod != null) {
backPointerPropertyType = getMethod.getReturnType();
}
} else {
JavaField backpointerField = referenceClass.getDeclaredField(property.getInverseReferencePropertyName());
if (backpointerField != null) {
backPointerPropertyType = backpointerField.getResolvedType();
}
}
if (helper.isCollectionType(backPointerPropertyType)) {
mapping.getInverseReferenceMapping().setContainerPolicy(ContainerPolicy.buildDefaultPolicy());
}
}
return mapping;
}
use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method postProcessXmlType.
/**
* Process XmlType for a given TypeInfo. Here we assume that the TypeInfo
* has an XmlType set - typically via preProcessXmlType or XmlProcessor
* override.
*/
private void postProcessXmlType(JavaClass javaClass, TypeInfo info, PackageInfo packageNamespace) {
// assumes that the TypeInfo has an XmlType set from
org.eclipse.persistence.jaxb.xmlmodel.XmlType xmlType = info.getXmlType();
// set/validate factoryClass and factoryMethod
String factoryClassName = xmlType.getFactoryClass();
String factoryMethodName = xmlType.getFactoryMethod();
if (factoryClassName.equals("jakarta.xml.bind.annotation.XmlType.DEFAULT")) {
if (factoryMethodName != null && !factoryMethodName.equals(EMPTY_STRING)) {
// factory method applies to the current class - verify method
// exists
JavaMethod method = javaClass.getDeclaredMethod(factoryMethodName, new JavaClass[] {});
if (method == null) {
throw org.eclipse.persistence.exceptions.JAXBException.factoryMethodNotDeclared(factoryMethodName, javaClass.getName());
}
info.setFactoryMethodName(factoryMethodName);
}
} else {
if (factoryMethodName == null || factoryMethodName.equals(EMPTY_STRING)) {
throw org.eclipse.persistence.exceptions.JAXBException.factoryClassWithoutFactoryMethod(javaClass.getName());
}
info.setObjectFactoryClassName(factoryClassName);
info.setFactoryMethodName(factoryMethodName);
}
// figure out type name
String typeName = xmlType.getName();
if (typeName.equals(XMLProcessor.DEFAULT)) {
try {
typeName = info.getXmlNameTransformer().transformTypeName(javaClass.getName());
} catch (Exception ex) {
throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
}
}
info.setSchemaTypeName(typeName);
// set propOrder
if (xmlType.isSetPropOrder()) {
List<String> props = xmlType.getPropOrder();
if (props.size() == 0) {
info.setPropOrder(new String[0]);
} else if (props.get(0).equals(EMPTY_STRING)) {
info.setPropOrder(new String[] { EMPTY_STRING });
} else {
info.setPropOrder(xmlType.getPropOrder().toArray(new String[xmlType.getPropOrder().size()]));
}
}
// figure out namespace
if (xmlType.getNamespace().equals(XMLProcessor.DEFAULT)) {
info.setClassNamespace(packageNamespace.getNamespace());
} else {
info.setClassNamespace(xmlType.getNamespace());
}
}
use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.
the class CompilerHelper method getTypeFromAdapterClass.
/**
* If adapter class is null return null If there is a marshal method that
* returns something other than Object on the adapter class return the
* return type of that method Otherwise return Object.class
*/
public static JavaClass getTypeFromAdapterClass(JavaClass adapterClass, Helper helper) {
if (adapterClass != null) {
// JavaClass declJavaType = Object.class;
JavaClass declJavaType = helper.getJavaClass(Object.class);
// look for marshal method
Object[] tacMethods = adapterClass.getMethods().toArray();
for (int i = 0; i < tacMethods.length; i++) {
JavaMethod method = (JavaMethod) tacMethods[i];
if (method.getName().equals("marshal")) {
JavaClass returnType = method.getReturnType();
if (!(returnType.getQualifiedName().equals(declJavaType.getQualifiedName()))) {
declJavaType = returnType;
return declJavaType;
}
}
}
return declJavaType;
}
return null;
}
use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.
the class XMLProcessor method processPropertyForAccessorType.
private Property processPropertyForAccessorType(TypeInfo typeInfo, JavaAttribute javaAttribute, Property originalProperty) {
if (originalProperty == null) {
Property prop = createProperty(typeInfo, javaAttribute);
if (prop != null) {
typeInfo.addProperty(prop.getPropertyName(), prop);
}
return prop;
}
if ((javaAttribute.getXmlAccessorType() == XmlAccessType.FIELD && !(originalProperty.isMethodProperty())) || javaAttribute.getXmlAccessorType() == XmlAccessType.PROPERTY && originalProperty.isMethodProperty()) {
return originalProperty;
}
originalProperty.setMethodProperty(!(originalProperty.isMethodProperty()));
if (originalProperty.isMethodProperty()) {
// figure out get and set method names. See if they exist.
JavaClass jClass = this.jModelInput.getJavaModel().getClass(typeInfo.getJavaClassName());
String propName = originalProperty.getPropertyName();
propName = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
String getMethodName = GET_STR + propName;
String setMethodName = SET_STR + propName;
JavaMethod getMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
if (getMethod == null) {
getMethodName = IS_STR + propName;
getMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
}
JavaMethod setMethod = jClass.getDeclaredMethod(setMethodName, new JavaClass[] { originalProperty.getType() });
if (getMethod != null) {
originalProperty.setGetMethodName(getMethodName);
}
if (setMethod != null) {
originalProperty.setSetMethodName(setMethodName);
}
} else {
originalProperty.setGetMethodName(null);
originalProperty.setSetMethodName(null);
originalProperty.setMethodProperty(false);
}
return originalProperty;
}
Aggregations