Browse Source

Apply "instanceof pattern matching" in spring-oxm

This has only been applied to `src/main/java`.
pull/27562/head
Sam Brannen 3 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; @@ -111,6 +111,7 @@ import org.springframework.util.xml.StaxUtils;
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.0
* @see #setContextPath
* @see #setClassesToBeBound
@ -607,25 +608,21 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -607,25 +608,21 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
@Override
public boolean supports(Type genericType) {
if (genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
if (genericType instanceof ParameterizedType parameterizedType) {
if (JAXBElement.class == parameterizedType.getRawType() &&
parameterizedType.getActualTypeArguments().length == 1) {
Type typeArgument = parameterizedType.getActualTypeArguments()[0];
if (typeArgument instanceof Class) {
Class<?> classArgument = (Class<?>) typeArgument;
if (typeArgument instanceof Class<?> classArgument) {
return ((classArgument.isArray() && Byte.TYPE == classArgument.getComponentType()) ||
isPrimitiveWrapper(classArgument) || isStandardClass(classArgument) ||
supportsInternal(classArgument, false));
}
else if (typeArgument instanceof GenericArrayType) {
GenericArrayType arrayType = (GenericArrayType) typeArgument;
else if (typeArgument instanceof GenericArrayType arrayType) {
return (Byte.TYPE == arrayType.getGenericComponentType());
}
}
}
else if (genericType instanceof Class) {
Class<?> clazz = (Class<?>) genericType;
else if (genericType instanceof Class<?> clazz) {
return supportsInternal(clazz, this.checkForXmlRootElement);
}
return false;
@ -866,13 +863,11 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi @@ -866,13 +863,11 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
XMLReader xmlReader = null;
InputSource inputSource = null;
if (source instanceof SAXSource) {
SAXSource saxSource = (SAXSource) source;
if (source instanceof SAXSource saxSource) {
xmlReader = saxSource.getXMLReader();
inputSource = saxSource.getInputSource();
}
else if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
else if (source instanceof StreamSource streamSource) {
if (streamSource.getInputStream() != null) {
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 @@ -253,8 +253,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
*/
public void setConverterLookup(ConverterLookup converterLookup) {
this.converterLookup = converterLookup;
if (converterLookup instanceof ConverterRegistry) {
this.converterRegistry = (ConverterRegistry) converterLookup;
if (converterLookup instanceof ConverterRegistry registry) {
this.converterRegistry = registry;
}
}
@ -491,11 +491,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo @@ -491,11 +491,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
protected void configureXStream(XStream xstream) {
if (this.converters != null) {
for (int i = 0; i < this.converters.length; i++) {
if (this.converters[i] instanceof Converter) {
xstream.registerConverter((Converter) this.converters[i], i);
if (this.converters[i] instanceof Converter converter) {
xstream.registerConverter(converter, i);
}
else if (this.converters[i] instanceof SingleValueConverter) {
xstream.registerConverter((SingleValueConverter) this.converters[i], i);
else if (this.converters[i] instanceof SingleValueConverter converter) {
xstream.registerConverter(converter, i);
}
else {
throw new IllegalArgumentException("Invalid ConverterMatcher [" + this.converters[i] + "]");
@ -553,26 +553,23 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo @@ -553,26 +553,23 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
}
if (this.useAttributeFor != null) {
for (Map.Entry<?, ?> entry : this.useAttributeFor.entrySet()) {
if (entry.getKey() instanceof String) {
if (entry.getValue() instanceof Class) {
xstream.useAttributeFor((String) entry.getKey(), (Class<?>) entry.getValue());
if (entry.getKey() instanceof String key) {
if (entry.getValue() instanceof Class<?> clazz) {
xstream.useAttributeFor(key, clazz);
}
else {
throw new IllegalArgumentException(
"'useAttributesFor' takes Map<String, Class> when using a map key of type String");
}
}
else if (entry.getKey() instanceof Class) {
Class<?> key = (Class<?>) entry.getKey();
if (entry.getValue() instanceof String) {
xstream.useAttributeFor(key, (String) entry.getValue());
else if (entry.getKey() instanceof Class<?> key) {
if (entry.getValue() instanceof String value) {
xstream.useAttributeFor(key, value);
}
else if (entry.getValue() instanceof List) {
@SuppressWarnings("unchecked")
List<Object> listValue = (List<Object>) entry.getValue();
else if (entry.getValue() instanceof List<?> listValue) {
for (Object element : listValue) {
if (element instanceof String) {
xstream.useAttributeFor(key, (String) element);
if (element instanceof String value) {
xstream.useAttributeFor(key, value);
}
}
}
@ -619,11 +616,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo @@ -619,11 +616,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
String key = entry.getKey();
Object value = entry.getValue();
Class<?> type;
if (value instanceof Class) {
type = (Class<?>) value;
if (value instanceof Class<?> clazz) {
type = clazz;
}
else if (value instanceof String) {
String className = (String) value;
else if (value instanceof String className) {
type = ClassUtils.forName(className, this.beanClassLoader);
}
else {
@ -676,11 +672,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo @@ -676,11 +672,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
HierarchicalStreamWriter streamWriter;
if (node instanceof Document) {
streamWriter = new DomWriter((Document) node, this.nameCoder);
if (node instanceof Document document) {
streamWriter = new DomWriter(document, this.nameCoder);
}
else if (node instanceof Element) {
streamWriter = new DomWriter((Element) node, node.getOwnerDocument(), this.nameCoder);
else if (node instanceof Element element) {
streamWriter = new DomWriter(element, node.getOwnerDocument(), this.nameCoder);
}
else {
throw new IllegalArgumentException("DOMResult contains neither Document nor Element");
@ -691,10 +687,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo @@ -691,10 +687,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
@Override
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter);
LexicalHandler lexicalHandler = null;
if (contentHandler instanceof LexicalHandler) {
lexicalHandler = (LexicalHandler) contentHandler;
}
LexicalHandler lexicalHandler = (contentHandler instanceof LexicalHandler handler ? handler : null);
marshalSaxHandlers(graph, contentHandler, lexicalHandler);
}
@ -789,11 +782,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo @@ -789,11 +782,11 @@ public class XStreamMarshaller extends AbstractMarshaller implements BeanClassLo
@Override
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
HierarchicalStreamReader streamReader;
if (node instanceof Document) {
streamReader = new DomReader((Document) node, this.nameCoder);
if (node instanceof Document document) {
streamReader = new DomReader(document, this.nameCoder);
}
else if (node instanceof Element) {
streamReader = new DomReader((Element) node, this.nameCoder);
else if (node instanceof Element element) {
streamReader = new DomReader(element, this.nameCoder);
}
else {
throw new IllegalArgumentException("DOMSource contains neither Document nor Element");

Loading…
Cancel
Save