@Test
 public void acceptedCommandsCanBeSerializedTest() throws Exception {
   Field commandsField = JaxbCommandsRequest.class.getDeclaredField("commands");
   XmlElements xmlElemsAnno = (XmlElements) commandsField.getAnnotations()[0];
   XmlElement[] xmlElems = xmlElemsAnno.value();
   Set<Class> cmdSet = new HashSet<Class>(AcceptedCommands.getSet());
   assertEquals(cmdSet.size(), xmlElems.length);
   Set<String> xmlElemNameSet = new HashSet<String>();
   for (XmlElement xmlElemAnno : xmlElems) {
     Class cmdClass = xmlElemAnno.type();
     String name = xmlElemAnno.name();
     assertTrue(name + " is used twice as a name.", xmlElemNameSet.add(name));
     assertTrue(
         cmdClass.getSimpleName()
             + " is present in "
             + JaxbCommandsRequest.class.getSimpleName()
             + " but not in "
             + AcceptedCommands.class.getSimpleName(),
         cmdSet.remove(cmdClass));
   }
   for (Class cmdClass : cmdSet) {
     System.out.println("Missing: " + cmdClass.getSimpleName());
   }
   assertTrue(
       "See output for classes in "
           + AcceptedCommands.class.getSimpleName()
           + " that are not in "
           + JaxbCommandsRequest.class.getSimpleName(),
       cmdSet.size() == 0);
 }
Beispiel #2
0
  public Element(
      javax.lang.model.element.Element delegate,
      TypeDefinition typedef,
      EnunciateJaxbContext context) {
    super(delegate, typedef, context);

    XmlElement xmlElement = getAnnotation(XmlElement.class);
    XmlElements xmlElements = getAnnotation(XmlElements.class);
    if (xmlElements != null) {
      XmlElement[] elementChoices = xmlElements.value();
      if (elementChoices.length == 0) {
        xmlElements = null;
      } else if ((xmlElement == null) && (elementChoices.length == 1)) {
        xmlElement = elementChoices[0];
        xmlElements = null;
      }
    }

    this.xmlElement = xmlElement;
    this.choices = new ArrayList<Element>();
    if (xmlElements != null) {
      for (final XmlElement element : xmlElements.value()) {
        DecoratedTypeMirror typeMirror =
            Annotations.mirrorOf(
                new Callable<Class<?>>() {
                  @Override
                  public Class<?> call() throws Exception {
                    return element.type();
                  }
                },
                this.env,
                XmlElement.DEFAULT.class);

        if ((typeMirror instanceof ArrayType
                && ((ArrayType) typeMirror).getComponentType().getKind() != TypeKind.BYTE)
            || (typeMirror.isCollection())) {
          throw new EnunciateException(
              "Member "
                  + getName()
                  + " of "
                  + typedef.getQualifiedName()
                  + ": an element choice must not be a collection or an array.");
        }

        this.choices.add(new Element(getDelegate(), getTypeDefinition(), element, context));
      }
    } else {
      this.choices.add(this);
    }
  }
Beispiel #3
0
 private void processFieldRelatedAnnotations(
     Annotation annots[], String fieldName, Type defaultGenericType) {
   WrappedElementInfo wrappedInfo = null;
   for (Annotation annot : annots) {
     if (annot instanceof XmlElementWrapper) {
       XmlElementWrapper wrapper = (XmlElementWrapper) annot;
       wrappedInfo = new WrappedElementInfo(wrapper, fieldName);
       jaxbElemNodeInfo.add(wrappedInfo);
       break;
     }
   }
   for (Annotation annot : annots) {
     if (annot instanceof XmlValue) {
       elementValue = new JaxbValueInfo((XmlValue) annot, fieldName, defaultGenericType);
     } else if (annot instanceof XmlAttribute) {
       XmlAttribute attr = (XmlAttribute) annot;
       String attrName = attr.name();
       if ((attrName == null) || DEFAULT_MARKER.equals(attrName)) {
         attrName = fieldName;
       }
       this.setXmlAttributeInfo(attr, fieldName, defaultGenericType);
       this.attributeNames.add(attrName);
     } else if (annot instanceof XmlElement) {
       XmlElement xmlElem = (XmlElement) annot;
       if (wrappedInfo == null) {
         setXmlElementInfo(xmlElem, fieldName, defaultGenericType);
       } else {
         wrappedInfo.add(xmlElem, fieldName, defaultGenericType);
       }
     } else if (annot instanceof XmlElementRef) {
       XmlElementRef xmlElemR = (XmlElementRef) annot;
       if (wrappedInfo == null) {
         setXmlElementInfo(xmlElemR, null, null);
       } else {
         wrappedInfo.add(xmlElemR, null, null);
       }
     } else if (annot instanceof XmlElements) {
       JaxbPseudoNodeChoiceInfo choiceNode =
           new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
       if (wrappedInfo == null) {
         jaxbElemNodeInfo.add(choiceNode);
       } else {
         wrappedInfo.add(choiceNode);
       }
       XmlElements xmlElemsAnnot = (XmlElements) annot;
       for (XmlElement xmlE : xmlElemsAnnot.value()) {
         choiceNode.add(xmlE);
       }
     } else if (annot instanceof XmlElementRefs) {
       JaxbPseudoNodeChoiceInfo choiceNode =
           new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
       if (wrappedInfo == null) {
         jaxbElemNodeInfo.add(choiceNode);
       } else {
         wrappedInfo.add(choiceNode);
       }
       XmlElementRefs elemRefs = (XmlElementRefs) annot;
       for (XmlElementRef xmlE : elemRefs.value()) {
         choiceNode.add(xmlE);
       }
     }
   }
 }