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; }
private void checkForJAXBAnnotations( MessagePartInfo mpi, SchemaCollection schemaCollection, String ns) { Annotation[] anns = (Annotation[]) mpi.getProperty("parameter.annotations"); JAXBContextProxy ctx = JAXBUtils.createJAXBContextProxy(context, schemaCollection, ns); XmlJavaTypeAdapter jta = JAXBSchemaInitializer.findFromTypeAdapter(ctx, mpi.getTypeClass(), anns); if (jta != null) { JAXBBeanInfo jtaBeanInfo = JAXBSchemaInitializer.findFromTypeAdapter(ctx, jta.value()); JAXBBeanInfo beanInfo = JAXBSchemaInitializer.getBeanInfo(ctx, mpi.getTypeClass()); if (jtaBeanInfo != beanInfo) { mpi.setProperty("parameter.annotations", anns); mpi.setProperty("honor.jaxb.annotations", Boolean.TRUE); } } }
// TODO : consider moving JAXBDataBinding.createContext to JAXBUtils public static JAXBContext createJaxbContext( Set<Class<?>> classes, Class<?>[] extraClass, Map<String, Object> contextProperties) { if (classes == null || classes.isEmpty()) { return null; } JAXBUtils.scanPackages(classes, extraClass, null); JAXBContext ctx; try { ctx = JAXBContext.newInstance(classes.toArray(new Class[classes.size()]), contextProperties); return ctx; } catch (JAXBException ex) { LOG.log(Level.WARNING, "No JAXB context can be created", ex); } return null; }
private void addClass(Class<?> cls) { if (Throwable.class.isAssignableFrom(cls)) { if (!Throwable.class.equals(cls) && !Exception.class.equals(cls)) { walkReferences(cls); } addClass(String.class); } else { cls = JAXBUtils.getValidClass(cls); if (null != cls) { if (classes.contains(cls)) { return; } if (!cls.isInterface()) { classes.add(cls); } XmlSeeAlso xsa = cls.getAnnotation(XmlSeeAlso.class); if (xsa != null) { for (Class<?> c : xsa.value()) { addClass(c); } } XmlJavaTypeAdapter xjta = cls.getAnnotation(XmlJavaTypeAdapter.class); if (xjta != null) { // has an adapter. We need to inspect the adapter and then // return as the adapter will handle the superclass // and interfaces and such Type t = getTypeFromXmlAdapter(xjta); if (t != null) { addType(t); } return; } if (cls.getSuperclass() != null) { // JAXB should do this, but it doesn't always. // in particular, older versions of jaxb don't addClass(cls.getSuperclass()); } if (!cls.isInterface()) { walkReferences(cls); } } } }
void addClass(Class<?> claz) { if (Throwable.class.isAssignableFrom(claz)) { if (!Throwable.class.equals(claz) && !Exception.class.equals(claz)) { walkReferences(claz); } addClass(String.class); } else if (claz.getName().startsWith("java.") || claz.getName().startsWith("javax.")) { return; } else { Class<?> cls = JAXBUtils.getValidClass(claz); if (cls == null && ReflectionUtil.getDeclaredConstructors(claz).length > 0 && !Modifier.isAbstract(claz.getModifiers())) { if (LOG.isLoggable(Level.INFO)) { LOG.info( "Class " + claz.getName() + " does not have a default constructor which JAXB requires."); } // there is no init(), but other constructors Object factory = createFactory(claz, ReflectionUtil.getDeclaredConstructors(claz)[0]); unmarshallerProperties.put("com.sun.xml.bind.ObjectFactory", factory); cls = claz; } if (null != cls) { if (classes.contains(cls)) { return; } if (!cls.isInterface()) { classes.add(cls); } XmlSeeAlso xsa = cls.getAnnotation(XmlSeeAlso.class); if (xsa != null) { for (Class<?> c : xsa.value()) { addClass(c); } } XmlJavaTypeAdapter xjta = cls.getAnnotation(XmlJavaTypeAdapter.class); if (xjta != null) { // has an adapter. We need to inspect the adapter and then // return as the adapter will handle the superclass // and interfaces and such Type t = Utils.getTypeFromXmlAdapter(xjta); if (t != null) { addType(t); } return; } if (cls.getSuperclass() != null) { // JAXB should do this, but it doesn't always. // in particular, older versions of jaxb don't addClass(cls.getSuperclass()); } if (!cls.isInterface()) { walkReferences(cls); } } } }
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); }
// default access for tests. List<DOMResult> generateJaxbSchemas() throws IOException { return JAXBUtils.generateJaxbSchemas(context, BUILT_IN_SCHEMAS); }