use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method getFieldPropertiesForClass.
public ArrayList<Property> getFieldPropertiesForClass(JavaClass cls, TypeInfo info, boolean onlyPublic, boolean onlyExplicit) {
ArrayList<Property> properties = new ArrayList<>();
if (cls == null) {
return properties;
}
for (JavaField javaField : cls.getDeclaredFields()) {
Property property = null;
int modifiers = javaField.getModifiers();
if (!Modifier.isTransient(modifiers) && ((Modifier.isPublic(javaField.getModifiers()) && onlyPublic) || !onlyPublic || hasJAXBAnnotations(javaField))) {
if (!Modifier.isStatic(modifiers)) {
if ((onlyExplicit && hasJAXBAnnotations(javaField)) || !onlyExplicit) {
try {
property = buildNewProperty(info, cls, javaField, javaField.getName(), javaField.getResolvedType());
properties.add(property);
} catch (JAXBException ex) {
if (ex.getErrorCode() != JAXBException.INVALID_INTERFACE || !helper.isAnnotationPresent(javaField, XmlTransient.class)) {
throw ex;
}
}
}
} else {
try {
property = buildNewProperty(info, cls, javaField, javaField.getName(), javaField.getResolvedType());
if (helper.isAnnotationPresent(javaField, XmlAttribute.class)) {
Object value = ((JavaFieldImpl) javaField).get(null);
if (value != null) {
String stringValue = XMLConversionManager.getDefaultXMLManager().convertObject(value, String.class, property.getSchemaType());
property.setFixedValue(stringValue);
}
}
property.setWriteOnly(true);
if (!hasJAXBAnnotations(javaField)) {
property.setTransient(true);
}
properties.add(property);
} catch (ClassCastException e) {
// do Nothing
} catch (IllegalAccessException e) {
// do Nothing
} catch (JAXBException ex) {
if (ex.getErrorCode() != JAXBException.INVALID_INTERFACE || !helper.isAnnotationPresent(javaField, XmlTransient.class)) {
throw ex;
}
}
}
}
if (helper.isAnnotationPresent(javaField, XmlTransient.class)) {
if (property != null) {
property.setTransient(true);
}
}
}
return properties;
}
use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.
the class XMLProcessor method createProperty.
private Property createProperty(TypeInfo info, JavaAttribute javaAttribute) {
XmlAccessType xmlAccessorType = javaAttribute.getXmlAccessorType();
// Property prop = new Property();
// prop.setPropertyName(javaAttribute.getJavaAttribute());
String propName = javaAttribute.getJavaAttribute();
JavaHasAnnotations element = null;
JavaClass pType = null;
JavaClass jClass = this.jModelInput.getJavaModel().getClass(info.getJavaClassName());
if (xmlAccessorType == XmlAccessType.PROPERTY) {
// set up accessor method names
String name = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
String getMethodName = GET_STR + name;
String setMethodName = SET_STR + name;
JavaMethod jMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
if (jMethod == null) {
getMethodName = IS_STR + name;
jMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
}
if (jMethod != null) {
pType = jMethod.getReturnType();
element = jMethod;
} else {
// look for a set method if type is set on javaAttribute
for (Object next : jClass.getDeclaredMethods()) {
JavaMethod nextMethod = (JavaMethod) next;
if (nextMethod.getName().equals(setMethodName) && nextMethod.getParameterTypes().length == 1) {
pType = nextMethod.getParameterTypes()[0];
element = nextMethod;
}
}
if (element == null) {
return null;
}
}
} else {
JavaField jField = jClass.getDeclaredField(propName);
if (jField == null) {
return null;
}
pType = jField.getResolvedType();
element = jField;
}
return this.aProcessor.buildNewProperty(info, jClass, element, propName, pType);
}
use of org.eclipse.persistence.jaxb.javamodel.JavaField in project eclipselink by eclipse-ee4j.
the class JavaClassImpl method getFields.
public Collection getFields() {
ArrayList<JavaField> fieldCollection = new ArrayList<>();
Field[] fields = PrivilegedAccessHelper.getFields(jClass);
for (Field field : fields) {
fieldCollection.add(getJavaField(field));
}
return fieldCollection;
}
use of org.eclipse.persistence.jaxb.javamodel.JavaField 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.JavaField 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;
}
Aggregations