This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new e41f3ef2700 (chores) convert core/camel-core-processor to use pattern
matching for instanceof
e41f3ef2700 is described below
commit e41f3ef27008d40c44edede8825bfeb1833ccdaa
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Aug 21 10:25:52 2024 +0200
(chores) convert core/camel-core-processor to use pattern matching for
instanceof
---
.../camel/processor/DefaultProcessorFactory.java | 7 +++---
.../apache/camel/processor/MulticastProcessor.java | 25 +++++++++++-----------
.../org/apache/camel/processor/PollEnricher.java | 8 +++----
.../apache/camel/processor/ProcessorHelper.java | 15 ++++++-------
.../camel/processor/RecipientListProcessor.java | 8 +++----
.../org/apache/camel/processor/RoutingSlip.java | 4 ++--
.../camel/processor/SendDynamicProcessor.java | 16 +++++++-------
.../java/org/apache/camel/processor/Splitter.java | 8 +++----
.../apache/camel/processor/ThreadsProcessor.java | 3 +--
.../apache/camel/processor/WireTapProcessor.java | 5 ++---
.../processor/resume/ResumableCompletion.java | 8 ++-----
.../processor/validator/ProcessorValidator.java | 4 ++--
12 files changed, 51 insertions(+), 60 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/DefaultProcessorFactory.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/DefaultProcessorFactory.java
index e97eaa5f34e..29f9da45028 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/DefaultProcessorFactory.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/DefaultProcessorFactory.java
@@ -54,8 +54,8 @@ public class DefaultProcessorFactory implements
ProcessorFactory, BootstrapClose
@Override
public void close() throws IOException {
- if (finder instanceof BootstrapCloseable) {
- ((BootstrapCloseable) finder).close();
+ if (finder instanceof BootstrapCloseable bootstrapCloseable) {
+ bootstrapCloseable.close();
finder = null;
}
}
@@ -69,8 +69,7 @@ public class DefaultProcessorFactory implements
ProcessorFactory, BootstrapClose
}
try {
Object object = finder.newInstance(name).orElse(null);
- if (object instanceof ProcessorFactory) {
- ProcessorFactory pc = (ProcessorFactory) object;
+ if (object instanceof ProcessorFactory pc) {
Processor processor = pc.createChildProcessor(route,
definition, mandatory);
LineNumberAware.trySetLineNumberAware(processor, definition);
return processor;
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
index 6bc97a553c9..29dfa26857b 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
@@ -120,8 +120,8 @@ public class MulticastProcessor extends
AsyncProcessorSupport
@Override
public Producer getProducer() {
- if (processor instanceof Producer) {
- return (Producer) processor;
+ if (processor instanceof Producer producer) {
+ return producer;
}
return null;
}
@@ -755,8 +755,8 @@ public class MulticastProcessor extends
AsyncProcessorSupport
}
protected ScheduledFuture<?> schedule(Executor executor, Runnable
runnable, long delay, TimeUnit unit) {
- if (executor instanceof ScheduledExecutorService) {
- return ((ScheduledExecutorService) executor).schedule(runnable,
delay, unit);
+ if (executor instanceof ScheduledExecutorService
scheduledExecutorService) {
+ return scheduledExecutorService.schedule(runnable, delay, unit);
} else {
executor.execute(() -> {
try {
@@ -872,8 +872,8 @@ public class MulticastProcessor extends
AsyncProcessorSupport
}
}
// we are done so close the pairs iterator
- if (pairs instanceof Closeable) {
- IOHelper.close((Closeable) pairs, "pairs", LOG);
+ if (pairs instanceof Closeable closeable) {
+ IOHelper.close(closeable, "pairs", LOG);
}
// .. and then if there was an exception we need to configure the
redelivery exhaust
@@ -963,9 +963,9 @@ public class MulticastProcessor extends
AsyncProcessorSupport
Map<String, Object> txData = null;
StreamCache streamCache = null;
- if (isParallelProcessing() && exchange.getIn().getBody() instanceof
StreamCache) {
- // in parallel processing case, the stream must be copied,
therefore get the stream
- streamCache = (StreamCache) exchange.getIn().getBody();
+ if (isParallelProcessing() && exchange.getIn().getBody() instanceof
StreamCache streamCacheBody) {
+ // in parallel processing case, the stream must be copied,
therefore, get the stream
+ streamCache = streamCacheBody;
}
int index = 0;
@@ -1108,8 +1108,8 @@ public class MulticastProcessor extends
AsyncProcessorSupport
private Processor wrapInErrorHandler(Route route, Processor processor)
throws Exception {
// use the error handler from multicast and clone it to use the new
processor as its output
- if (errorHandler instanceof ErrorHandlerSupport) {
- return ((ErrorHandlerSupport) errorHandler).clone(processor);
+ if (errorHandler instanceof ErrorHandlerSupport errorHandlerSupport) {
+ return errorHandlerSupport.clone(processor);
}
// fallback and use reifier to create the error handler
return
camelContext.getCamelContextExtension().createErrorHandler(route, processor);
@@ -1201,8 +1201,7 @@ public class MulticastProcessor extends
AsyncProcessorSupport
}
protected static void setToEndpoint(Exchange exchange, Processor
processor) {
- if (processor instanceof Producer) {
- Producer producer = (Producer) processor;
+ if (processor instanceof Producer producer) {
exchange.setProperty(ExchangePropertyKey.TO_ENDPOINT,
producer.getEndpoint().getEndpointUri());
}
}
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
index 2e88412dc6e..a5de92aa02e 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
@@ -391,14 +391,14 @@ public class PollEnricher extends AsyncProcessorSupport
implements IdAware, Rout
private static boolean isBridgeErrorHandler(PollingConsumer consumer) {
Consumer delegate = consumer;
- if (consumer instanceof EventDrivenPollingConsumer) {
- delegate = ((EventDrivenPollingConsumer)
consumer).getDelegateConsumer();
+ if (consumer instanceof EventDrivenPollingConsumer
eventDrivenPollingConsumer) {
+ delegate = eventDrivenPollingConsumer.getDelegateConsumer();
}
// is the consumer bridging the error handler?
boolean bridgeErrorHandler = false;
- if (delegate instanceof DefaultConsumer) {
- ExceptionHandler handler = ((DefaultConsumer)
delegate).getExceptionHandler();
+ if (delegate instanceof DefaultConsumer defaultConsumer) {
+ ExceptionHandler handler = defaultConsumer.getExceptionHandler();
if (handler instanceof BridgeExceptionHandlerToErrorHandler) {
bridgeErrorHandler = true;
}
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java
index 64c270ca3dc..c7498df3a45 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java
@@ -32,15 +32,15 @@ final class ProcessorHelper {
static Object prepareRecipient(Exchange exchange, Object recipient) throws
NoTypeConversionAvailableException {
if (recipient instanceof Endpoint || recipient instanceof
NormalizedEndpointUri) {
return recipient;
- } else if (recipient instanceof String) {
+ } else if (recipient instanceof String string) {
// trim strings as end users might have added spaces between
separators
- recipient = ((String) recipient).trim();
+ recipient = string.trim();
}
if (recipient != null) {
CamelContext ecc = exchange.getContext();
String uri;
- if (recipient instanceof String) {
- uri = (String) recipient;
+ if (recipient instanceof String string) {
+ uri = string;
} else {
// convert to a string type we can work with
uri = ecc.getTypeConverter().mandatoryConvertTo(String.class,
exchange, recipient);
@@ -56,12 +56,11 @@ final class ProcessorHelper {
}
static Endpoint getExistingEndpoint(CamelContext context, Object
recipient) {
- if (recipient instanceof Endpoint) {
- return (Endpoint) recipient;
+ if (recipient instanceof Endpoint endpoint) {
+ return endpoint;
}
if (recipient != null) {
- if (recipient instanceof NormalizedEndpointUri) {
- NormalizedEndpointUri nu = (NormalizedEndpointUri) recipient;
+ if (recipient instanceof NormalizedEndpointUri nu) {
ExtendedCamelContext ecc = context.getCamelContextExtension();
return ecc.hasEndpoint(nu);
} else {
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java
index d28cf236064..a67cee87781 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java
@@ -340,11 +340,11 @@ public class RecipientListProcessor extends
MulticastProcessor {
protected ExchangePattern resolveExchangePattern(Object recipient) {
String s = null;
- if (recipient instanceof NormalizedEndpointUri) {
- s = ((NormalizedEndpointUri) recipient).getUri();
- } else if (recipient instanceof String) {
+ if (recipient instanceof NormalizedEndpointUri normalizedEndpointUri) {
+ s = normalizedEndpointUri.getUri();
+ } else if (recipient instanceof String str) {
// trim strings as end users might have added spaces between
separators
- s = ((String) recipient).trim();
+ s = str.trim();
}
if (s != null) {
return EndpointHelper.resolveExchangePatternFromUrl(s);
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
index 5e6c97a75c9..4a89ed7226a 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java
@@ -185,8 +185,8 @@ public class RoutingSlip extends AsyncProcessorSupport
implements Traceable, IdA
Expression exp = expression;
Object slip =
exchange.removeProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT);
if (slip != null) {
- if (slip instanceof Expression) {
- exp = (Expression) slip;
+ if (slip instanceof Expression expression) {
+ exp = expression;
} else {
exp = ExpressionBuilder.constantExpression(slip);
}
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
index 54fca416cec..349cad5f5e6 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
@@ -255,10 +255,10 @@ public class SendDynamicProcessor extends
AsyncProcessorSupport implements IdAwa
String uri;
// trim strings as end users might have added spaces between separators
- if (recipient instanceof String) {
- uri = ((String) recipient).trim();
- } else if (recipient instanceof Endpoint) {
- uri = ((Endpoint) recipient).getEndpointKey();
+ if (recipient instanceof String string) {
+ uri = string.trim();
+ } else if (recipient instanceof Endpoint endpoint) {
+ uri = endpoint.getEndpointKey();
} else {
// convert to a string type we can work with
uri =
exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class,
exchange, recipient);
@@ -281,15 +281,15 @@ public class SendDynamicProcessor extends
AsyncProcessorSupport implements IdAwa
protected static Object prepareRecipient(Exchange exchange, Object
recipient) throws NoTypeConversionAvailableException {
if (recipient instanceof Endpoint || recipient instanceof
NormalizedEndpointUri) {
return recipient;
- } else if (recipient instanceof String) {
+ } else if (recipient instanceof String string) {
// trim strings as end users might have added spaces between
separators
- recipient = ((String) recipient).trim();
+ recipient = string.trim();
}
if (recipient != null) {
CamelContext ecc = exchange.getContext();
String uri;
- if (recipient instanceof String) {
- uri = (String) recipient;
+ if (recipient instanceof String string) {
+ uri = string;
} else {
// convert to a string type we can work with
uri = ecc.getTypeConverter().mandatoryConvertTo(String.class,
exchange, recipient);
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/Splitter.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/Splitter.java
index f8d2ab96cfd..4c2159d1eae 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/Splitter.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/Splitter.java
@@ -236,8 +236,8 @@ public class Splitter extends MulticastProcessor implements
AsyncProcessor, Trac
if (isShareUnitOfWork()) {
prepareSharedUnitOfWork(newExchange, copy);
}
- if (part instanceof Message) {
- newExchange.setIn((Message) part);
+ if (part instanceof Message message) {
+ newExchange.setIn(message);
} else {
Message in = newExchange.getIn();
in.setBody(part);
@@ -279,8 +279,8 @@ public class Splitter extends MulticastProcessor implements
AsyncProcessor, Trac
}
}
} finally {
- if (pairs instanceof Closeable) {
- IOHelper.close((Closeable) pairs,
"Splitter:ProcessorExchangePairs");
+ if (pairs instanceof Closeable closeable) {
+ IOHelper.close(closeable, "Splitter:ProcessorExchangePairs");
}
}
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThreadsProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThreadsProcessor.java
index 48731414e82..b9e2ddaa63b 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThreadsProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThreadsProcessor.java
@@ -133,8 +133,7 @@ public class ThreadsProcessor extends AsyncProcessorSupport
implements IdAware,
// tell Camel routing engine we continue routing asynchronous
return false;
} catch (Exception e) {
- if (executorService instanceof ThreadPoolExecutor) {
- ThreadPoolExecutor tpe = (ThreadPoolExecutor) executorService;
+ if (executorService instanceof ThreadPoolExecutor tpe) {
// process the call in synchronous mode
ProcessCall call = new ProcessCall(exchange, callback, true);
rejectedPolicy.asRejectedExecutionHandler().rejectedExecution(call, tpe);
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/WireTapProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/WireTapProcessor.java
index a0222c09a97..ee533fb4c47 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/WireTapProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/WireTapProcessor.java
@@ -226,9 +226,8 @@ public class WireTapProcessor extends AsyncProcessorSupport
// if the body is a stream cache we must use a copy of the stream in
the wire tapped exchange
Message msg = answer.getMessage();
- if (msg.getBody() instanceof StreamCache) {
- // in parallel processing case, the stream must be copied,
therefore get the stream
- StreamCache cache = (StreamCache) msg.getBody();
+ if (msg.getBody() instanceof StreamCache cache) {
+ // in parallel processing case, the stream must be copied,
therefore, get the stream
StreamCache copied = cache.copy(answer);
if (copied != null) {
msg.setBody(copied);
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/resume/ResumableCompletion.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/resume/ResumableCompletion.java
index 35035215b59..0d97c72d58d 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/resume/ResumableCompletion.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/resume/ResumableCompletion.java
@@ -48,9 +48,7 @@ public class ResumableCompletion implements Synchronization {
Object offset =
ExchangeHelper.getResultMessage(exchange).getHeader(Exchange.OFFSET);
- if (offset instanceof Resumable) {
- Resumable resumable = (Resumable) offset;
-
+ if (offset instanceof Resumable resumable) {
if (LOG.isTraceEnabled()) {
LOG.trace("Processing the resumable: {}",
resumable.getOffsetKey());
LOG.trace("Processing the resumable of type: {}",
resumable.getLastOffset().getValue());
@@ -75,9 +73,7 @@ public class ResumableCompletion implements Synchronization {
Exception e = exchange.getException();
Object resObj = exchange.getMessage().getHeader(Exchange.OFFSET);
- if (resObj instanceof Resumable) {
- Resumable resumable = (Resumable) resObj;
-
+ if (resObj instanceof Resumable resumable) {
String logMessage = String.format(
"Skipping offset update with address '%s' and offset value
'%s' due to failure in processing: %s",
resumable.getOffsetKey(),
resumable.getLastOffset().getValue(), e.getMessage());
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/validator/ProcessorValidator.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/validator/ProcessorValidator.java
index cb3855ce275..fabae082c4c 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/validator/ProcessorValidator.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/validator/ProcessorValidator.java
@@ -68,8 +68,8 @@ public class ProcessorValidator extends Validator {
ExchangeHelper.copyResults(exchange, copy);
}
} catch (Exception e) {
- if (e instanceof ValidationException) {
- throw (ValidationException) e;
+ if (e instanceof ValidationException validationException) {
+ throw validationException;
} else {
throw new ValidationException(String.format("Validation failed
for '%s'", type), exchange, e);
}