Skip to content

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

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

chore(QTDI-2893): use pattern matching for instanceof (S6201)#1227
undx wants to merge 2 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.

@sonar-rnd
Copy link
Copy Markdown

sonar-rnd Bot commented May 21, 2026

Failed Quality Gate failed

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

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

View in SonarQube

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants