davsclaus commented on code in PR #27997:
URL: https://github.com/apache/camel/pull/27997#discussion_r4301956281
##########
tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PrepareCamelJBangCommandsMojo.java:
##########
@@ -534,22 +518,109 @@ private void
parseOptionsFromClassHierarchy(JavaClassSource clazz, List<OptionIn
// Also parse options from @CommandLine.Mixin fields
for (FieldSource<JavaClassSource> field : clazz.getFields()) {
if (hasMixinAnnotation(field)) {
- String mixinTypeName = field.getType().getName();
- File mixinFile = findClassFile(baseDir, mixinTypeName);
- if (mixinFile != null && mixinFile.exists()) {
- try {
- JavaType<?> parsedMixin = Roaster.parse(mixinFile);
- if (parsedMixin instanceof JavaClassSource mixinClazz)
{
- parseOptionsFromClassHierarchy(mixinClazz,
options, mixinFile.getParentFile());
- } else {
- getLog().debug("Skipping options from non-class
mixin type: " + mixinTypeName);
- }
- } catch (Exception e) {
- getLog().debug("Could not parse mixin class: " +
mixinTypeName + " - " + e.getMessage());
+ parseOptionsFromType(clazz, field.getType().getName(),
options, baseDir, "mixin");
+ }
+ }
+ }
+
+ /**
+ * Parses the options of a parent class or a mixin type of a class: from
its source when it is one of the command
+ * classes, or else from its class on the classpath of the project, such
as the class of a library that a plugin
+ * command extends. The source is found by the simple name of the type, so
for a type that the class imports by name
+ * it is only taken when it is of that type, as a class of a library may
have the simple name of a command of
+ * camel-jbang-core. (The package that Roaster resolves for a type that is
not imported by name is not certain: with
+ * an import on demand of a class, it takes a class of the same package
for a nested class of the imported one.)
+ */
+ private void parseOptionsFromType(JavaClassSource clazz, String type,
List<OptionInfo> options, File baseDir, String kind) {
+ String simpleName = type.substring(type.lastIndexOf('.') + 1);
+ String imported = importedByName(clazz, simpleName);
+ File file = findClassFile(baseDir, simpleName);
+ if (file != null && file.exists()) {
+ try {
+ JavaType<?> parsed = Roaster.parse(file);
+ if (imported == null ||
imported.equals(parsed.getQualifiedName())) {
+ if (parsed instanceof JavaClassSource source) {
+ parseOptionsFromClassHierarchy(source, options,
file.getParentFile());
+ } else {
+ getLog().debug("Skipping options from non-class " +
kind + " type: " + simpleName);
}
+ return;
}
+ } catch (Exception e) {
+ getLog().debug("Could not parse " + kind + " class: " +
simpleName + " - " + e.getMessage());
+ return;
Review Comment:
Done in the latest commit: a parse failure returns only when the type is not
imported by name, so otherwise the options are read from the class on the
classpath.
`testPluginCommandGetsTheOptionsOfTheLibraryClassWhenASourceWithItsNameCannotBeParsed`
has a camel-jbang-core `Init.java` that Roaster cannot parse (it throws
`ParserException`) and gets the options of the library class; with the early
return it got none.
_Claude Code on behalf of davsclaus_
--
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]