Skip to content

Evaluate expressions in the annotation metadata of intercepted methods that also have placeholders - #14545

Merged
sdelamo merged 5 commits into
5.2.xfrom
expressions-with-placeholders
Nov 24, 2026
Merged

sdelamo merged 5 commits into
5.2.xfrom
expressions-with-placeholders

Conversation

@sdelamo

@sdelamo sdelamo commented Nov 20, 2026

Copy link
Copy Markdown
Member

An interceptor reads #{...} evaluated expressions as their literal text from context.getAnnotationMetadata() when the annotations of the intercepted method, or of its class, also contain a ${...} property placeholder. Without any placeholder the expression is evaluated.

Reproduction

@Singleton
class Test {

    @Traced // any @Around advice
    @Label("#{ 'eval' + 'uated' }")
    @Note("${test.note}")
    public void run() {
    }
}

In the interceptor bound to @Traced:

context.getAnnotationMetadata().stringValue(Label.class);
// expected: Optional[evaluated]
// actual:   Optional[#{ 'eval' + 'uated' }]

The same happens when the placeholder is in another member of the same annotation (@Label(value = "#{ ... }", scope = "${test.scope}")) or in an annotation of the class. Both the full metadata and getDeclaredMetadata() return the unevaluated value, with @Around and with @Around(proxyTarget = true).

Cause

The proxy builds its own executable methods and DefaultInterceptorRegistry configures each of them with the bean context first, which wraps the metadata in an EvaluatedAnnotationMetadata, and then with the environment when the metadata has property placeholders. DispatchedExecutableMethod.configure(Environment) rebuilds the metadata from the method reference, so it replaced the expression aware metadata with a bare environment aware one. Nothing wrapped it again, and InterceptorChain found no EvaluatedAnnotationMetadata to copy with the invocation arguments.

Executable methods obtained from the bean definition were not affected: getExecutableMethodByIndex configures the environment first and the bean context second.

Changes

DispatchedExecutableMethod remembers the bean context it was configured with. configure(Environment) wraps the environment aware metadata again when a bean context was already configured. The result is expression aware metadata over environment aware metadata, the order the bean definition path already produced, whichever of the two is configured first.

The change is internal to AbstractExecutableMethodsDefinition. InterceptorChain still takes a copy with withArguments for each invocation, so arguments stay with their own invocation.

Tests

EvaluatedAnnotationMetadataInterceptedSpec compiles intercepted beans and checks what the interceptor reads from its invocation context, for @Around and @Around(proxyTarget = true):

  • an expression without any placeholder (passed before the change)
  • an expression with a placeholder in another annotation of the method
  • an expression with a placeholder in another member of the same annotation
  • an expression with a placeholder in an annotation of the class
  • an expression that references a method argument, over two invocations

Each case asserts that the expression is evaluated, that the placeholder is resolved, and that findAnnotation and stringValue agree on the full metadata and on getDeclaredMetadata(). Before the change every case with a placeholder failed.

Ran locally, all passing: :micronaut-inject:test (575 tests), :micronaut-inject-java:test for the AOP, expression, *EvaluatedAnnotationMetadata* and *EnvironmentAnnotationMetadata* specs (3718 tests), :micronaut-inject-groovy:test for the AOP specs (309 tests), the AOP, expression and scheduling tests of :test-suite:test (51 tests), and :micronaut-inject:japiCmp.

Not changed

A single member that mixes both, such as "${test.prefix:pre}-#{ 'x' }", is compiled as an expression as a whole: the expression is evaluated and the placeholder is kept as written (${test.prefix:pre}-x). This was already the behaviour and the spec records it.

Related: #14466, #14172.

🤖 Generated with Claude Code

…s that also have placeholders

The executable method of a proxy is configured with the bean context, which
makes its annotation metadata expression aware, and then with the environment
when the metadata has property placeholders. Configuring the environment
rebuilt the metadata from the method reference and dropped the expression
aware wrapper, so an interceptor read `#{...}` expressions as their literal
text whenever the annotations of the method or of its class also held a
`${...}` placeholder.

The method now remembers the bean context it was configured with and wraps
the environment aware metadata again, so the result is expression aware over
environment aware metadata whichever of the two is configured first.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 09:41

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

sdelamo and others added 4 commits November 20, 2026 11:05
…data change

- Extract the repeated context build and annotation type lookup of
  EvaluatedAnnotationMetadataInterceptedSpec into helpers.
- Drop an assertion that cannot fail and a block that repeated earlier
  assertions from the per-invocation arguments feature, and state what the
  remaining assertions prove.
- Rename the parameter of configureEvaluatedExpressions so it no longer
  shadows the beanContext field.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… write

configure(Environment) assigned the environment aware metadata and then
wrapped it in the expression aware one, so an invocation running while a
proxy instance was built could see the metadata without its expression
wrapper. Build the final metadata locally, configure it with the bean
context, and assign it once. configure(BeanContext) does the same.

Document on the beanContext field why it is kept: the interceptor registry
configures the bean context first, getExecutableMethodByIndex the
environment first, and the expression aware metadata must wrap the
environment aware one in both orders.

Cover a prototype bean, whose shared executable method is configured again
for every instance, and pin the bean definition path.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ntercepted metadata spec

- An intercepted method with a placeholder evaluates the expression of a
  parameter annotation, which needs the bean context its configure passes on.
- Two contexts over the same compiled classes each resolve their own
  properties, so the remembered bean context never crosses contexts.
- Mark the mixed member feature as characterising a known limitation.
- Reword the single assignment comment so it claims no more than the plain
  field guarantees.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The executable methods of a definition without placeholders or expressions
are held in a static field shared by every context in the JVM, and the
interceptor registry configures them with each context. Storing the bean
context there pinned the last context, even after it was closed. Only
configure(Environment) reads the field, and only for placeholder metadata,
whose definitions get a copy per bean definition: keep it only then.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@sdelamo

sdelamo commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Automated review pipeline

Four fix commits were pushed to this branch. No high severity finding is left open.

Base branch

The branch already contained everything on origin/5.2.x; no merge was needed.

Existing review conversations

None on the pull request.

Brooks sweep (commit e18af2d)

Eight findings, three fixed:

  • Test duplication: the spec now uses buildInterceptedContext and annotationType helpers instead of repeating the compile and class loading in every feature.
  • Coverage illusion: removed !first.is(second) (cannot fail, withArguments always returns a new instance) and a duplicated assertion block in the arguments feature; the remaining assertions state what they prove.
  • Readability: the helper parameter no longer shadows the new beanContext field.

Declined: the instanceof AbstractEnvironmentAnnotationMetadata assertion (it pins the layering the PR guarantees), splitting the shared SOURCE fixture (a restructure), the mixed-member characterisation test (recorded as deliberate). Out of scope, pre-existing: non-volatile fields of the shared DispatchedExecutableMethod reconfigured on every proxy build, and getReturnType() caching metadata that a later configure(...) makes stale.

Round 1, code quality (commit 2f58ea1)

Four findings, all fixed:

  • configure(Environment) wrote the bare environment-aware metadata and then the wrapped one, so a concurrent invocation on a prototype proxy could briefly see the unwrapped metadata. It now builds the final value locally and assigns the field once, through a private static evaluated(...) helper also used by configure(BeanContext).
  • No test ran the reconfiguration path: added a @Prototype bean whose proxy is built twice.
  • The bean-definition path (getExecutableMethodByIndex, environment first) was not pinned: added a feature reading the bean definition's executable method.
  • The invariant was split across two comments: it is now stated once on the beanContext field, naming both call sites and their opposite orders.

Round 2, usability

Skipped: the diff touches a private inner class of the inject runtime and a test, with no template, copy, API or CLI surface.

Round 3, test quality (commit a2f4d99)

Six findings, three fixed:

  • Added a feature for an expression on a method parameter annotation, which exercises the argumentsAnnotationsWithExpressions loop (a mutation check confirmed it fails when the loop is disabled).
  • Added a feature building two application contexts from the same compiled classes with different property values, asserting each interceptor sees its own.
  • Added a comment marking the mixed-member feature as a characterisation of a known limitation.

Declined: a latch-based concurrency test (the window is a few instructions wide, so the test could not fail even on the old code); instead the "single write" comment no longer claims more than the non-volatile field guarantees. Also declined: a nested-wrapper assertion that cannot fail (EvaluatedAnnotationMetadata does not extend AbstractEnvironmentAnnotationMetadata), and sharing contexts across features (would couple them through the interceptor's recorded lists for a spec that runs in about 5 s).

Round 4, performance

No findings in the PR. Recorded, pre-existing and out of scope: every proxy build creates a new MethodAnnotationMetadata, discarding the warmed placeholder cache; only visible for prototype or request scoped proxies built at a high rate.

Round 5, application security (commit d440d16)

One low finding, fixed: configure(BeanContext) stored the context in every DispatchedExecutableMethod, including the static instance shared JVM-wide by definitions that are not context dependent, which pinned the last context (even a closed one) from a static field. The context is now stored only when the method metadata has property placeholders, in which case the definition is always a per-context copy. A feature checks that an expression-free intercepted method keeps no context. No widening of expression evaluation scope and no cross-invocation or cross-context leak was found.

Round 6, dependencies, build and configuration

No findings: no build file, dependency, workflow or configuration is touched, and the spec compiles only a static in-test source string.

Final check

./gradlew :micronaut-inject:check :micronaut-inject-java:spotlessCheck plus the new spec: 575 inject tests and 22 spec tests pass, spotless and japicmp pass, working tree clean. The full inject-java suite beyond the annotation and aop packages, and the inject-groovy and test-suite modules, were not re-run after the fix commits.

Left for the author

  • The transient state while two proxy instances of the same definition are built concurrently (non-volatile fields, wrapper reconfigured in place) predates this PR and is narrowed, not removed.
  • getReturnType() caching stale metadata predates this PR.

🤖 Generated with Claude Code

@sdelamo
sdelamo requested a review from dstepanov November 20, 2026 10:47
@sonarqubecloud

Copy link
Copy Markdown

@dstepanov dstepanov left a comment

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.

The fix is right, and it gives the same layering whichever configure call comes first: expression-aware metadata over environment-aware metadata. Things I checked:

  • Methods without placeholders behave as before. evaluated() is the old configure(BeanContext) body, and beanContext is only kept when the method has property expressions, so methods without placeholders don't start holding a context.
  • No new retention. For placeholder methods the old metadata already held the environment, and the EvaluatedAnnotationMetadata held the context. Keeping beanContext adds nothing new.
  • Environment first, then the context, settles correctly. When a method is configured with the environment and then the context, configure(Environment) may wrap with the context from an earlier configure(BeanContext) (shared definition methods). The following configure(BeanContext) then re-configures the same EvaluatedAnnotationMetadata with the current context, so it ends up with the right one.
  • Tests: the spec covers both @Around modes, a placeholder in another member, a placeholder on the class, and getDeclaredMetadata().

CI is green.


Generated by Claude Code

@sdelamo
sdelamo merged commit 9a88cb1 into 5.2.x Nov 24, 2026
18 checks passed
@sdelamo
sdelamo deleted the expressions-with-placeholders branch November 24, 2026 09:02
@github-project-automation github-project-automation Bot moved this from Backlog to Done in 5.2.3 Release Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants