zbendhiba commented on PR #30391:
URL: https://github.com/apache/camel/pull/30391#issuecomment-7006043314
## Half of this producer is reimplemented bean binding. Is there a better
way?
I would like an opinion from people who know `camel-bean` better than I do,
because I am not
convinced the shape I ended up with is the right one.
`LangChain4jServiceProducer` is 540 lines. Roughly 250 of them are parameter
binding that
`camel-bean` already knows how to do: a `computeParameterBindings()` pass, a
`ParameterBinding`
interface, three implementations (`UserMessageBinding`,
`TemplateVariableBinding`,
`MemoryIdBinding`), a `findAnnotation` helper, a per-message resolution
loop, and
`InvocationTargetException` unwrapping around
`ObjectHelper.invokeMethodSafe`.
Method and annotation discovery do go through `camel-bean`, and that part
works well. `BeanInfo`
walks the whole type hierarchy, so an annotation declared on an interface is
found even when the
registered object is a class a framework generated. Binding is the part I
could not delegate.
### What I looked at first
`BeanInfo.createParameterUnmarshalExpressionForAnnotation()` is an `if/else`
chain over Camel's
own annotations, with one generic escape hatch at the end:
```java
LanguageAnnotation languageAnnotation =
annotation.annotationType().getAnnotation(LanguageAnnotation.class);
```
`LanguageAnnotation` is `@Target({ElementType.ANNOTATION_TYPE})`, so it has
to sit on the
annotation type itself. `@UserMessage`, `@V` and `@MemoryId` are declared in
a LangChain4j jar,
so I cannot annotate them.
`ParameterMappingStrategy` is discoverable, `ParameterMappingStrategyHelper`
does look it up in
the registry. But its only method is
```java
Expression getDefaultParameterTypeExpression(Class<?> parameterType);
```
It dispatches on the parameter type alone, with no access to the annotation
instance or its
value. Here `@V("city")` and `@V("country")` are both `String`, so nothing
type-based can tell
them apart. And only one instance per application is honoured, which makes
it the wrong scope
for a component anyway.
That is as far as I got. If there is an existing mechanism I walked past, I
would much rather use
it than keep the code I wrote.
### One way out, not necessarily the right one
If there is genuinely no hook, a small binding SPI consulted **last** would
do it, only for
annotations `camel-bean` has already declined, so built-in binding cannot be
hijacked and
existing behaviour is unchanged by construction:
```java
Expression createExpression(CamelContext camelContext, Method method,
Class<?> parameterType, Annotation annotation);
```
I wrote that up so the discussion has something concrete to push against:
the dispatch point, the
resolution scope, the propagation to superclasses, a pre-existing
`BeanInfoCacheKey` cache issue
it surfaces, the compatibility story and a test plan.
[Proposal: a pluggable parameter annotation binding SPI in
camel-bean](https://gist.github.com/zbendhiba/90c470301741379fdeb8535f4207e78e)
I am not attached to it. It is one sketch from someone who has read this
code for a few days, not
for years. If the answer is "an SPI there is a bad idea because X", that is
a useful answer, and X
is the thing I want to learn.
Open questions, including the first one which matters most:
1. Is there a better angle than a new SPI? Extending
`ParameterMappingStrategy` with an
annotation-aware method, exposing the binding differently, or something
else entirely?
2. Is a binding SPI in `camel-bean` acceptable in principle, or has this
been discussed and
rejected before?
3. Constructor-scoped resolution, or registry discovery? I left discovery
out because
`findByType` returns a `Set` and the order between two implementations
handling the same
annotation would be undefined. `ParameterMappingStrategyHelper` solves
that by accepting one
instance and warning above that, which may be the precedent to follow.
4. `BeanInfoCacheKey` holds only type, instance and explicit method, so two
`BeanInfo` instances
built with different `ParameterMappingStrategy` values already share a
cache entry today.
Latent, since nobody passes a custom strategy. Worth a separate bug, or
did I misread it?
### Not asking to block this PR
The component works as it is, and the duplicated binding is contained in one
class. Whatever comes
out of this, it is a separate ticket, and adopting it here would be a third
one that deletes most
of what I described above.
But I would rather ask before this lands than add a fourth copy of parameter
binding to the
codebase and leave it there.
_Claude Code on behalf of @zbendhiba_
--
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]