chore(QTDI-2893): use pattern matching for instanceof (S6201)#1227
chore(QTDI-2893): use pattern matching for instanceof (S6201)#1227undx wants to merge 10 commits into
Conversation
Replace `instanceof` + manual cast patterns with Java 17 pattern matching for instanceof to reduce Sonar warnings (java:S6201). Both `(X) value` and `X.class.cast(value)` cast forms are handled. Excluded cases: - Conditions combined with `||` (pattern matching incompatible with alternation): AvroSchemaCache, SchemaConverter (Double||Float). - ParameterSetter#set: target variable declared outside the `if` block (would require restructuring). - Casts to a type different from the `instanceof` check (not S6201).
There was a problem hiding this comment.
Pull request overview
This PR modernizes several Java 17 code paths by replacing instanceof + explicit cast patterns with pattern matching for instanceof, primarily to address Sonar warning java:S6201 across the component runtime and related modules.
Changes:
- Refactors multiple
instanceofblocks to use Java 17 pattern variables (e.g.,if (x instanceof Foo foo)). - Simplifies downstream usage by removing redundant
Class#cast()and explicit casts once a pattern variable is available. - Applies the refactor consistently across runtime, DI studio, Beam integration, tools, and tests.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| component-tools/src/main/java/org/talend/sdk/component/tools/validator/ActionValidator.java | Uses pattern matching for ParameterizedType handling in return-type validation. |
| component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/studio/RuntimeContextInjector.java | Refactors Delegated handling to use a pattern variable. |
| component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/studio/ParameterSetter.java | Refactors list access guarded by instanceof List to use a pattern variable. |
| component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/studio/AfterVariableExtracter.java | Refactors Delegated handling to use a pattern variable. |
| component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/schema/TaCoKitGuessSchema.java | Refactors several instanceof/cast sites in schema discovery and result interpretation. |
| component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/OutputsHandler.java | Refactors Record handling to use a pattern variable. |
| component-runtime-testing/component-runtime-http-junit/src/main/java/org/talend/sdk/component/junit/http/internal/impl/DefaultResponseLocator.java | Refactors ParameterizedType equality path to use a pattern variable. |
| component-runtime-manager/src/test/java/org/talend/sdk/component/runtime/manager/service/ProducerFinderImplTest.java | Refactors a test helper Record cast to pattern matching. |
| component-runtime-manager/src/main/java/org/talend/sdk/component/runtime/manager/xbean/converter/SchemaConverter.java | Refactors primitive-wrapper handling in JSON conversion (and exposes a Float-cast bug in the touched hunk). |
| component-runtime-manager/src/main/java/org/talend/sdk/component/runtime/manager/service/ServiceHelper.java | Refactors BaseService cast to pattern matching. |
| component-runtime-manager/src/main/java/org/talend/sdk/component/runtime/manager/ComponentManager.java | Refactors JsonObject / JsonArray casts in jsonToMap to pattern matching. |
| component-runtime-impl/src/main/java/org/talend/sdk/component/runtime/record/RecordImpl.java | Refactors datetime-related instanceof casts to pattern matching. |
| component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroSchemaCache.java | Refactors SchemaImpl cast to pattern matching. |
| component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java | Refactors multiple mapping/casting branches to pattern matching. |
| component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroEntryBuilder.java | Refactors AvroSchema cast to pattern matching. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ozhelezniak-talend
left a comment
There was a problem hiding this comment.
LGTM
potentially might have a merge conflict with my pr...
Sure, you'll merge first, will rebase then. |
# Conflicts: # component-runtime-beam/src/main/java/org/talend/sdk/component/runtime/beam/spi/record/AvroRecord.java # component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/OutputsHandler.java # component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/schema/TaCoKitGuessSchema.java # component-studio/component-runtime-di/src/main/java/org/talend/sdk/component/runtime/di/studio/ParameterSetter.java
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 97 out of 98 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
vault-client/src/main/java/org/talend/sdk/components/vault/client/VaultClient.java:415
- In the auth error handler,
wae.getResponse()is dereferenced even though you already captured it inresponse. IfgetResponse()is null, this will throw a NPE while handling an error. Please use the localresponsevariable and guard for null before reading the entity/status.
rebase brought 83 files more than initial pr. |
| final ResponseLocator responseLocator = server.getResponseLocator(); | ||
| if (responseLocator instanceof DefaultResponseLocator) { | ||
| if (responseLocator instanceof DefaultResponseLocator defaultResponseLocatorVal) { | ||
| final DefaultResponseLocator defaultResponseLocator = |
There was a problem hiding this comment.
Strange that IDEA somewhere did a smart replacement and get rid of this variable, and somewhere it's done a dummy replacement with redundant variable.
Did you use Intellij Idea Code clean-up tool?
| executeOnListeners(l -> l.onError(event), null); | ||
| if (!response.isCommitted() && response instanceof HttpServletResponse) { | ||
| final HttpServletResponse http = (HttpServletResponse) response; | ||
| if (!response.isCommitted() && response instanceof HttpServletResponse) { // NOSONAR |
There was a problem hiding this comment.
again. this NOSONAR is strange...
| onClose.run(); | ||
| if (asciidoctor != null && !Boolean.getBoolean("talend.component.tools.jruby.teardown.skip")) { | ||
| if (asciidoctor instanceof AutoCloseable) { | ||
| if (asciidoctor instanceof AutoCloseable) { // NOSONAR |
There was a problem hiding this comment.
Could we double check all those no SONAR (I didn't report all of them in the comments)

https://qlik-dev.atlassian.net/browse/QTDI-2893
Replace
instanceof+ manual cast patterns with Java 17 pattern matching for instanceof to reduce Sonar warnings (java:S6201).Both
(X) valueandX.class.cast(value)cast forms are handled.Excluded cases:
||(pattern matching incompatible with alternation): AvroSchemaCache, SchemaConverter (Double||Float).ifblock (would require restructuring).instanceofcheck (not S6201).Requirements
Why this PR is needed?
What does this PR adds (design/code thoughts)?
AI generated code
https://internal.qlik.dev/general/ways-of-working/code-reviews/#guidelines-for-ai-generated-code