Java Code Examples for javax.xml.bind.annotation.XmlAccessType

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project commons-j, under directory /src/main/java/nerds/antelax/commons/xml/.

Source file:


 * Checks a JAXB-annotated object to make sure all required data is present, however it does not check any data formatting constraints. Traverses the entire object graph to make sure sub-elements are also correctly populated.
 * @return <code>true</code> if all attributes and fields (and sub-fields) that are required are present, or if the object isnot a JAXB-generated class. Returns <code>false</code> otherwise.
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IntrospectionException
public static boolean hasAllRequiredData(final Object jaxb) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException, IntrospectionException {
  final Class<?> clazz=jaxb.getClass();
  final XmlAccessorType xmlAccessorType=clazz.getAnnotation(XmlAccessorType.class);
  final XmlType xmlType=clazz.getAnnotation(XmlType.class);
  if (xmlAccessorType == null)   return true;
  Preconditions.checkArgument(xmlAccessorType.value() == XmlAccessType.FIELD,"Only JAXB fields are supported at this time");
  if (xmlType == null)   return true;
  final String[] properties=combine(xmlType.propOrder(),xmlAttributeProperties(clazz));
  for (  final String property : properties) {
    final Field field=clazz.getDeclaredField(property);
    final boolean isList=field.getType() == List.class;
    if (field.getAnnotation(XmlTransient.class) != null)     continue;
    final XmlElement elementAnnotation=field.getAnnotation(XmlElement.class);
    final XmlAttribute attributeAnnotation=field.getAnnotation(XmlAttribute.class);
    final boolean required=elementAnnotation != null ? elementAnnotation.required() : attributeAnnotation != null ? attributeAnnotation.required() : false;
    if (isList) {
      final List<?> list=(List<?>)getJaxbProperty(jaxb,property);
      final Boolean emptyList=(Boolean)findMethod(list,"isEmpty").invoke(list);
      if (required && emptyList)       return false;
 else       for (      final Object listElement : list)       if (!hasAllRequiredData(listElement))       return false;
 else {
      final Object propertyValue=getJaxbProperty(jaxb,property);
      if (required && propertyValue == null)       return false;
 else       if (propertyValue != null)       if (!hasAllRequiredData(propertyValue))       return false;
  return true;