gnodet-bot commented on code in PR #30035:
URL: https://github.com/apache/camel/pull/30035#discussion_r4579069005
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/TransformRoute.java:
##########
@@ -181,6 +184,23 @@ protected void doAddInitialProperty(KameletMain main) {
return printDump(target);
}
+ /**
+ * The Java route with the class named after the file it is written to: a
class must have the name of its file, and
+ * --output=Out.java got the class named after the route file
(CAMEL-26931).
+ */
+ static String withClassName(String java, String fileName, Path out) {
+ String wanted = out.getFileName().toString();
+ if (!wanted.endsWith(".java") || fileName == null ||
!fileName.endsWith(".java")) {
+ return java;
+ }
+ wanted = wanted.substring(0, wanted.length() - ".java".length());
+ String current = fileName.substring(0, fileName.length() -
".java".length());
+ if (wanted.equals(current) || !SourceVersion.isName(wanted)) {
Review Comment:
`SourceVersion.isName(wanted)` accepts **qualified** names (JLS §6.2: a
qualified name is a sequence of identifiers separated by dots). A file named
`My.Route.java` would strip to `wanted = "My.Route"`, which passes `isName`
(splits on `.` → both `My` and `Route` are valid identifiers) but generates
`class My.Route` — invalid Java that will fail to compile.
The correct check for a simple class name is `isIdentifier` (no dots
allowed) combined with a keyword guard:
```suggestion
if (wanted.equals(current) || !SourceVersion.isIdentifier(wanted) ||
SourceVersion.isKeyword(wanted)) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]