@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);
 }
Example #2
0
  /**
   * The default value, or null if none exists.
   *
   * @return The default value, or null if none exists.
   */
  public String getDefaultValue() {
    String defaultValue = null;

    if ((xmlElement != null) && (!"\u0000".equals(xmlElement.defaultValue()))) {
      defaultValue = xmlElement.defaultValue();
    }

    return defaultValue;
  }
Example #3
0
  // Inherited.
  public String getName() {
    String propertyName = getSimpleName().toString();

    if ((xmlElement != null) && (!"##default".equals(xmlElement.name()))) {
      propertyName = xmlElement.name();
    }

    return propertyName;
  }
Example #4
0
  // Inherited.
  public String getNamespace() {
    String namespace = null;

    if (getForm() == XmlNsForm.QUALIFIED) {
      namespace = getTypeDefinition().getNamespace();
    }

    if ((xmlElement != null) && (!"##default".equals(xmlElement.namespace()))) {
      namespace = xmlElement.namespace();
    }

    return namespace;
  }
Example #5
0
 private static Field getElField(String partName, final Class<?> wrapperType) {
   String fieldName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.VARIABLE);
   Field[] fields = ReflectionUtil.getDeclaredFields(wrapperType);
   for (Field field : fields) {
     XmlElement el = field.getAnnotation(XmlElement.class);
     if (el != null && partName.equals(el.name())) {
       return field;
     }
     if (field.getName().equals(fieldName)) {
       return field;
     }
   }
   return null;
 }
Example #6
0
  /**
   * Whether this element is required.
   *
   * @return Whether this element is required.
   */
  public boolean isRequired() {
    boolean required = BeanValidationUtils.isNotNull(this);

    if (xmlElement != null && !required) {
      required = xmlElement.required();
    }

    return required;
  }
Example #7
0
  /**
   * Whether this element is nillable.
   *
   * @return Whether this element is nillable.
   */
  public boolean isNillable() {
    boolean nillable = false;

    if (xmlElement != null) {
      nillable = xmlElement.nillable();
    }

    return nillable;
  }
Example #8
0
  protected boolean isMandatory(Object bean, String propertyName) {
    // lets look at the setter method and see if its got a @Required
    // annotation
    if (bean instanceof AbstractNode) {
      AbstractNode node = (AbstractNode) bean;
      Class<?> camelClass = node.getCamelDefinitionClass();
      if (camelClass != null) {
        XmlAccessorType accessorType = camelClass.getAnnotation(XmlAccessorType.class);
        boolean useMethods = true;
        if (accessorType != null) {
          if (accessorType.value().equals(XmlAccessType.FIELD)) {
            useMethods = false;
          }
        }
        try {
          BeanInfo beanInfo = Introspector.getBeanInfo(camelClass);
          PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
          if (propertyDescriptors != null) {
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
              if (propertyName.equals(propertyDescriptor.getName())) {
                Method writeMethod = propertyDescriptor.getWriteMethod();
                if (writeMethod != null) {
                  Required annotation = writeMethod.getAnnotation(Required.class);
                  if (annotation != null) {
                    return true;
                  }
                  if (useMethods) {
                    XmlElement element = writeMethod.getAnnotation(XmlElement.class);
                    if (element != null && element.required()) {
                      return true;
                    }
                    XmlAttribute attribute = writeMethod.getAnnotation(XmlAttribute.class);
                    if (attribute != null && attribute.required()) {
                      return true;
                    }
                  }
                }
                break;
              }
            }
          }
          if (!useMethods) {
            Field[] fields = camelClass.getDeclaredFields();
            for (Field field : fields) {
              if (propertyName.equals(field.getName())) {
                Required annotation = field.getAnnotation(Required.class);
                if (annotation != null) {
                  return true;
                }
                XmlElement element = field.getAnnotation(XmlElement.class);
                if (element != null && element.required()) {
                  return true;
                }
                XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);
                if (attribute != null && attribute.required()) {
                  return true;
                }
              }
            }
          }
        } catch (IntrospectionException e) {
          // ignore
        }
      }
    }

    // expression is mandatory on resequence
    if (node instanceof Resequence && "expression".equals(propertyName)) {
      return true;
    }

    // lets make all URI properties mandatory by default to avoid complex
    // validation with ref v uri
    boolean answer =
        ("uri".equals(propertyName) || propertyName.endsWith("Uri"))
            || (bean instanceof Aggregate && "strategyRef".equals(propertyName))
            || (bean instanceof ConvertBody && "type".equals(propertyName))
            || (bean instanceof ExpressionDefinition && isMandatoryExpression())
            || (bean instanceof Log && "message".equals(propertyName))
            || (bean instanceof Process && "ref".equals(propertyName))
            || (bean instanceof RemoveHeader && "headerName".equals(propertyName))
            || (bean instanceof RemoveProperty && "propertyName".equals(propertyName))
            || (bean instanceof SetHeader && "headerName".equals(propertyName))
            || (bean instanceof SetOutHeader && "headerName".equals(propertyName))
            || (bean instanceof SetProperty && "propertyName".equals(propertyName));
    return answer;
  }
