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 df382a12083 (chores) convert core/camel-base to use pattern matching
for instanceof (#15179)
df382a12083 is described below
commit df382a12083451217b267e2a586bd2217aaefbc7
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Mon Aug 19 10:01:29 2024 +0200
(chores) convert core/camel-base to use pattern matching for instanceof
(#15179)
---
.../properties/DefaultPropertiesLookup.java | 33 +++++++++++-----------
.../properties/DefaultPropertiesParser.java | 11 +++++---
.../component/properties/PropertiesComponent.java | 31 ++++++++------------
.../camel/converter/CollectionConverter.java | 17 ++++++-----
.../org/apache/camel/converter/IOConverter.java | 8 +++---
.../apache/camel/converter/ObjectConverter.java | 7 ++---
.../camel/impl/converter/ArrayTypeConverter.java | 3 +-
.../converter/AsyncProcessorTypeConverter.java | 4 +--
.../impl/converter/BaseTypeConverterRegistry.java | 10 +++----
.../impl/converter/CoreTypeConverterRegistry.java | 12 ++++----
.../camel/impl/converter/FutureTypeConverter.java | 3 +-
.../camel/impl/event/DefaultEventFactory.java | 8 +++---
12 files changed, 68 insertions(+), 79 deletions(-)
diff --git
a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java
b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java
index 629584e0e03..3f8ad715e0a 100644
---
a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java
+++
b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java
@@ -58,8 +58,8 @@ public class DefaultPropertiesLookup implements
PropertiesLookup {
answer =
component.getCamelContext().getTypeConverter().mandatoryConvertTo(String.class,
value);
String loc = location(local, name, "LocalProperties");
String localDefaultValue = null;
- if (local instanceof OrderedLocationProperties) {
- Object val = ((OrderedLocationProperties)
local).getDefaultValue(name);
+ if (local instanceof OrderedLocationProperties
locationProperties) {
+ Object val = locationProperties.getDefaultValue(name);
if (val != null) {
localDefaultValue
=
component.getCamelContext().getTypeConverter().mandatoryConvertTo(String.class,
val);
@@ -85,18 +85,17 @@ public class DefaultPropertiesLookup implements
PropertiesLookup {
answer = ps.getProperty(name, defaultValue);
if (answer != null) {
String source = ps.getName();
- if (ps instanceof ClasspathPropertiesSource) {
- source = "classpath:" + ((LocationPropertiesSource)
ps).getLocation().getPath();
- } else if (ps instanceof FilePropertiesSource) {
- source = "file:" + ((LocationPropertiesSource)
ps).getLocation().getPath();
- } else if (ps instanceof RefPropertiesSource) {
- source = "ref:" + ((LocationPropertiesSource)
ps).getLocation().getPath();
- } else if (ps instanceof LocationPropertiesSource) {
- source = ((LocationPropertiesSource)
ps).getLocation().getPath();
- } else if (ps instanceof LoadablePropertiesSource) {
- Properties prop = ((LoadablePropertiesSource)
ps).loadProperties();
- if (prop instanceof OrderedLocationProperties) {
- OrderedLocationProperties olp =
(OrderedLocationProperties) prop;
+ if (ps instanceof ClasspathPropertiesSource propSource) {
+ source = "classpath:" +
propSource.getLocation().getPath();
+ } else if (ps instanceof FilePropertiesSource propSource) {
+ source = "file:" + propSource.getLocation().getPath();
+ } else if (ps instanceof RefPropertiesSource propSource) {
+ source = "ref:" + propSource.getLocation().getPath();
+ } else if (ps instanceof LocationPropertiesSource
propSource) {
+ source = propSource.getLocation().getPath();
+ } else if (ps instanceof LoadablePropertiesSource
propSource) {
+ Properties prop = propSource.loadProperties();
+ if (prop instanceof OrderedLocationProperties olp) {
source = olp.getLocation(name);
}
}
@@ -131,13 +130,13 @@ public class DefaultPropertiesLookup implements
PropertiesLookup {
private static String location(Properties prop, String name, String
defaultLocation) {
String loc = null;
- if (prop instanceof OrderedLocationProperties) {
- loc = ((OrderedLocationProperties) prop).getLocation(name);
+ if (prop instanceof OrderedLocationProperties value) {
+ loc = value.getLocation(name);
}
+
if (loc == null) {
loc = defaultLocation;
}
return loc;
}
-
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
index ef7286d8e6c..2a5dd373ea8 100644
---
a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
+++
b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
@@ -37,8 +37,11 @@ import static
org.apache.camel.util.IOHelper.lookupEnvironmentVariable;
* A parser to parse a string which contains property placeholders.
*/
public class DefaultPropertiesParser implements PropertiesParser {
+
private static final String UNRESOLVED_PREFIX_TOKEN = "@@[";
+
private static final String UNRESOLVED_SUFFIX_TOKEN = "]@@";
+
private static final String GET_OR_ELSE_TOKEN = ":";
protected final Logger log = LoggerFactory.getLogger(getClass());
@@ -424,8 +427,8 @@ public class DefaultPropertiesParser implements
PropertiesParser {
if (value != null) {
String localDefaultValue = null;
String loc = location(local, key, "LocalProperties");
- if (local instanceof OrderedLocationProperties) {
- Object val = ((OrderedLocationProperties)
local).getDefaultValue(key);
+ if (local instanceof OrderedLocationProperties propSource)
{
+ Object val = propSource.getDefaultValue(key);
if (val != null) {
localDefaultValue
=
propertiesComponent.getCamelContext().getTypeConverter().tryConvertTo(String.class,
val);
@@ -510,8 +513,8 @@ public class DefaultPropertiesParser implements
PropertiesParser {
private static String location(Properties prop, String name, String
defaultLocation) {
String loc = null;
- if (prop instanceof OrderedLocationProperties) {
- loc = ((OrderedLocationProperties) prop).getLocation(name);
+ if (prop instanceof OrderedLocationProperties olp) {
+ loc = olp.getLocation(name);
}
if (loc == null) {
loc = defaultLocation;
diff --git
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index 21864dd5199..0cfa3746e72 100644
---
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -205,13 +205,12 @@ public class PropertiesComponent extends ServiceSupport
// override properties from higher priority ones
for (int i = sources.size(); i-- > 0;) {
PropertiesSource ps = sources.get(i);
- if (ps instanceof LoadablePropertiesSource) {
- LoadablePropertiesSource lps = (LoadablePropertiesSource)
ps;
+ if (ps instanceof LoadablePropertiesSource lps) {
Properties p = lps.loadProperties();
- if (p instanceof OrderedLocationProperties) {
- prop.putAll((OrderedLocationProperties) p);
- } else if (ps instanceof LocationPropertiesSource) {
- String loc = ((LocationPropertiesSource)
ps).getLocation().getPath();
+ if (p instanceof OrderedLocationProperties propSource) {
+ prop.putAll(propSource);
+ } else if (ps instanceof LocationPropertiesSource
propSource) {
+ String loc = propSource.getLocation().getPath();
prop.putAll(loc, p);
} else {
prop.putAll(lps.getName(), p);
@@ -273,11 +272,9 @@ public class PropertiesComponent extends ServiceSupport
// override properties from higher priority ones
for (int i = sources.size(); i-- > 0;) {
PropertiesSource ps = sources.get(i);
- if (ps instanceof LoadablePropertiesSource) {
- LoadablePropertiesSource lps = (LoadablePropertiesSource)
ps;
+ if (ps instanceof LoadablePropertiesSource lps) {
Properties p = lps.loadProperties(filter);
- if (p instanceof OrderedLocationProperties) {
- OrderedLocationProperties olp =
(OrderedLocationProperties) p;
+ if (p instanceof OrderedLocationProperties olp) {
for (String name : olp.stringPropertyNames()) {
String loc = olp.getLocation(name);
Object value = olp.getProperty(name);
@@ -286,8 +283,7 @@ public class PropertiesComponent extends ServiceSupport
}
} else {
String loc = lps.getName();
- if (ps instanceof LocationPropertiesSource) {
- LocationPropertiesSource olp =
(LocationPropertiesSource) ps;
+ if (ps instanceof LocationPropertiesSource olp) {
loc = olp.getLocation().getPath();
}
for (String name : p.stringPropertyNames()) {
@@ -711,9 +707,7 @@ public class PropertiesComponent extends ServiceSupport
// find sources with this location to reload
for (PropertiesSource source : sources) {
- if (source instanceof LocationPropertiesSource && source
instanceof LoadablePropertiesSource) {
- LocationPropertiesSource loc = (LocationPropertiesSource)
source;
- LoadablePropertiesSource loadable = (LoadablePropertiesSource)
source;
+ if (source instanceof LocationPropertiesSource loc && source
instanceof LoadablePropertiesSource loadable) {
String schemeAndPath = loc.getLocation().getResolver() + ":" +
loc.getLocation().getPath();
String path = loc.getLocation().getPath();
if (PatternHelper.matchPattern(schemeAndPath, pattern) ||
PatternHelper.matchPattern(path, pattern)) {
@@ -757,8 +751,8 @@ public class PropertiesComponent extends ServiceSupport
}
// inject the component to the parser
- if (propertiesParser instanceof DefaultPropertiesParser) {
- ((DefaultPropertiesParser)
propertiesParser).setPropertiesComponent(this);
+ if (propertiesParser instanceof DefaultPropertiesParser
defaultPropertiesParser) {
+ defaultPropertiesParser.setPropertiesComponent(this);
}
if (isAutoDiscoverPropertiesSources()) {
@@ -774,8 +768,7 @@ public class PropertiesComponent extends ServiceSupport
Class<?> type =
factoryFinder.findClass("properties-source-factory").orElse(null);
if (type != null) {
Object obj =
getCamelContext().getInjector().newInstance(type, false);
- if (obj instanceof PropertiesSource) {
- PropertiesSource ps = (PropertiesSource) obj;
+ if (obj instanceof PropertiesSource ps) {
addPropertiesSource(ps);
LOG.debug("PropertiesComponent added custom
PropertiesSource (factory): {}", ps);
} else if (obj != null) {
diff --git
a/core/camel-base/src/main/java/org/apache/camel/converter/CollectionConverter.java
b/core/camel-base/src/main/java/org/apache/camel/converter/CollectionConverter.java
index 78759eca0f6..7bbfc6ae7e6 100644
---
a/core/camel-base/src/main/java/org/apache/camel/converter/CollectionConverter.java
+++
b/core/camel-base/src/main/java/org/apache/camel/converter/CollectionConverter.java
@@ -72,8 +72,8 @@ public final class CollectionConverter {
*/
@Converter(order = 4)
public static <T> ArrayList<T> toArrayList(Iterator<T> it) {
- if (it instanceof ArrayList) {
- return (ArrayList<T>) it;
+ if (it instanceof ArrayList list) {
+ return list;
}
ArrayList<T> list = new ArrayList<>();
while (it.hasNext()) {
@@ -87,8 +87,8 @@ public final class CollectionConverter {
*/
@Converter(order = 5)
public static <T> ArrayList<T> toArrayList(Iterable<T> it) {
- if (it instanceof ArrayList) {
- return (ArrayList<T>) it;
+ if (it instanceof ArrayList list) {
+ return list;
}
ArrayList<T> list = new ArrayList<>();
for (T value : it) {
@@ -139,8 +139,8 @@ public final class CollectionConverter {
*/
@Converter(order = 13)
public static <T> List<T> toList(Iterable<T> iterable) {
- if (iterable instanceof List) {
- return (List<T>) iterable;
+ if (iterable instanceof List list) {
+ return list;
}
List<T> result = new LinkedList<>();
for (T value : iterable) {
@@ -154,8 +154,8 @@ public final class CollectionConverter {
*/
@Converter(order = 14)
public static <T> List<T> toList(Iterator<T> it) {
- if (it instanceof List) {
- return (List<T>) it;
+ if (it instanceof List value) {
+ return value;
}
List<T> result = new LinkedList<>();
while (it.hasNext()) {
@@ -163,5 +163,4 @@ public final class CollectionConverter {
}
return result;
}
-
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
b/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
index b3e8e4e6909..b446b60f309 100644
--- a/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
+++ b/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
@@ -210,8 +210,8 @@ public final class IOConverter {
@Converter(order = 26)
public static ObjectOutput toObjectOutput(OutputStream stream) throws
IOException {
- if (stream instanceof ObjectOutput) {
- return (ObjectOutput) stream;
+ if (stream instanceof ObjectOutput out) {
+ return out;
} else {
return new ObjectOutputStream(IOHelper.buffered(stream));
}
@@ -219,8 +219,8 @@ public final class IOConverter {
@Converter(order = 27)
public static ObjectInput toObjectInput(final InputStream stream, final
Exchange exchange) throws IOException {
- if (stream instanceof ObjectInput) {
- return (ObjectInput) stream;
+ if (stream instanceof ObjectInput objectInput) {
+ return objectInput;
} else {
return new ObjectInputStream(IOHelper.buffered(stream)) {
@Override
diff --git
a/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
b/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
index 44196407fee..fc365e117c1 100644
---
a/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
+++
b/core/camel-base/src/main/java/org/apache/camel/converter/ObjectConverter.java
@@ -229,13 +229,12 @@ public final class ObjectConverter {
if (org.apache.camel.util.ObjectHelper.isNaN(value)) {
return null;
}
- if (value instanceof String) {
- return new BigInteger((String) value);
+ if (value instanceof String str) {
+ return new BigInteger(str);
}
Long num = null;
- if (value instanceof Number) {
- Number number = (Number) value;
+ if (value instanceof Number number) {
num = number.longValue();
}
if (num != null) {
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
index 0bb1a376c4f..4716d0c62e3 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/ArrayTypeConverter.java
@@ -35,8 +35,7 @@ public class ArrayTypeConverter extends TypeConverterSupport {
@SuppressWarnings("unchecked")
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
if (type.isArray()) {
- if (value instanceof Collection) {
- Collection<?> collection = (Collection<?>) value;
+ if (value instanceof Collection collection) {
Object array = Array.newInstance(type.getComponentType(),
collection.size());
if (array instanceof Object[]) {
collection.toArray((Object[]) array);
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
index de891173877..5581f0e998a 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/AsyncProcessorTypeConverter.java
@@ -31,8 +31,8 @@ public class AsyncProcessorTypeConverter extends
TypeConverterSupport {
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
if (type.equals(AsyncProcessor.class)) {
- if (value instanceof Processor) {
- return
type.cast(AsyncProcessorConverterHelper.convert((Processor) value));
+ if (value instanceof Processor p) {
+ return type.cast(AsyncProcessorConverterHelper.convert(p));
}
}
return null;
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
index cb6617cc3fd..4d0e868e097 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
@@ -93,8 +93,8 @@ public abstract class BaseTypeConverterRegistry extends
CoreTypeConverterRegistr
try {
// scan the class for @Converter and load them into this registry
TypeConvertersLoader loader;
- if (typeConverters instanceof Class) {
- loader = new TypeConvertersLoader((Class<?>) typeConverters);
+ if (typeConverters instanceof Class tcs) {
+ loader = new TypeConvertersLoader(tcs);
} else {
loader = new TypeConvertersLoader(typeConverters);
}
@@ -162,8 +162,7 @@ public abstract class BaseTypeConverterRegistry extends
CoreTypeConverterRegistr
}
Object obj = getInjector().newInstance(clazz, false);
CamelContextAware.trySetCamelContext(obj, getCamelContext());
- if (obj instanceof TypeConverterLoader) {
- TypeConverterLoader loader = (TypeConverterLoader) obj;
+ if (obj instanceof TypeConverterLoader loader) {
CamelContextAware.trySetCamelContext(loader,
getCamelContext());
LOG.debug("TypeConverterLoader: {} loading converters", name);
loader.load(this);
@@ -272,8 +271,7 @@ public abstract class BaseTypeConverterRegistry extends
CoreTypeConverterRegistr
.filter(Objects::nonNull)
.findAny().orElseThrow(() -> new
ClassNotFoundException(name));
Object obj = getInjector().newInstance(clazz, false);
- if (obj instanceof TypeConverter) {
- TypeConverter fb = (TypeConverter) obj;
+ if (obj instanceof TypeConverter fb) {
LOG.debug("Adding loaded FallbackTypeConverter: {}", name);
addFallbackTypeConverter(fb, false);
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/CoreTypeConverterRegistry.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/CoreTypeConverterRegistry.java
index 7ea6c911648..f37de3bf176 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/CoreTypeConverterRegistry.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/CoreTypeConverterRegistry.java
@@ -114,9 +114,9 @@ public abstract class CoreTypeConverterRegistry extends
ServiceSupport implement
Object answer = ObjectConverter.toBoolean(value);
requireNonNullBoolean(type, value, answer);
return (T) answer;
- } else if (type == Boolean.class && value instanceof String) {
+ } else if (type == Boolean.class && value instanceof String str) {
// String -> Boolean
- Boolean parsedBoolean = customParseBoolean((String) value);
+ Boolean parsedBoolean = customParseBoolean(str);
if (parsedBoolean != null) {
return (T) parsedBoolean;
}
@@ -215,9 +215,9 @@ public abstract class CoreTypeConverterRegistry extends
ServiceSupport implement
Object answer = ObjectConverter.toBoolean(value);
requireNonNullBoolean(type, value, answer);
return (T) answer;
- } else if (type == Boolean.class && value instanceof String) {
+ } else if (type == Boolean.class && value instanceof String str) {
// String -> Boolean
- Boolean parsedBoolean = customParseBoolean((String) value);
+ Boolean parsedBoolean = customParseBoolean(str);
if (parsedBoolean != null) {
return (T) parsedBoolean;
}
@@ -573,8 +573,8 @@ public abstract class CoreTypeConverterRegistry extends
ServiceSupport implement
protected TypeConversionException createTypeConversionException(
Exchange exchange, Class<?> type, Object value, Throwable cause) {
- if (cause instanceof TypeConversionException) {
- if (((TypeConversionException) cause).getToType() == type) {
+ if (cause instanceof TypeConversionException tce) {
+ if (tce.getToType() == type) {
return (TypeConversionException) cause;
}
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java
index 69b8af477e7..9da2fc178c4 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java
@@ -79,8 +79,7 @@ public final class FutureTypeConverter extends
TypeConverterSupport {
// maybe from is already the type we want
if (type.isAssignableFrom(body.getClass())) {
return type.cast(body);
- } else if (body instanceof Exchange) {
- Exchange result = (Exchange) body;
+ } else if (body instanceof Exchange result) {
body = ExchangeHelper.extractResultBody(result,
result.getPattern());
}
diff --git
a/core/camel-base/src/main/java/org/apache/camel/impl/event/DefaultEventFactory.java
b/core/camel-base/src/main/java/org/apache/camel/impl/event/DefaultEventFactory.java
index 5ce424cce97..2b53623566e 100644
---
a/core/camel-base/src/main/java/org/apache/camel/impl/event/DefaultEventFactory.java
+++
b/core/camel-base/src/main/java/org/apache/camel/impl/event/DefaultEventFactory.java
@@ -293,8 +293,8 @@ public class DefaultEventFactory implements EventFactory {
Exchange exchange, Processor failureHandler, boolean
deadLetterChannel, String deadLetterUri) {
// unwrap delegate processor
Processor handler = failureHandler;
- if (handler instanceof DelegateProcessor) {
- handler = ((DelegateProcessor) handler).getProcessor();
+ if (handler instanceof DelegateProcessor delegateProcessor) {
+ handler = delegateProcessor.getProcessor();
}
CamelEvent answer = new ExchangeFailureHandlingEvent(exchange,
handler, deadLetterChannel, deadLetterUri);
if (timestampEnabled) {
@@ -309,8 +309,8 @@ public class DefaultEventFactory implements EventFactory {
boolean deadLetterChannel, String deadLetterUri) {
// unwrap delegate processor
Processor handler = failureHandler;
- if (handler instanceof DelegateProcessor) {
- handler = ((DelegateProcessor) handler).getProcessor();
+ if (handler instanceof DelegateProcessor delegateProcessor) {
+ handler = delegateProcessor.getProcessor();
}
CamelEvent answer = new ExchangeFailureHandledEvent(exchange, handler,
deadLetterChannel, deadLetterUri);
if (timestampEnabled) {