示例#1
0
  // Inherited.
  public String getName() {
    String propertyName = getSimpleName().toString();

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

    return propertyName;
  }
 @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);
 }
示例#3
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;
 }
示例#4
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);
  }