Example #9
0
  public WrapperHelper createWrapperHelper(
      Class<?> wrapperType,
      QName wrapperName,
      List<String> partNames,
      List<String> elTypeNames,
      List<Class<?>> partClasses) {
    List<Method> getMethods = new ArrayList<Method>(partNames.size());
    List<Method> setMethods = new ArrayList<Method>(partNames.size());
    List<Method> jaxbMethods = new ArrayList<Method>(partNames.size());
    List<Field> fields = new ArrayList<Field>(partNames.size());

    Method allMethods[] = wrapperType.getMethods();
    String packageName = PackageUtils.getPackageName(wrapperType);

    // if wrappertype class is generated by ASM,getPackage() always return null
    if (wrapperType.getPackage() != null) {
      packageName = wrapperType.getPackage().getName();
    }

    String objectFactoryClassName = packageName + ".ObjectFactory";

    Object objectFactory = null;
    try {
      objectFactory = wrapperType.getClassLoader().loadClass(objectFactoryClassName).newInstance();
    } catch (Exception e) {
      // ignore, probably won't need it
    }
    Method allOFMethods[];
    if (objectFactory != null) {
      allOFMethods = objectFactory.getClass().getMethods();
    } else {
      allOFMethods = new Method[0];
    }

    for (int x = 0; x < partNames.size(); x++) {
      String partName = partNames.get(x);
      if (partName == null) {
        getMethods.add(null);
        setMethods.add(null);
        fields.add(null);
        jaxbMethods.add(null);
        continue;
      }

      String elementType = elTypeNames.get(x);

      String getAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
      String setAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
      Method getMethod = null;
      Method setMethod = null;
      Class<?> valueClass = wrapperType;

      try {
        getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
      } catch (NoSuchMethodException ex) {
        // ignore for now
      }

      Field elField = getElField(partName, valueClass);
      if (getMethod == null
          && elementType != null
          && "boolean".equals(elementType.toLowerCase())
          && (elField == null
              || (!Collection.class.isAssignableFrom(elField.getType())
                  && !elField.getType().isArray()))) {

        try {
          String newAcc = getAccessor.replaceFirst("get", "is");
          getMethod = wrapperType.getMethod(newAcc, AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
          // ignore for now
        }
      }
      if (getMethod == null && "return".equals(partName)) {
        // RI generated code uses this
        try {
          getMethod = valueClass.getMethod("get_return", AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
          try {
            getMethod = valueClass.getMethod("is_return", new Class[0]);
          } catch (NoSuchMethodException ex2) {
            // ignore for now
          }
        }
      }
      if (getMethod == null && elField != null) {
        getAccessor =
            JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.GETTER);
        setAccessor =
            JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.SETTER);
        try {
          getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
        } catch (NoSuchMethodException ex) {
          // ignore for now
        }
      }
      String setAccessor2 = setAccessor;
      if ("return".equals(partName)) {
        // some versions of jaxb map "return" to "set_return" instead of "setReturn"
        setAccessor2 = "set_return";
      }

      for (Method method : allMethods) {
        if (method.getParameterTypes() != null
            && method.getParameterTypes().length == 1
            && (setAccessor.equals(method.getName()) || setAccessor2.equals(method.getName()))) {
          setMethod = method;
          break;
        }
      }

      getMethods.add(getMethod);
      setMethods.add(setMethod);
      if (setMethod != null
          && JAXBElement.class.isAssignableFrom(setMethod.getParameterTypes()[0])) {

        Type t = setMethod.getGenericParameterTypes()[0];
        Class<?> pcls = null;
        if (t instanceof ParameterizedType) {
          t = ((ParameterizedType) t).getActualTypeArguments()[0];
        }
        if (t instanceof Class) {
          pcls = (Class<?>) t;
        }

        String methodName =
            "create" + wrapperType.getSimpleName() + setMethod.getName().substring(3);

        for (Method m : allOFMethods) {
          if (m.getName().equals(methodName)
              && m.getParameterTypes().length == 1
              && (pcls == null || pcls.equals(m.getParameterTypes()[0]))) {
            jaxbMethods.add(m);
          }
        }
      } else {
        jaxbMethods.add(null);
      }

      if (elField != null) {
        // JAXB Type get XmlElement Annotation
        XmlElement el = elField.getAnnotation(XmlElement.class);
        if (el != null && (partName.equals(el.name()) || "##default".equals(el.name()))) {
          ReflectionUtil.setAccessible(elField);
          fields.add(elField);
        } else {
          if (getMethod == null && setMethod == null) {
            if (el != null) {
              LOG.warning(
                  "Could not create accessor for property "
                      + partName
                      + " of type "
                      + wrapperType.getName()
                      + " as the @XmlElement "
                      + "defines the name as "
                      + el.name());
            } else {
              LOG.warning(
                  "Could not create accessor for property "
                      + partName
                      + " of type "
                      + wrapperType.getName());
            }
          }
          fields.add(null);
        }
      } else {
        fields.add(null);
      }
    }

    return createWrapperHelper(
        wrapperType,
        setMethods.toArray(new Method[setMethods.size()]),
        getMethods.toArray(new Method[getMethods.size()]),
        jaxbMethods.toArray(new Method[jaxbMethods.size()]),
        fields.toArray(new Field[fields.size()]),
        objectFactory);
  }