Skip to content

chore(QTDI-2893): use pattern matching for instanceof (S6201)#1227

Open
undx wants to merge 10 commits into
masterfrom
ouf/QTDI-2893_pattern_instanceof
Open

chore(QTDI-2893): use pattern matching for instanceof (S6201)#1227
undx wants to merge 10 commits into
masterfrom
ouf/QTDI-2893_pattern_instanceof

Conversation

@undx
Copy link
Copy Markdown
Member

@undx undx commented May 21, 2026

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) 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).

Requirements

  • Any code change adding any logic MUST be tested through a unit test executed with the default build
  • Any API addition MUST be done with a documentation update if relevant

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

  • this PR has been written with the help of GitHub Copilot or another generative AI tool

undx added 2 commits May 21, 2026 17:56
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).
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 instanceof blocks 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.

@undx undx requested a review from ozhelezniak-talend May 21, 2026 17:32
Copy link
Copy Markdown
Contributor

@ozhelezniak-talend ozhelezniak-talend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
potentially might have a merge conflict with my pr...

@undx
Copy link
Copy Markdown
Member Author

undx commented May 22, 2026

LGTM potentially might have a merge conflict with my pr...

Sure, you'll merge first, will rebase then.

undx added 2 commits May 29, 2026 11:38
# 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
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in response. If getResponse() is null, this will throw a NPE while handling an error. Please use the local response variable and guard for null before reading the entity/status.

@undx
Copy link
Copy Markdown
Member Author

undx commented Jun 3, 2026

LGTM potentially might have a merge conflict with my pr...

Sure, you'll merge first, will rebase then.

rebase brought 83 files more than initial pr.

@undx undx requested a review from ozhelezniak-talend June 3, 2026 17:41
Comment thread component-runtime-impl/pom.xml Outdated
final ResponseLocator responseLocator = server.getResponseLocator();
if (responseLocator instanceof DefaultResponseLocator) {
if (responseLocator instanceof DefaultResponseLocator defaultResponseLocatorVal) {
final DefaultResponseLocator defaultResponseLocator =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we double check all those no SONAR (I didn't report all of them in the comments)

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 102 out of 103 changed files in this pull request and generated 2 comments.

@sonar-rnd
Copy link
Copy Markdown

sonar-rnd Bot commented Jun 4, 2026

Failed Quality Gate failed

  • 0.00% Coverage on New Code (is less than 80.00%)
  • 117 New Issues (is greater than 0)

Project ID: org.talend.sdk.component:component-runtime

View in SonarQube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants