private void walkReferences(Class<?> cls) {
    if (cls == null) {
      return;
    }
    if (cls.getName().startsWith("java.") || cls.getName().startsWith("javax.")) {
      return;
    }
    // walk the public fields/methods to try and find all the classes. JAXB will only load the
    // EXACT classes in the fields/methods if they are in a different package. Thus,
    // subclasses won't be found and the xsi:type stuff won't work at all.
    // We'll grab the public field/method types and then add the ObjectFactory stuff
    // as well as look for jaxb.index files in those packages.

    XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
    if (accessorType == null && cls.getPackage() != null) {
      accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
    }
    XmlAccessType accessType =
        accessorType != null ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;

    if (accessType != XmlAccessType.PROPERTY) { // only look for fields if we are instructed to
      // fields are accessible even if not public, must look at the declared fields
      // then walk to parents declared fields, etc...
      Field fields[] = ReflectionUtil.getDeclaredFields(cls);
      for (Field f : fields) {
        if (isFieldAccepted(f, accessType)) {
          addType(f.getGenericType());
        }
      }
      walkReferences(cls.getSuperclass());
    }

    if (accessType != XmlAccessType.FIELD) { // only look for methods if we are instructed to
      Method methods[] = ReflectionUtil.getDeclaredMethods(cls);
      for (Method m : methods) {
        if (isMethodAccepted(m, accessType)) {
          addType(m.getGenericReturnType());
          for (Type t : m.getGenericParameterTypes()) {
            addType(t);
          }
        }
      }
    }
  }
Example #2
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 #3
0
  private void gatherInfo() {
    XmlAccessorType accessorType;
    rootElementName = null;
    XmlAccessType accessType = null;

    XmlRootElement rootE = (XmlRootElement) jaxbClass.getAnnotation(XmlRootElement.class);
    if (rootE != null) {
      rootElementName = rootE.name();
    }
    xmlType = (XmlType) jaxbClass.getAnnotation(XmlType.class);

    accessorType = (XmlAccessorType) jaxbClass.getAnnotation(XmlAccessorType.class);
    if (accessorType == null) {
      Package pkg = jaxbClass.getPackage();
      accessorType = (XmlAccessorType) pkg.getAnnotation(XmlAccessorType.class);
    }
    if (accessorType != null) {
      accessType = accessorType.value();
    }
    if (accessType == null) {
      // Default value for JAXB
      accessType = XmlAccessType.PUBLIC_MEMBER;
    }

    Field fields[] = jaxbClass.getDeclaredFields();
    for (Field field : fields) {
      XmlTransient xmlTransient = (XmlTransient) field.getAnnotation(XmlTransient.class);
      if (xmlTransient != null) {
        continue;
      }
      Annotation fAnnots[] = field.getAnnotations();
      if ((fAnnots == null) || (fAnnots.length == 0)) {
        boolean autoFields =
            (accessType.equals(XmlAccessType.PUBLIC_MEMBER)
                || accessType.equals(XmlAccessType.FIELD));
        if (!autoFields) {
          continue;
        }
      }
      processFieldRelatedAnnotations(fAnnots, field.getName(), field.getGenericType());
    }

    Method methods[] = jaxbClass.getDeclaredMethods();
    for (Method method : methods) {
      XmlTransient xmlTransient = (XmlTransient) method.getAnnotation(XmlTransient.class);
      if (xmlTransient != null) {
        continue;
      }
      if (!isGetterOrSetter(method)) {
        continue;
      }
      Annotation mAnnots[] = method.getAnnotations();
      if ((mAnnots == null) || (mAnnots.length == 0)) {
        boolean autoGettersSetters =
            (accessType.equals(XmlAccessType.PUBLIC_MEMBER)
                || accessType.equals(XmlAccessType.PROPERTY));
        if (!autoGettersSetters) {
          continue;
        }
      }
      processFieldRelatedAnnotations(
          mAnnots,
          guessFieldNameFromGetterOrSetter(method.getName()),
          method.getGenericReturnType());
    }
  }