public static String getRootElementNamespace(Class<?> kls) { String namespace = null; if (kls == null) { return null; } XmlRootElement re = kls.getAnnotation(XmlRootElement.class); if (re != null) { namespace = re.namespace(); } return namespace; }
protected QName getRootElementName(Object object) { boolean xmlRootElemAnnoFound = false; Class<?> objClass = object.getClass(); // This usually doesn't work in the kie-wb/bpms environment, see comment below XmlRootElement xmlRootElemAnno = objClass.getAnnotation(XmlRootElement.class); logger.debug("Getting XML root element annotation for " + object.getClass().getName()); if (xmlRootElemAnno != null) { xmlRootElemAnnoFound = true; return new QName(xmlRootElemAnno.name()); } else { /** * There seem to be weird classpath issues going on here, probably related to the fact that * kjar's have their own classloader.. (The XmlRootElement class can't be found in the same * classpath as the class from the Kjar) */ for (Annotation anno : objClass.getAnnotations()) { Class<?> annoClass = anno.annotationType(); // we deliberately compare *names* and not classes because it's on a different classpath! if (XmlRootElement.class.getName().equals(annoClass.getName())) { xmlRootElemAnnoFound = true; try { Method nameMethod = annoClass.getMethod("name"); Object nameVal = nameMethod.invoke(anno); if (nameVal instanceof String) { return new QName((String) nameVal); } } catch (Exception e) { throw RestOperationException.internalServerError( "Unable to retrieve XmlRootElement info via reflection", e); } } } if (!xmlRootElemAnnoFound) { String errorMsg = "Unable to serialize " + object.getClass().getName() + " instance " + "because it is missing a " + XmlRootElement.class.getName() + " annotation with a name value."; throw RestOperationException.internalServerError(errorMsg); } return null; } }
public static String getRootElementName(Class<?> kls) { String name; if (kls == null) { return null; } XmlRootElement re = kls.getAnnotation(XmlRootElement.class); if (re != null) { name = re.name(); } else { name = kls.getName(); int lastDot = name.lastIndexOf('.'); if (lastDot >= 0) { name = name.substring(lastDot + 1); } } return name; }
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()); } }