Skip to content

(chores): fix SonarCloud S1612 across multiple modules#22304

Open
orpiske wants to merge 1 commit intoapache:mainfrom
orpiske:ci-camel-4-S1612
Open

(chores): fix SonarCloud S1612 across multiple modules#22304
orpiske wants to merge 1 commit intoapache:mainfrom
orpiske:ci-camel-4-S1612

Conversation

@orpiske
Copy link
Copy Markdown
Contributor

@orpiske orpiske commented Mar 28, 2026

Summary

Claude Code on behalf of Otavio Piske

  • Replace lambdas with method references where applicable (SonarCloud rule S1612)
  • Covers 145 files across core, components, dsl, test-infra, and tooling modules
  • ~220 lambda-to-method-reference replacements

Details

SonarCloud rule S1612 flags lambdas that can be replaced with method references for improved readability. Examples:

  • x -> x.method()Type::method
  • () -> obj.method()obj::method
  • x -> x instanceof TypeType.class::isInstance
  • () -> new Type()Type::new

A few cases were intentionally left as lambdas where method references cause type inference ambiguity (e.g., assertDoesNotThrow with methods that throw checked exceptions, or Camel DSL body() overloads).

Test plan

  • Core module tests pass (ObjectHelperTest, ServiceSupportTest, FluentProducerTemplateTest, MainTest, etc.)
  • All changed modules compile successfully (some modules have pre-existing build issues unrelated to this change)
  • Kafka unit tests pass
  • CI verification

Replace lambdas with method references where applicable (S1612).
Covers core, components, dsl, test-infra, and tooling modules.

Some cases involving assertDoesNotThrow ambiguity or Camel DSL
body() method overloads were left as lambdas since method
references cause type inference ambiguity in those contexts.
@github-actions
Copy link
Copy Markdown
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using build-all, build-dependents, skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

Copy link
Copy Markdown
Contributor

@gnodet gnodet left a comment

Choose a reason for hiding this comment

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

Review: (chores): fix SonarCloud S1612 across multiple modules

Summary

All ~220 lambda-to-method-reference conversions were individually verified and are semantically correct — no behavioral changes, no type inference ambiguity, and no checked exception issues. The refactoring is well-executed overall.

However, the PR includes unrelated changes in a generated file that must be addressed before merging.

Detailed Analysis

Correctness: Every method reference was checked against its original lambda for semantic equivalence. Key patterns verified:

  • x instanceof TypeType.class::isInstance: all target types match the original instanceof checks
  • () -> obj.method()obj::method in assertThrows/assertDoesNotThrow: all valid — Executable declares throws Throwable, so checked-exception methods are compatible
  • (carrier, key, value) -> carrier.put(key, value)SpanContextPropagationInjector::put in tracing code: correctly resolved as unbound instance method reference matching TextMapSetter<SpanContextPropagationInjector>
  • Constructor references (SSLContextParameters::new, SalesforceEndpointConfig::new): all no-arg, safe
  • All 9 newly added imports are used and correct

Thread safety: No concerns — all conversions are purely syntactic.

Test coverage: This is a refactoring PR with no behavioral changes; existing tests provide adequate coverage.

Issues Found

Severity Issue
Blocking camelYamlDsl.json contains ~1,500 lines of unrelated generated schema changes (see inline)
Minor Comparable::compareTo should be String::compareTo in ZooKeeperGroup.java (see inline suggestion)
Nit Several explanatory comments silently removed in Kafka/Elasticsearch tests (see inline)

Claude Code on behalf of Guillaume Nodet

}
} ]
},
"org.apache.camel.model.SagaActionUriDefinition" : {
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.

Blocking: This generated file contains ~1,500 lines of changes that are not part of the S1612 lambda-to-method-reference refactoring:

  • Removal of groovyJson / GroovyJSonDataFormat entries (3 locations)
  • Addition of SagaActionUriDefinition (new schema definition, shown here)
  • SagaDefinition.compensation and completion changed from type: string to $ref: SagaActionUriDefinition
  • Addition of org.apache.camel.model.app.* and org.apache.camel.model.cloud.* definitions
  • WireTapDefinition schema expansion/rewrite

These are regeneration artifacts from building against a branch state that has diverged from main. Including unrelated generated file changes in a focused refactoring PR makes review harder and risks sneaking in unintended schema changes.

Please revert this file:

git checkout main -- dsl/camel-yaml-dsl/camel-yaml-dsl/src/generated/resources/schema/camelYamlDsl.json

Or regenerate it cleanly against the PR's target branch.

ensurePath.ensure(client.getZookeeperClient());
List<String> children = client.getChildren().usingWatcher(childrenWatcher).forPath(path);
children.sort((String left, String right) -> left.compareTo(right));
children.sort(Comparable::compareTo);
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.

Minor: Comparable::compareTo uses a raw type method reference. Since children is List<String>, String::compareTo is more precise and avoids potential unchecked/raw type warnings.

Suggested change
children.sort(Comparable::compareTo);
children.sort(String::compareTo);

// retry the message with an error
doCommitOffset(exchange);
});
.process(KafkaBreakOnFirstErrorWithBatchUsingKafkaManualCommitIT.this::doCommitOffset);
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.

Nit: The lambda-to-method-reference conversion here also removed an explanatory comment that documented why the manual commit is needed:

// if we don't commit
// camel will continuously
// retry the message with an error

Similar comment removals occur in:

  • ElasticsearchRestClientComponentIT.java ("Assert that the direct:get-by-id endpoint received the exception")
  • KafkaBatchingProcessingBreakOnFirstErrorManualCommitIT.java ("Manual commit on success")

These comments explain intent, not implementation, and remain valuable even with method references. Consider preserving them above the .process() call:

                        // if we don't commit, camel will continuously retry the message with an error
                        .process(KafkaBreakOnFirstErrorWithBatchUsingKafkaManualCommitIT.this::doCommitOffset);

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.

2 participants