Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use instanceof pattern variable #1395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class SimpleFaultMessageResolver implements FaultMessageResolver {
/** Throws a new {@code WebServiceFaultException}. */
@Override
public void resolveFault(WebServiceMessage message) {
if (message instanceof FaultAwareWebServiceMessage) {
throw new WebServiceFaultException((FaultAwareWebServiceMessage) message);
if (message instanceof FaultAwareWebServiceMessage faultAwareWebServiceMessage) {
throw new WebServiceFaultException(faultAwareWebServiceMessage);
} else {
throw new WebServiceFaultException("Message has unknown fault: " + message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ public WebServiceTemplate(WebServiceMessageFactory messageFactory) {
*/
public WebServiceTemplate(Marshaller marshaller) {
Assert.notNull(marshaller, "marshaller must not be null");
if (!(marshaller instanceof Unmarshaller)) {
if (!(marshaller instanceof Unmarshaller unmarshallerInstance)) {
throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller "
+ "interface. Please set an Unmarshaller explicitly by using the "
+ "WebServiceTemplate(Marshaller, Unmarshaller) constructor.");
} else {
this.setMarshaller(marshaller);
this.setUnmarshaller((Unmarshaller) marshaller);
this.setUnmarshaller(unmarshallerInstance);
}
initDefaultStrategies();
}
Expand Down Expand Up @@ -641,9 +641,8 @@ private void sendRequest(WebServiceConnection connection, WebServiceMessage requ
protected boolean hasError(WebServiceConnection connection, WebServiceMessage request) throws IOException {
if (checkConnectionForError && connection.hasError()) {
// could be a fault
if (checkConnectionForFault && connection instanceof FaultAwareWebServiceConnection) {
FaultAwareWebServiceConnection faultConnection = (FaultAwareWebServiceConnection) connection;
return !(faultConnection.hasFault() && request instanceof FaultAwareWebServiceMessage);
if (checkConnectionForFault && connection instanceof FaultAwareWebServiceConnection faultConnection) {
return !(faultConnection.hasFault() && request instanceof FaultAwareWebServiceMessage);
} else {
return true;
}
Expand Down Expand Up @@ -699,17 +698,15 @@ private void logResponse(MessageContext messageContext) throws IOException {
* @throws IOException in case of I/O errors
*/
protected boolean hasFault(WebServiceConnection connection, WebServiceMessage response) throws IOException {
if (checkConnectionForFault && connection instanceof FaultAwareWebServiceConnection) {
if (checkConnectionForFault && connection instanceof FaultAwareWebServiceConnection faultConnection) {
// check whether the connection has a fault (i.e. status code 500 in HTTP)
FaultAwareWebServiceConnection faultConnection = (FaultAwareWebServiceConnection) connection;
if (!faultConnection.hasFault()) {
if (!faultConnection.hasFault()) {
return false;
}
}
if (response instanceof FaultAwareWebServiceMessage) {
if (response instanceof FaultAwareWebServiceMessage faultMessage) {
// either the connection has a fault, or checkConnectionForFault is false: let's verify the fault
FaultAwareWebServiceMessage faultMessage = (FaultAwareWebServiceMessage) response;
return faultMessage.hasFault();
return faultMessage.hasFault();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public final Attachment addAttachment(String contentId, File file) throws Attach
public final Attachment addAttachment(String contentId, InputStreamSource inputStreamSource, String contentType) {
Assert.hasLength(contentId, "contentId must not be empty");
Assert.notNull(inputStreamSource, "InputStreamSource must not be null");
if (inputStreamSource instanceof Resource && ((Resource) inputStreamSource).isOpen()) {
if (inputStreamSource instanceof Resource resource && resource.isOpen()) {
throw new IllegalArgumentException("Passed-in Resource contains an open stream: invalid argument. "
+ "MIME requires an InputStreamSource that creates a fresh stream for every call.");
}
Expand Down Expand Up @@ -91,9 +91,8 @@ public String getContentType() {

@Override
public String getName() {
if (inputStreamSource instanceof Resource) {
Resource resource = (Resource) inputStreamSource;
return resource.getFilename();
if (inputStreamSource instanceof Resource resource) {
return resource.getFilename();
} else {
throw new UnsupportedOperationException("DataSource name not available");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ public String toString() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
try {
if (outputStream instanceof TransportOutputStream) {
TransportOutputStream transportOutputStream = (TransportOutputStream) outputStream;
transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
if (outputStream instanceof TransportOutputStream transportOutputStream) {
transportOutputStream.addHeader(TransportConstants.HEADER_CONTENT_TYPE, contentType);
}
transformer.transform(getPayloadSource(), new StreamResult(outputStream));
} catch (TransformerException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ private void triggerHandleResponse(EndpointInvocationChain mappedEndpoint, int i
&& !ObjectUtils.isEmpty(mappedEndpoint.getInterceptors())) {
boolean hasFault = false;
WebServiceMessage response = messageContext.getResponse();
if (response instanceof FaultAwareWebServiceMessage) {
hasFault = ((FaultAwareWebServiceMessage) response).hasFault();
if (response instanceof FaultAwareWebServiceMessage faultAwareWebServiceMessage) {
hasFault = faultAwareWebServiceMessage.hasFault();
}
boolean resume = true;
for (int i = interceptorIndex; resume && i >= 0; i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ protected Element getDocumentElement(Source source) throws TransformerException
if (source == null) {
return null;
}
if (!alwaysTransform && source instanceof DOMSource) {
Node node = ((DOMSource) source).getNode();
if (!alwaysTransform && source instanceof DOMSource domSource) {
Node node = domSource.getNode();
if (node.getNodeType() == Node.DOCUMENT_NODE) {
DOMReader domReader = new DOMReader();
Document document = domReader.read((org.w3c.dom.Document) node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ protected Element getDocumentElement(Source source, DocumentBuilder documentBuil
if (source == null) {
return null;
}
if (!alwaysTransform && source instanceof DOMSource) {
Node node = ((DOMSource) source).getNode();
if (!alwaysTransform && source instanceof DOMSource domSource) {
Node node = domSource.getNode();
if (node.getNodeType() == Node.ELEMENT_NODE) {
return (Element) node;
} else if (node.getNodeType() == Node.DOCUMENT_NODE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public final int getOrder() {
*/
@Override
public final boolean resolveException(MessageContext messageContext, Object endpoint, Exception ex) {
Object mappedEndpoint = endpoint instanceof MethodEndpoint ? ((MethodEndpoint) endpoint).getBean() : endpoint;
Object mappedEndpoint = endpoint instanceof MethodEndpoint methodEndpoint ? methodEndpoint.getBean() : endpoint;
if (mappedEndpoints != null && !mappedEndpoints.contains(mappedEndpoint)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ protected Element getDocumentElement(Source source) throws TransformerException
if (source == null) {
return null;
}
if (!alwaysTransform && source instanceof DOMSource) {
Node node = ((DOMSource) source).getNode();
if (!alwaysTransform && source instanceof DOMSource domSource) {
Node node = domSource.getNode();
DOMBuilder domBuilder = new DOMBuilder();
if (node.getNodeType() == Node.ELEMENT_NODE) {
return domBuilder.build((org.w3c.dom.Element) node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ protected AbstractMarshallingPayloadEndpoint() {}
*/
protected AbstractMarshallingPayloadEndpoint(Marshaller marshaller) {
Assert.notNull(marshaller, "marshaller must not be null");
if (!(marshaller instanceof Unmarshaller)) {
if (!(marshaller instanceof Unmarshaller unmarshallerInstance)) {
throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller "
+ "interface. Please set an Unmarshaller explicitly by using the "
+ "AbstractMarshallingPayloadEndpoint(Marshaller, Unmarshaller) constructor.");
} else {
setMarshaller(marshaller);
setUnmarshaller((Unmarshaller) marshaller);
setUnmarshaller(unmarshallerInstance);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ public MethodEndpoint(String beanName, BeanFactory beanFactory, Method method) {

/** Returns the object bean for this method endpoint. */
public Object getBean() {
if (beanFactory != null && bean instanceof String) {
String beanName = (String) bean;
return beanFactory.getBean(beanName);
if (beanFactory != null && bean instanceof String beanName) {
return beanFactory.getBean(beanName);
} else {
return bean;
}
Expand Down Expand Up @@ -140,14 +139,14 @@ public Object invoke(Object... args) throws Exception {

private void handleInvocationTargetException(InvocationTargetException ex) throws Exception {
Throwable targetException = ex.getTargetException();
if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
if (targetException instanceof RuntimeException runtimeException) {
throw runtimeException;
}
if (targetException instanceof Error) {
throw (Error) targetException;
if (targetException instanceof Error error) {
throw error;
}
if (targetException instanceof Exception) {
throw (Exception) targetException;
if (targetException instanceof Exception exception) {
throw exception;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class AbstractMethodEndpointAdapter extends TransformerObjectSup
*/
@Override
public final boolean supports(Object endpoint) {
return endpoint instanceof MethodEndpoint && supportsInternal((MethodEndpoint) endpoint);
return endpoint instanceof MethodEndpoint methodEndpoint && supportsInternal(methodEndpoint);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ private boolean supportsReturnType(Method method) {
if (Void.TYPE.equals(method.getReturnType())) {
return true;
} else {
if (getMarshaller() instanceof GenericMarshaller) {
return ((GenericMarshaller) getMarshaller()).supports(method.getGenericReturnType());
if (getMarshaller() instanceof GenericMarshaller genericMarshaller) {
return genericMarshaller.supports(method.getGenericReturnType());
} else {
return getMarshaller().supports(method.getReturnType());
}
Expand All @@ -97,9 +97,8 @@ private boolean supportsReturnType(Method method) {
private boolean supportsParameters(Method method) {
if (method.getParameterTypes().length != 1) {
return false;
} else if (getUnmarshaller() instanceof GenericUnmarshaller) {
GenericUnmarshaller genericUnmarshaller = (GenericUnmarshaller) getUnmarshaller();
return genericUnmarshaller.supports(method.getGenericParameterTypes()[0]);
} else if (getUnmarshaller() instanceof GenericUnmarshaller genericUnmarshaller) {
return genericUnmarshaller.supports(method.getGenericParameterTypes()[0]);
} else {
return getUnmarshaller().supports(method.getParameterTypes()[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public MarshallingMethodEndpointAdapter() {}
*/
public MarshallingMethodEndpointAdapter(Marshaller marshaller) {
Assert.notNull(marshaller, "marshaller must not be null");
if (!(marshaller instanceof Unmarshaller)) {
if (!(marshaller instanceof Unmarshaller unmarshallerInstance)) {
throw new IllegalArgumentException("Marshaller [" + marshaller + "] does not implement the Unmarshaller "
+ "interface. Please set an Unmarshaller explicitly by using the "
+ "MarshallingMethodEndpointAdapter(Marshaller, Unmarshaller) constructor.");
} else {
this.setMarshaller(marshaller);
this.setUnmarshaller((Unmarshaller) marshaller);
this.setUnmarshaller(unmarshallerInstance);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
Unmarshaller unmarshaller = getUnmarshaller();
if (unmarshaller == null) {
return false;
} else if (unmarshaller instanceof GenericUnmarshaller) {
return ((GenericUnmarshaller) unmarshaller).supports(parameter.getGenericParameterType());
} else if (unmarshaller instanceof GenericUnmarshaller genericUnmarshaller) {
return genericUnmarshaller.supports(parameter.getGenericParameterType());
} else {
return unmarshaller.supports(parameter.getParameterType());
}
Expand All @@ -137,9 +137,8 @@ protected boolean supportsResponsePayloadReturnType(MethodParameter returnType)
Marshaller marshaller = getMarshaller();
if (marshaller == null) {
return false;
} else if (marshaller instanceof GenericMarshaller) {
GenericMarshaller genericMarshaller = (GenericMarshaller) marshaller;
return genericMarshaller.supports(returnType.getGenericParameterType());
} else if (marshaller instanceof GenericMarshaller genericMarshaller) {
return genericMarshaller.supports(returnType.getGenericParameterType());
} else {
return marshaller.supports(returnType.getParameterType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
@Override
protected Element resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload)
throws TransformerException {
if (requestPayload instanceof DOMSource) {
org.w3c.dom.Node node = ((DOMSource) requestPayload).getNode();
if (requestPayload instanceof DOMSource domSource) {
org.w3c.dom.Node node = domSource.getNode();
if (node.getNodeType() == org.w3c.dom.Node.DOCUMENT_NODE) {
DOMReader domReader = new DOMReader();
Document document = domReader.read((org.w3c.dom.Document) node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {

@Override
protected Node resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload) throws Exception {
if (requestPayload instanceof DOMSource) {
return resolveArgumentDomSource(parameter, (DOMSource) requestPayload);
if (requestPayload instanceof DOMSource domSource) {
return resolveArgumentDomSource(parameter, domSource);
} else {
DOMResult domResult = new DOMResult();
transform(requestPayload, domResult);
Expand All @@ -60,9 +60,8 @@ private Node resolveArgumentDomSource(MethodParameter parameter, DOMSource reque
Node requestNode = requestSource.getNode();
if (parameterType.isAssignableFrom(requestNode.getClass())) {
return requestNode;
} else if (Element.class.equals(parameterType) && requestNode instanceof Document) {
Document document = (Document) requestNode;
return document.getDocumentElement();
} else if (Element.class.equals(parameterType) && requestNode instanceof Document document) {
return document.getDocumentElement();
}
// should not happen
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {

@Override
protected Element resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload) throws Exception {
if (requestPayload instanceof DOMSource) {
Node node = ((DOMSource) requestPayload).getNode();
if (requestPayload instanceof DOMSource domSource) {
Node node = domSource.getNode();
DOMBuilder domBuilder = new DOMBuilder();
if (node.getNodeType() == Node.ELEMENT_NODE) {
return domBuilder.build((org.w3c.dom.Element) node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ protected boolean supportsRequestPayloadParameter(MethodParameter parameter) {
@Override
protected Element resolveRequestPayloadArgument(MethodParameter parameter, Source requestPayload)
throws TransformerException, IOException, ParsingException {
if (requestPayload instanceof DOMSource) {
org.w3c.dom.Node node = ((DOMSource) requestPayload).getNode();
if (requestPayload instanceof DOMSource domSource) {
org.w3c.dom.Node node = domSource.getNode();
if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
return DOMConverter.convert((org.w3c.dom.Element) node);
} else if (node.getNodeType() == org.w3c.dom.Node.DOCUMENT_NODE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ protected final void marshalToResponsePayload(MessageContext messageContext, Cla
logger.debug("Marshalling [" + jaxbElement + "] to response payload");
}
WebServiceMessage response = messageContext.getResponse();
if (response instanceof StreamingWebServiceMessage) {
StreamingWebServiceMessage streamingResponse = (StreamingWebServiceMessage) response;

StreamingPayload payload = new JaxbStreamingPayload(clazz, jaxbElement);
if (response instanceof StreamingWebServiceMessage streamingResponse) {
StreamingPayload payload = new JaxbStreamingPayload(clazz, jaxbElement);
streamingResponse.setStreamingPayload(payload);
} else {
Result responsePayload = response.getPayloadResult();
Expand Down Expand Up @@ -176,8 +174,8 @@ private Source getRequestPayload(MessageContext messageContext) {
}

private JAXBException convertToJaxbException(Exception ex) {
if (ex instanceof JAXBException) {
return (JAXBException) ex;
if (ex instanceof JAXBException jaxbException) {
return jaxbException;
} else {
return new JAXBException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ public final EndpointInvocationChain getEndpoint(MessageContext messageContext)
if (endpoint == null) {
return null;
}
if (endpoint instanceof String) {
String endpointName = (String) endpoint;
endpoint = resolveStringEndpoint(endpointName);
if (endpoint instanceof String endpointName) {
endpoint = resolveStringEndpoint(endpointName);
if (endpoint == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public final void setEndpointMap(Map<String, Object> endpointMap) {
*/
public void setMappings(Properties mappings) {
for (Map.Entry<Object, Object> entry : mappings.entrySet()) {
if (entry.getKey() instanceof String) {
temporaryEndpointMap.put((String) entry.getKey(), entry.getValue());
if (entry.getKey() instanceof String key) {
temporaryEndpointMap.put(key, entry.getValue());
}
}
}
Expand Down Expand Up @@ -139,9 +139,8 @@ protected void registerEndpoint(String key, Object endpoint) throws BeansExcepti
throw new ApplicationContextException("Cannot map endpoint [" + endpoint + "] on registration key [" + key
+ "]: there's already endpoint [" + mappedEndpoint + "] mapped");
}
if (!lazyInitEndpoints && endpoint instanceof String) {
String endpointName = (String) endpoint;
endpoint = resolveStringEndpoint(endpointName);
if (!lazyInitEndpoints && endpoint instanceof String endpointName) {
endpoint = resolveStringEndpoint(endpointName);
}
if (endpoint == null) {
throw new ApplicationContextException("Could not find endpoint for key [" + key + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ protected void registerEndpoint(URI action, Object endpoint) throws BeansExcepti
Assert.notNull(endpoint, "Endpoint object must not be null");
Object resolvedEndpoint = endpoint;

if (endpoint instanceof String) {
String endpointName = (String) endpoint;
if (getApplicationContext().isSingleton(endpointName)) {
if (endpoint instanceof String endpointName) {
if (getApplicationContext().isSingleton(endpointName)) {
resolvedEndpoint = getApplicationContext().getBean(endpointName);
}
}
Expand Down
Loading