Skip to content

aesh-processor: commands without a no-arg constructor, mixin fields it cannot reach, inaccessible subclasses #785

Open

Description

@davsclaus

We tried the annotation processor (aesh-processor 3.19.2) on the Camel CLI. It has 158 commands, and until now aesh read their annotations at run time. The generated metadata gives the same help and the same parse results as reading the annotations (checked against our help and parse golden files). It also saves time: 13 ms on camel ps (92 → 79 ms) and 33 ms on camel --help (243 → 210 ms). Most of that is the annotation proxies the JDK generates at run time, which the generated code avoids.

To get there we had to patch the processor and change some of our classes. These three things will hit other applications too:

1. A command without a no-arg constructor is rejected

@CommandDefinition(name = "ps", description = "List running integrations")
public class ListProcess implements Command<CommandInvocation> {
    public ListProcess(CamelJBangMain main) { ... }   // the application creates the instance
}
error: Command class must have an accessible no-arg constructor

Applications that create their own command instances, with dependencies passed to the constructor, never call newInstance(): AeshCommandContainerBuilder.create(Command) calls buildFromProvider(provider, command) with the given instance. Every Camel command takes CamelJBangMain. Citrus works around this with a second, empty constructor on each command.

Suggestion: when there is no accessible no-arg constructor, generate the metadata anyway, with a newInstance() that throws UnsupportedOperationException. Maybe also a note (not an error), so create(Class) users know. We tried exactly that (a few lines in validate and CodeGenerator), and it works.

2. Mixin fields that the generated class cannot reach

The generated code reads mixin fields directly, so these do not compile:

  • an @Option field that is package-private, in a mixin class of another package than the command:

    package org.example.commands;
    public class DurationMixin {
        @Option(name = "duration") long duration;      // package-private
    }
    
    package org.example.commands.action;
    public class LogAction implements Command<CommandInvocation> {
        @Mixin DurationMixin duration;
    }
    LogAction_AeshMetadata.java: duration is not public in org.example.commands.DurationMixin; cannot be accessed from outside package
    
  • a private @Mixin field:

    UpdateRun_AeshMetadata.java: updateMixin has private access in ...UpdateRun
    

A private @Option field on the command itself works: the processor warns ("requires reflective access at runtime") and goes through the Accessor. Mixin fields could use the same fallback, at least for fields the generated class cannot reach. Even an error from the processor, naming the field, would be better than a compile error in generated code.

3. Subclasses that the generated class cannot reach

@CommandDefinition is @Inherited, so getElementsAnnotatedWith also returns every subclass, including private nested classes in tests:

class CliHelpGoldenTest {
    private static class TestMain extends CamelJBangMain { ... }   // CamelJBangMain has @CommandDefinition
}
CliHelpGoldenTest_TestMain_AeshMetadata.java: CliHelpGoldenTest.TestMain has private access in CliHelpGoldenTest

Suggestion: skip (with a note) classes that the generated metadata class cannot reach: private nested classes, or nested classes of a private class. The lookup walks the class hierarchy anyway, so they would fall back to the provider of their superclass, or to reflection. We now turn the processor off for test compilation (<proc>none</proc>), but other projects may not know why their tests stop compiling.


Context: Camel adds the processor in Camel 4.25.0, after aesh 3.19.2 (where MetadataProviderRegistry.useOnly already took 30-40 ms off the CLI, thank you!). Happy to send a PR for 1, and for the others if that helps.

Drafted by Claude Code on behalf of Claus Ibsen

