Skip to content

Jaxb2Marshaller.supports(Class clazz) is too eager [SWS-171] #325

Closed
@gregturn

Description

@gregturn

Martin Vanek opened SWS-171 and commented

When using multiple Jaxb2Marshallers for multiple endpoints, first encountered Jaxb2Marshaller is always used, because it's "support" method accepts every Jaxb class. My proposition for improvement is:

public boolean supports(Class clazz) {
	boolean isJaxbClass = clazz.getAnnotation(XmlRootElement.class) != null
			|| JAXBElement.class.isAssignableFrom(clazz);
	if (isJaxbClass) {
		if (StringUtils.hasLength(getContextPath())) {
			String pckgName = clazz.getPackage().getName();
			StringTokenizer st = new StringTokenizer(getContextPath(), ":");
			while (st.hasMoreTokens()) {
				if (st.nextToken().equals(pckgName)) {
					return true;
				}
			}
		} else if (!ObjectUtils.isEmpty(classesToBeBound)) {
			for (Class clazzToBeBo : classesToBeBound) {
				if (clazzToBeBo.equals(clazz)) {
					return true;
				}
			}
		} else {
			throw new IllegalArgumentException(
					"setting either contextPath or classesToBeBound is required");
		}
	}
	return false;
}

Affects: 1.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions