Browse Source

Apply "instanceof pattern matching" in spring-oxm

This has only been applied to `src/main/java`.
pull/27562/head
Sam Brannen 4 years ago
parent
commit
9b4f3880b3
  1. 19
      spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java
  2. 61
      spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java

19
spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

@ -111,6 +111,7 @@ import org.springframework.util.xml.StaxUtils;
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.0 * @since 3.0
* @see #setContextPath * @see #setContextPath
* @see #setClassesToBeBound * @see #setClassesToBeBound
@ -607,25 +608,21 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
@Override @Override
public boolean supports(Type genericType) { public boolean supports(Type genericType) {
if (genericType instanceof ParameterizedType) { if (genericType instanceof ParameterizedType parameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
if (JAXBElement.class == parameterizedType.getRawType() && if (JAXBElement.class == parameterizedType.getRawType() &&
parameterizedType.getActualTypeArguments().length == 1) { parameterizedType.getActualTypeArguments().length == 1) {
Type typeArgument = parameterizedType.getActualTypeArguments()[0]; Type typeArgument = parameterizedType.getActualTypeArguments()[0];
if (typeArgument instanceof Class) { if (typeArgument instanceof Class<?> classArgument) {
Class<?> classArgument = (Class<?>) typeArgument;
return ((classArgument.isArray() && Byte.TYPE == classArgument.getComponentType()) || return ((classArgument.isArray() && Byte.TYPE == classArgument.getComponentType()) ||
isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) || isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) ||
supportsInternal(classArgument, false)); supportsInternal(classArgument, false));
} }
else if (typeArgument instanceof GenericArrayType) { else if (typeArgument instanceof GenericArrayType arrayType) {
GenericArrayType arrayType = (GenericArrayType) typeArgument;
return (Byte.TYPE == arrayType.getGenericComponentType()); return (Byte.TYPE == arrayType.getGenericComponentType());
} }
} }
} }
else if (genericType instanceof Class) { else if (genericType instanceof Class<?> clazz) {
Class<?> clazz = (Class<?>) genericType;
return supportsInternal(clazz, this.checkForXmlRootElement); return supportsInternal(clazz, this.checkForXmlRootElement);
} }
return false; return false;
@ -866,13 +863,11 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
XMLReader xmlReader = null; XMLReader xmlReader = null;
InputSource inputSource = null; InputSource inputSource = null;
if (source instanceof SAXSource) { if (source instanceof SAXSource saxSource) {
SAXSource saxSource = (SAXSource) source;
xmlReader = saxSource.getXMLReader(); xmlReader = saxSource.getXMLReader();
inputSource = saxSource.getInputSource(); inputSource = saxSource.getInputSource();
} }
else if (source instanceof StreamSource) { else if (source instanceof StreamSource streamSource) {
StreamSource streamSource = (StreamSource) source;
if (streamSource.getInputStream() != null) { if (streamSource.getInputStream() != null) {
inputSource = new InputSource(streamSource.getInputStream()); inputSource = new InputSource(streamSource.getInputStream());
} }

61
spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java

@ -253,8 +253,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
*/ */
public void setConverterLookup(ConverterLookup converterLookup) { public void setConverterLookup(ConverterLookup converterLookup) {
this.converterLookup = converterLookup; this.converterLookup = converterLookup;
if (converterLookup instanceof ConverterRegistry) { if (converterLookup instanceof ConverterRegistry registry) {
this.converterRegistry = (ConverterRegistry) converterLookup; this.converterRegistry = registry;
} }
} }
@ -491,11 +491,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
protected void configureXStream(XStream xstream) { protected void configureXStream(XStream xstream) {
if (this.converters != null) { if (this.converters != null) {
for (int i = 0; i < this.converters.length; i++) { for (int i = 0; i < this.converters.length; i++) {
if (this.converters[i] instanceof Converter) { if (this.converters[i] instanceof Converter converter) {
xstream.registerConverter((Converter) this.converters[i], i); xstream.registerConverter(converter, i);
} }
else if (this.converters[i] instanceof SingleValueConverter) { else if (this.converters[i] instanceof SingleValueConverter converter) {
xstream.registerConverter((SingleValueConverter) this.converters[i], i); xstream.registerConverter(converter, i);
} }
else { else {
throw new IllegalArgumentException("Invalid ConverterMatcher [" + this.converters[i] + "]"); throw new IllegalArgumentException("Invalid ConverterMatcher [" + this.converters[i] + "]");
@ -553,26 +553,23 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
} }
if (this.useAttributeFor != null) { if (this.useAttributeFor != null) {
for (Map.Entry<?, ?> entry : this.useAttributeFor.entrySet()) { for (Map.Entry<?, ?> entry : this.useAttributeFor.entrySet()) {
if (entry.getKey() instanceof String) { if (entry.getKey() instanceof String key) {
if (entry.getValue() instanceof Class) { if (entry.getValue() instanceof Class<?> clazz) {
xstream.useAttributeFor((String) entry.getKey(), (Class<?>) entry.getValue()); xstream.useAttributeFor(key, clazz);
} }
else { else {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"'useAttributesFor' takes Map<String, Class> when using a map key of type String"); "'useAttributesFor' takes Map<String, Class> when using a map key of type String");
} }
} }
else if (entry.getKey() instanceof Class) { else if (entry.getKey() instanceof Class<?> key) {
Class<?> key = (Class<?>) entry.getKey(); if (entry.getValue() instanceof String value) {
if (entry.getValue() instanceof String) { xstream.useAttributeFor(key, value);
xstream.useAttributeFor(key, (String) entry.getValue());
} }
else if (entry.getValue() instanceof List) { else if (entry.getValue() instanceof List<?> listValue) {
@SuppressWarnings("unchecked")
List<Object> listValue = (List<Object>) entry.getValue();
for (Object element : listValue) { for (Object element : listValue) {
if (element instanceof String) { if (element instanceof String value) {
xstream.useAttributeFor(key, (String) element); xstream.useAttributeFor(key, value);
} }
} }
} }
@ -619,11 +616,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
String key = entry.getKey(); String key = entry.getKey();
Object value = entry.getValue(); Object value = entry.getValue();
Class<?> type; Class<?> type;
if (value instanceof Class) { if (value instanceof Class<?> clazz) {
type = (Class<?>) value; type = clazz;
} }
else if (value instanceof String) { else if (value instanceof String className) {
String className = (String) value;
type = ClassUtils.forName(className, this.beanClassLoader); type = ClassUtils.forName(className, this.beanClassLoader);
} }
else { else {
@ -676,11 +672,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
@Override @Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException { protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
HierarchicalStreamWriter streamWriter; HierarchicalStreamWriter streamWriter;
if (node instanceof Document) { if (node instanceof Document document) {
streamWriter = new DomWriter((Document) node, this.nameCoder); streamWriter = new DomWriter(document, this.nameCoder);
} }
else if (node instanceof Element) { else if (node instanceof Element element) {
streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), this.nameCoder); streamWriter = new DomWriter(element, node.getOwnerDocument(), this.nameCoder);
} }
else { else {
throw new IllegalArgumentException("DOMResult contains neither Document nor Element"); throw new IllegalArgumentException("DOMResult contains neither Document nor Element");
@ -691,10 +687,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
@Override @Override
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException { protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter); ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter);
LexicalHandler lexicalHandler = null; LexicalHandler lexicalHandler = (contentHandler instanceof LexicalHandler handler ? handler : null);
if (contentHandler instanceof LexicalHandler) {
lexicalHandler = (LexicalHandler) contentHandler;
}
marshalSaxHandlers(graph, contentHandler, lexicalHandler); marshalSaxHandlers(graph, contentHandler, lexicalHandler);
} }
@ -789,11 +782,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
@Override @Override
protected Object unmarshalDomNode(Node node) throws XmlMappingException { protected Object unmarshalDomNode(Node node) throws XmlMappingException {
HierarchicalStreamReader streamReader; HierarchicalStreamReader streamReader;
if (node instanceof Document) { if (node instanceof Document document) {
streamReader = new DomReader((Document) node, this.nameCoder); streamReader = new DomReader(document, this.nameCoder);
} }
else if (node instanceof Element) { else if (node instanceof Element element) {
streamReader = new DomReader((Element) node, this.nameCoder); streamReader = new DomReader(element, this.nameCoder);
} }
else { else {
throw new IllegalArgumentException("DOMSource contains neither Document nor Element"); throw new IllegalArgumentException("DOMSource contains neither Document nor Element");

Loading…
Cancel
Save