Activity

  1. davsclaus commented on Nov 19, 2026

    @davsclaus
    ContributorAuthor

    Camel side: https://issues.apache.org/jira/browse/CAMEL-27232 (4.25.0)

    Claude Code on behalf of Claus Ibsen

  2. davsclaus commented on Nov 19, 2026

    @davsclaus
    ContributorAuthor

    What we changed for the prototype, for a quick look (against v3.19.2):

    1. No no-arg constructor. This is the only part we patched in aesh: drop the error in AeshAnnotationProcessor.validate, and in CodeGenerator.generate generate a newInstance() that throws when there is no accessible no-arg constructor:

    --- a/aesh-processor/src/main/java/org/aesh/processor/AeshAnnotationProcessor.java
    +++ b/aesh-processor/src/main/java/org/aesh/processor/AeshAnnotationProcessor.java
    @@ -210,11 +210,8 @@ public class AeshAnnotationProcessor extends AbstractProcessor {
    -        if (!hasNoArgConstructor) {
    -            messager.printMessage(Diagnostic.Kind.ERROR,
    -                    "Command class must have an accessible no-arg constructor", element);
    -            valid = false;
    -        }
    +        // a command without a no-arg constructor gets a newInstance() that throws; the application
    +        // passes its own instances
    --- a/aesh-processor/src/main/java/org/aesh/processor/CodeGenerator.java
    +++ b/aesh-processor/src/main/java/org/aesh/processor/CodeGenerator.java
    @@ -192,7 +192,20 @@ final class CodeGenerator {
             sb.append("    public ").append(simpleName).append(" newInstance() {\n");
    -        sb.append("        return new ").append(simpleName).append("();\n");
    +        boolean noArg = false;
    +        for (javax.lang.model.element.Element enclosed : commandElement.getEnclosedElements()) {
    +            if (enclosed.getKind() == javax.lang.model.element.ElementKind.CONSTRUCTOR
    +                    && ((ExecutableElement) enclosed).getParameters().isEmpty()
    +                    && !enclosed.getModifiers().contains(Modifier.PRIVATE)) {
    +                noArg = true;
    +            }
    +        }
    +        if (noArg) {
    +            sb.append("        return new ").append(simpleName).append("();\n");
    +        } else {
    +            sb.append("        throw new UnsupportedOperationException(\"").append(simpleName)
    +                    .append(" has no no-arg constructor\");\n");
    +        }

    (hasNoArgConstructor is already computed in validate, so it could be passed on instead of looking again.)

    2. Mixin fields. No aesh patch: we made the fields wider in Camel (the 4 mixins' fields public, the private @Mixin field package-private). Where we think it goes wrong:

    • CodeGenerator.isPrivateField only checks private. A package-private (or protected) field of a mixin in another package than the command is just as unreachable from the generated class, which lives in the command's package. Checking "reachable from the package of the generated class" instead of "private" would send those through the reflective accessor as well.
    • For an option of a mixin (FieldAccessorInfo with a mixinFieldName), info.isPrivate is about the option field only. When the option field is reachable but the @Mixin field on the command is private, the generated setter still writes ((Cmd) inst).updateMixin.x (around CodeGenerator line 1364, and the getter and resetter next to it). generateMixin already handles the private mixin field itself; the option accessors need to do the same.

    3. Subclasses it cannot reach. No aesh patch: we turned the processor off for our test compilation. A check in validate (or before it, in process) to skip a class that the generated class cannot reach would do it: a private nested class, or a nested class of a private class. Since @CommandDefinition is @Inherited, every subclass ends up there, also ones that never meant to be commands of their own.

    With these, plus MetadataProviderRegistry.useOnly(new InternalCommandMetadataRegistry(), new _AeshMetadataRegistry()), all 158 Camel commands get metadata, and our help and parse golden files are the same as with the annotations.

    Claude Code on behalf of Claus Ibsen

  3. davsclaus commented on Nov 19, 2026

    @davsclaus
    ContributorAuthor

    An update on point 1: on the Camel side we decided to give our commands a no-arg constructor (the main object is set after creation), so Camel no longer needs point 1, and we can use the released aesh-processor 3.19.2. For points 2 and 3 we make the mixin fields reachable and turn the processor off for our test compilation.

    So nothing here blocks Camel any more. We still think all three are worth fixing for other aesh users: an application that passes its own command instances (point 1), or has a mixin in another package (2) or a private nested subclass (3), gets a compile error inside generated code today, which is hard to understand.

    Claude Code on behalf of Claus Ibsen

  4. davsclaus commented on Nov 19, 2026

    @davsclaus
    ContributorAuthor

    Thanks Ståle, that's what we did in the end (apache/camel#30386, on top of aesh 3.19.2):

    • the private option fields of our core commands are now package-private
    • the mixins that commands in other packages use have public option fields
    • the processor gives zero "requires reflective access" warnings for our core commands

    camel ps went from 103 to 90 ms (and from 144 ms before 3.19.2).

    A small idea: an option to name the generated registry class, e.g. -AaeshRegistryClass=org.example.MyMetadataRegistry. Today _AeshMetadataRegistry lands in the package of the first command the processor sees, and that package can differ between machines. So we don't reference it. Instead we look up X_AeshMetadata by name, which costs a Class.forName and a reflective constructor per command. With a fixed name we could register the generated registry and do it with plain new.

    Claude Code on behalf of Claus Ibsen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions