use of org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method getNoAccessTypePropertiesForClass.
public ArrayList getNoAccessTypePropertiesForClass(JavaClass cls, TypeInfo info) {
ArrayList<Property> list = new ArrayList<>();
if (cls == null) {
return list;
}
// Iterate over the field and method properties. If ANYTHING contains an
// annotation and
// doesn't appear in the other list, add it to the final list
List<Property> fieldProperties = getFieldPropertiesForClass(cls, info, false);
Map<String, Property> fields = new HashMap<>(fieldProperties.size());
for (Property next : fieldProperties) {
JavaHasAnnotations elem = next.getElement();
if (!hasJAXBAnnotations(elem)) {
next.setTransient(true);
}
list.add(next);
fields.put(next.getPropertyName(), next);
}
List<Property> methodProperties = getPropertyPropertiesForClass(cls, info, false);
for (Property next : methodProperties) {
JavaHasAnnotations elem = next.getElement();
if (hasJAXBAnnotations(elem)) {
// If the property is annotated remove the corresponding field
Property fieldProperty = fields.get(next.getPropertyName());
list.remove(fieldProperty);
list.add(next);
} else {
// If the property is not annotated only add it if there is no
// corresponding field.
next.setTransient(true);
if (fields.get(next.getPropertyName()) == null) {
list.add(next);
}
}
}
return list;
}
use of org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method processPropertyAnnotations.
private void processPropertyAnnotations(TypeInfo info, JavaClass cls, JavaHasAnnotations propertyElement, Property property) {
// Check for mixed context
if (helper.isAnnotationPresent(propertyElement, XmlMixed.class)) {
info.setMixed(true);
property.setMixedContent(true);
findAndProcessObjectFactory(cls);
}
if (helper.isAnnotationPresent(propertyElement, XmlInverseReference.class)) {
XmlInverseReference inverseReference = (XmlInverseReference) helper.getAnnotation(propertyElement, XmlInverseReference.class);
property.setInverseReferencePropertyName(inverseReference.mappedBy());
TypeInfo targetInfo = this.getTypeInfos().get(property.getActualType().getName());
if (targetInfo != null && targetInfo.getXmlAccessType() == XmlAccessType.PROPERTY) {
String propName = property.getPropertyName();
propName = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
property.setInverseReferencePropertyGetMethodName(GET_STR + propName);
property.setInverseReferencePropertySetMethodName(SET_STR + propName);
}
property.setInverseReference(true, helper.isAnnotationPresent(propertyElement, XmlElement.class));
}
processXmlJavaTypeAdapter(property, info, cls);
if (helper.isAnnotationPresent(propertyElement, XmlAttachmentRef.class) && areEquals(property.getActualType(), JAVAX_ACTIVATION_DATAHANDLER)) {
property.setIsSwaAttachmentRef(true);
property.setSchemaType(Constants.SWA_REF_QNAME);
}
processXmlElement(property, info);
// JavaClass ptype = property.getActualType();
if (!(property.isSwaAttachmentRef()) && isMtomAttachment(property)) {
property.setIsMtomAttachment(true);
property.setSchemaType(Constants.BASE_64_BINARY_QNAME);
}
if (helper.isAnnotationPresent(propertyElement, XmlMimeType.class)) {
property.setMimeType(((XmlMimeType) helper.getAnnotation(propertyElement, XmlMimeType.class)).value());
}
// non-binary data type won't have any affect
if (helper.isAnnotationPresent(propertyElement, XmlInlineBinaryData.class) || info.isBinaryDataToBeInlined()) {
property.setisInlineBinaryData(true);
}
// later use:
if (helper.isAnnotationPresent(propertyElement, XmlSchemaType.class)) {
XmlSchemaType schemaType = (XmlSchemaType) helper.getAnnotation(propertyElement, XmlSchemaType.class);
QName schemaTypeQname = new QName(schemaType.namespace(), schemaType.name());
property.setSchemaType(schemaTypeQname);
}
if (helper.isAnnotationPresent(propertyElement, XmlAttribute.class)) {
property.setIsAttribute(true);
property.setIsRequired(((XmlAttribute) helper.getAnnotation(propertyElement, XmlAttribute.class)).required());
}
if (helper.isAnnotationPresent(propertyElement, XmlAnyAttribute.class)) {
if (info.isSetAnyAttributePropertyName() && !info.getAnyAttributePropertyName().equals(property.getPropertyName())) {
throw org.eclipse.persistence.exceptions.JAXBException.multipleAnyAttributeMapping(cls.getName());
}
if (!helper.isMapType(property.getType())) {
throw org.eclipse.persistence.exceptions.JAXBException.anyAttributeOnNonMap(property.getPropertyName());
}
property.setIsAnyAttribute(true);
info.setAnyAttributePropertyName(property.getPropertyName());
}
// Make sure XmlElementWrapper annotation is on a collection or array
if (helper.isAnnotationPresent(propertyElement, XmlElementWrapper.class)) {
XmlElementWrapper wrapper = (XmlElementWrapper) helper.getAnnotation(propertyElement, XmlElementWrapper.class);
org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper xmlEltWrapper = new org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper();
String wrapperName = wrapper.name();
if (wrapperName.equals(XMLProcessor.DEFAULT)) {
wrapperName = info.getXmlNameTransformer().transformElementName(property.getPropertyName());
}
xmlEltWrapper.setName(wrapperName);
xmlEltWrapper.setNamespace(wrapper.namespace());
xmlEltWrapper.setNillable(wrapper.nillable());
xmlEltWrapper.setRequired(wrapper.required());
property.setXmlElementWrapper(xmlEltWrapper);
}
if (helper.isAnnotationPresent(propertyElement, XmlList.class)) {
// Make sure XmlList annotation is on a collection or array
if (!helper.isCollectionType(property.getType()) && !property.getType().isArray()) {
throw JAXBException.invalidList(property.getPropertyName());
}
property.setIsXmlList(true);
}
if (helper.isAnnotationPresent(propertyElement, XmlValue.class)) {
property.setIsXmlValue(true);
info.setXmlValueProperty(property);
}
if (helper.isAnnotationPresent(propertyElement, XmlReadOnly.class)) {
property.setReadOnly(true);
}
if (helper.isAnnotationPresent(propertyElement, XmlWriteOnly.class)) {
property.setWriteOnly(true);
}
if (helper.isAnnotationPresent(propertyElement, XmlCDATA.class)) {
property.setCdata(true);
}
if (helper.isAnnotationPresent(propertyElement, XmlAccessMethods.class)) {
XmlAccessMethods accessMethods = (XmlAccessMethods) helper.getAnnotation(propertyElement, XmlAccessMethods.class);
if (!(accessMethods.getMethodName().equals(EMPTY_STRING))) {
property.setGetMethodName(accessMethods.getMethodName());
}
if (!(accessMethods.setMethodName().equals(EMPTY_STRING))) {
property.setSetMethodName(accessMethods.setMethodName());
}
if (!(property.isMethodProperty())) {
property.setMethodProperty(true);
}
}
// handle user properties
if (helper.isAnnotationPresent(propertyElement, XmlProperties.class)) {
XmlProperties xmlProperties = (XmlProperties) helper.getAnnotation(propertyElement, XmlProperties.class);
Map<Object, Object> propertiesMap = createUserPropertiesMap(xmlProperties.value());
property.setUserProperties(propertiesMap);
} else if (helper.isAnnotationPresent(propertyElement, XmlProperty.class)) {
XmlProperty xmlProperty = (XmlProperty) helper.getAnnotation(propertyElement, XmlProperty.class);
Map<Object, Object> propertiesMap = createUserPropertiesMap(new XmlProperty[] { xmlProperty });
property.setUserProperties(propertiesMap);
}
// handle XmlKey
if (helper.isAnnotationPresent(propertyElement, XmlKey.class)) {
info.addXmlKeyProperty(property);
}
// handle XmlJoinNode(s)
processXmlJoinNodes(property);
processXmlNullPolicy(property, cls, info);
// Handle XmlLocation
JavaHasAnnotations elem = propertyElement;
if (helper.isAnnotationPresent(elem, XmlLocation.class) || helper.isAnnotationPresent(elem, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(elem, CompilerHelper.OLD_XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(elem, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS)) {
if (!helper.getJavaClass(Constants.LOCATOR_CLASS).isAssignableFrom(property.getType())) {
throw JAXBException.invalidXmlLocation(property.getPropertyName(), property.getType().getName());
}
property.setXmlLocation(true);
}
}
use of org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations 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.JavaHasAnnotations in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method addFacets.
/**
* @since 2.6
* @author Marcel Valovy
* @param property property for which facets will be generated
*/
private void addFacets(Property property) {
final JavaHasAnnotations element = property.getElement();
if (helper.isAnnotationPresent(element, DecimalMin.class)) {
DecimalMin a = (DecimalMin) helper.getAnnotation(element, DecimalMin.class);
DecimalMinFacet facet = new DecimalMinFacet(a.value(), a.inclusive());
property.addFacet(facet);
}
if (helper.isAnnotationPresent(element, DecimalMax.class)) {
DecimalMax a = (DecimalMax) helper.getAnnotation(element, DecimalMax.class);
DecimalMaxFacet facet = new DecimalMaxFacet(a.value(), a.inclusive());
property.addFacet(facet);
}
if (helper.isAnnotationPresent(element, Digits.class)) {
Digits a = (Digits) helper.getAnnotation(element, Digits.class);
DigitsFacet facet = new DigitsFacet(a.integer(), a.fraction());
property.addFacet(facet);
}
if (helper.isAnnotationPresent(element, Max.class)) {
Max a = (Max) helper.getAnnotation(element, Max.class);
MaxFacet facet = new MaxFacet(a.value());
property.addFacet(facet);
}
if (helper.isAnnotationPresent(element, Min.class)) {
Min a = (Min) helper.getAnnotation(element, Min.class);
MinFacet facet = new MinFacet(a.value());
property.addFacet(facet);
}
if (helper.isAnnotationPresent(element, NotNull.class)) {
property.setNotNullAnnotated(true);
}
if (helper.isAnnotationPresent(element, Pattern.class)) {
Pattern a = (Pattern) helper.getAnnotation(element, Pattern.class);
PatternFacet facet = new PatternFacet(a.regexp(), a.flags());
property.addFacet(facet);
}
/* Example:
@Pattern.List({
@Pattern(regexp = "first_expression", message = "first.Pattern.message"),
@Pattern(regexp = "second_expression", message = "second.Pattern.message"),
@Pattern(regexp = "third_expression", message = "third.Pattern.message")
}) */
if (helper.isAnnotationPresent(element, Pattern.List.class)) {
Pattern.List a = (Pattern.List) helper.getAnnotation(element, Pattern.List.class);
PatternListFacet facet = new PatternListFacet(new ArrayList<>());
for (Pattern pat : a.value()) {
PatternFacet pf = new PatternFacet(pat.regexp(), pat.flags());
facet.addPattern(pf);
}
property.addFacet(facet);
}
if (helper.isAnnotationPresent(element, Size.class)) {
Size a = (Size) helper.getAnnotation(element, Size.class);
final int min = a.min();
final int max = a.max();
if (min != 0 || max != Integer.MAX_VALUE) {
// Fixes generation of an empty facet.
if ("java.lang.String".equals(property.getType().getName())) {
// @Size serves for both length facet and occurs restriction.
// For minLength, maxLength.
SizeFacet facet = new SizeFacet(min, max);
property.addFacet(facet);
} else {
// 0 is default minBoundary.
if (min > 0)
property.setMinOccurs(min);
// 2^31 is default maxBoundary.
if (max < Integer.MAX_VALUE)
property.setMaxOccurs(max);
}
}
}
}
Aggregations