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.

    XmlAccessType accessType = Utils.getXmlAccessType(cls);

    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)) {
          XmlJavaTypeAdapter xjta = Utils.getFieldXJTA(f);
          if (xjta != null) {
            Type t = Utils.getTypeFromXmlAdapter(xjta);
            if (t != null) {
              addType(t);
              continue;
            }
          }
          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)) {
          XmlJavaTypeAdapter xjta = Utils.getMethodXJTA(m);
          if (xjta != null) {
            Type t = Utils.getTypeFromXmlAdapter(xjta);
            if (t != null) {
              addType(t);
              continue;
            }
          }
          addType(m.getGenericReturnType());
          for (Type t : m.getGenericParameterTypes()) {
            addType(t);
          }
        }
      }
    }
  }
Exemple #2
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;
 }