Skip to content

8.0 (fix): Cache with the class's own cache manager where a parameter is named like it - #17151

Merged
codeconsole merged 7 commits into
apache:8.0.xfrom
codeconsole:fix/cache-shadowed-properties-8.0.x
Nov 25, 2026
Merged

codeconsole merged 7 commits into
apache:8.0.xfrom
codeconsole:fix/cache-shadowed-properties-8.0.x

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The code the cache annotations generate read the class's cache manager and key generator by their bare names, grailsCacheManager and customCacheKeyGenerator. A parameter of the annotated method with one of those names hides the name inside the method. So a method like this cached in the cache manager it was passed, not the class's own:

@Cacheable('books')
Book find(Long id, CacheManager grailsCacheManager) { ... }

A parameter named customCacheKeyGenerator that was passed null made every call that makes a key fail with a NullPointerException. So did a parameter named grailsCacheManager passed null to a method whose cache the cache manager doesn't have, in the report of that missing cache.

The generated code now reads both as properties of this: this.grailsCacheManager and this.customCacheKeyGenerator, which a parameter cannot hide. That is what the bare names meant without such a parameter:

  • A field the class declares: where the class declares its own field of that name, private, protected or as a property, that field is used.
  • Otherwise: each property resolves through its getter. A class can define its own getGrailsCacheManager() or getCustomCacheKeyGenerator(). The corresponding GrailsCacheManagerAware getter supplies the value by default, including when a superclass only declares the field, as in 8.0.0.
  • Everywhere it is read: the cache lookup, the check that there is a cache manager, the key, and the report of a missing cache.

declareCache now takes any expression for the cache manager. The signature taking a VariableExpression stays, as do the property-name constants, for subclasses compiled against 8.0.0.

Tests

ShadowedPropertiesSpec compiles cache-aware classes statically and dynamically.

  • Parameters of both names: @Cacheable, @CachePut, @CacheEvict by key and of every entry take them and act on the class's cache manager, not the one passed in, with a null key generator passed. Before the change both modes failed. With only the key generator read the new way, they cached in the cache manager passed in.
  • A missing cache: a method whose cache the class's cache manager doesn't have, called with null and with another cache manager, runs uncached and reports the class's cache manager once.
  • Classes that declare their own members:
    • The rows: a private, protected or property cache manager, a private or property key generator, or a private, protected or property cache manager in a superclass.
    • The calls: each annotation and a missing cache, without parameters of those names and with them, passed a cache manager of another class and another key generator.
    • The checks: where each caches and puts, which entries an eviction by key removes, which key generator made each key, and which cache manager a missing cache is reported for.

All grails-cache tests pass, as do the grails-test-examples/cache app's tests and the CachingSpec of grails-test-examples/app1.

…ike it

The code the cache annotations generate read the cache manager and the
key generator by their property names, grailsCacheManager and
customCacheKeyGenerator. A parameter of the annotated method with one of
those names hides the property in the method, so the method cached in
the cache manager passed to it, or failed with a NullPointerException
where the key generator passed was null. Both are now read through their
getters, which a parameter cannot hide.

ShadowedPropertiesSpec compiles a class, statically and dynamically,
whose @Cacheable, @cACHEpUT and @CacheEvict methods take parameters of
both names, and checks they act on the class's own cache manager; before
the change both modes failed, and with only the key generator read
through its getter they cached in the cache manager passed.
matrei
matrei previously requested changes Nov 23, 2026

@matrei matrei 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.

Review of #17151 at 41e5267ac2 (base 8.0.x): one commit on 5bdcbc2e82. Since #16980 merged, it conflicts with the current tip, fca95de32c. The conflict is only in the imports of AbstractCacheTransformation: keep both block and callThisX.

What I ran, on Java 21:

  • The fix, A/B at this head: with the four transformations of 8.0.x put back, both features of ShadowedPropertiesSpec fail, statically and dynamically, with the NullPointerException on the key generator passed as null. With the PR they pass.
  • A merge with the current 8.0.x, with the conflict resolved as above: the grails-cache suite, without the build cache, 124 tests, no failures. That includes the 2 features of ShadowedPropertiesSpec, the 4 of MissingCacheSpec and the 70 of GrailsCacheManagerAliasMatrixSpec. A probe of the case in point 1 fails there.
  • That merge with the line from point 1: the same 124 tests and the probe pass, as do the grails-test-examples/cache app's test (19) and integrationTest (25), and the CachingSpec of grails-test-examples/app1 (20).

@codeconsole, thanks for splitting this out. The change is right; it only needs the merge with #16980 finished.

1. The missing-cache report still reads grailsCacheManager by name

#16980 merged first, so this is the PR that switches the report over, as its description says. After the merge, whereCacheExists still passes varX(GRAILS_CACHE_MANAGER_PROPERTY_NAME) to MissingCacheReporter.report (AbstractCacheTransformation.groovy:200 on 8.0.x). So in a method whose cache the class's cache manager doesn't have, a parameter named grailsCacheManager is what gets reported. A probe at the merge, statically and dynamically:

  • passed null, the call fails with NullPointerException: Cannot invoke "Object.getClass()" because "cacheManager" is null, from MissingCacheReporter.report;
  • passed another cache manager, the warning names that one (NoOpCacheManager), not the class's, and the reporter records the cache as reported for the wrong cache manager.

None of the existing tests reaches this, so the merge passes without the change. The fix is one line:

args(grailsCacheManagerExpression(), constX(cacheName), classX(classNode.plainNodeReference)))

A feature like this in ShadowedPropertiesSpec covers it. It fails without the line, in both modes, with the NullPointerException above, and passes with it:

    @Unroll
    void 'a cache the class\'s cache manager does not have is reported for that cache manager, compiled #mode'() {
        given: 'the class created as a bean beside its cache manager, which has no cache named missing'
        CacheManager cacheManager = new ConcurrentMapCacheManager('configured')
        GenericApplicationContext context = new GenericApplicationContext()
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context)
        context.registerBean('grailsCacheManager', CacheManager, { -> cacheManager } as Supplier<CacheManager>)
        context.registerBean('service', (Class<Object>) compile(mode))
        context.refresh()
        def service = context.getBean('service')
        LogCapture log = new LogCapture(MissingCacheReporter, Level.WARN)

        when: 'the method is called with no cache manager, then with another one'
        List<Object> results = [service.uncached(1, null, null), service.uncached(1, new NoOpCacheManager(), null)]

        then: 'it ran uncached both times, and the class\'s cache manager was reported once'
        results == [2, 2]
        service.calls == 2
        log.events*.formattedMessage == ["ShadowingService does not cache with missing: its cache manager, ${ConcurrentMapCacheManager.name}, ".toString() +
                'has no cache of that name, so the methods run uncached. Configure the cache in that cache manager.']

        cleanup:
        log?.close()
        context?.close()

        where:
        mode << ['statically', 'dynamically']
    }

with this method in ShadowingService:

    @Cacheable('missing')
    int uncached(int x, CacheManager grailsCacheManager, GrailsCacheKeyGenerator customCacheKeyGenerator) {
        calls++
        x * 2
    }

and imports of Level, NoOpCacheManager, LogCapture and MissingCacheReporter, as MissingCacheSpec has them.

2. FYI: 7.0.x has the same code

On 7.0.x, the four transformations and the trait are identical to those of 8.0.x before #16980. So the bug is there too, and this commit applies there as it is. Nobody has reported it, and a parameter with one of those names is rare, so I'm fine with fixing it on 8.0.x only. If you'd rather have it on 7.0.x, the merge-up into 8.0.x then needs the line from point 1.

Verified as correct

  • Both getters come from GrailsCacheManagerAware, which is unchanged, and a parameter can't hide a method called on this. AbstractMethodDecoratingTransformation compiles the generated method statically in both modes (compileMethodStatically), so where no parameter has one of those names, the old property read resolved to the same getters. The existing tests pass unchanged.
  • The signature of declareCache taking a VariableExpression stays, and so do the public constants, so a subclass compiled against 8.0.0 still links. After point 1, neither GRAILS_CACHE_MANAGER_PROPERTY_NAME nor GRAILS_CACHE_KEY_GENERATOR_PROPERTY_NAME is used any more; keeping them is right on a patch line.
  • It's a bug fix: nothing documents that a parameter picks the cache manager or the key generator, so it needs no upgrade note.
  • The one failure TestLens reports, RedirectWithAndWithoutParamsFunctionalSpec in hibernate7-app1 on the Groovy snapshot canary, is unrelated to grails-cache.

@jamesfredley jamesfredley 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.

Requested changes. A service with its own cache manager still caches in the bean of that name.

* @return the call of the getter on {@code this}
*/
protected static Expression grailsCacheManagerExpression() {
callThisX('getGrailsCacheManager')

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.

A service keeps its own cache manager in a field of that name, and a method parameter has the same name. A call passes a third manager. The entry is stored in the bean of that name, not in the service field. Use the service field.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, you're right, and it went further than a parameter. Where a class declares its own private or protected grailsCacheManager field, it has no getter of its own. So getGrailsCacheManager() is the trait's and returned the bean, even with no parameter of that name, where 8.0.0's bare name read the class's field. The same went for a private customCacheKeyGenerator field. A Groovy property was already right, since its own getter wins.

In 98ed17c the generated code reads this.grailsCacheManager and this.customCacheKeyGenerator instead. That's the class's own field where it declares one, otherwise the trait's getter, and a parameter can't hide it. Without a parameter, it matches 8.0.0 in every case I compared. That includes a superclass that declares the field, private, protected or as a property, which reads the bean in both.

ShadowedPropertiesSpec compiles each of those classes statically and dynamically, in f86a22c and 320d430. Each calls @Cacheable, @CachePut, @CacheEvict by key and of every entry, and a missing cache, without the parameters and with them, passed another cache manager and key generator. It checks where the entries land and which are evicted, which key generator made each key, and which cache manager is reported. Before 98ed17c the private and protected cache-manager rows and the private key-generator row fail in both modes.

…-properties-8.0.x

# Conflicts:
#	grails-cache/src/ast/groovy/org/grails/plugin/cache/compiler/AbstractCacheTransformation.groovy
…meter is named like it

The report of a cache the cache manager does not have, which came with
the merge of 8.0.x, read grailsCacheManager by its name, so a parameter
of that name was reported in place of the class's cache manager: passed
null, the call failed with a NullPointerException, and passed another
cache manager, that one was reported and remembered as reported. It now
reads the cache manager the way the rest of the generated code does.

ShadowedPropertiesSpec calls a method whose cache is missing with no
cache manager and with another one, statically and dynamically, and
checks the class's cache manager is reported once.
The code the cache annotations generate read the cache manager and the
key generator through the getters of GrailsCacheManagerAware, so a class
that declares its own private or protected grailsCacheManager field
cached in the bean of that name, with or without a parameter of that
name, and one with its own private customCacheKeyGenerator field never
used it. 8.0.0 read the bare names, which were the class's fields there.
They are now read as properties of this, which are the class's field
where it declares one, and otherwise the trait's getter, and which a
parameter cannot hide.

ShadowedPropertiesSpec compiles classes, statically and dynamically,
that declare a private, a protected or a property cache manager, a
private or a property key generator, or whose superclass declares a
private cache manager, and calls their methods with and without
parameters of those names, passed another cache manager and key
generator. It checks where they cache, which key generator makes the
keys, and which cache manager a missing cache is reported for. Before
the change the private and protected cache managers and the private key
generator failed, in both modes.
ShadowedPropertiesSpec's feature for a class that declares its own
cache manager or key generator also puts and evicts every entry, with
parameters of those names, and checks both act on the cache manager the
class means and that the put's key comes from the key generator it
means. With only @cACHEpUT and @CacheEvict reading the cache manager
through the trait's getter, the private and protected cache manager
rows fail.
…s protected field or property

ShadowedPropertiesSpec's feature for a class that declares its own
cache manager or key generator now calls each annotation without the
parameters and with them: @Cacheable, @cACHEpUT, @CacheEvict by key and
of every entry, and a missing cache. Each key comes from a key closure,
so the eviction by key is checked to remove just its entries, which run
again, while the ones put still come from the cache; the key generator
the class means is checked to have made every key, and the passed one
none. The cache manager passed is of a class of its own, so a report of
it is told apart from one of the bean in every row. Rows for a
superclass's protected field and property join the private one, and
read the bean, as in 8.0.0.

The feature for parameters of both names also evicts by key, passed a
null key generator, which failed with a NullPointerException while the
generated code read the key generator by its bare name.
@codeconsole

Copy link
Copy Markdown
Contributor Author

Thanks @matrei.

  • The merge: I merged 8.0.x in 54965a6 and kept both block and callThisX in the imports, as you said. 98ed17c then dropped callThisX (see below). The grails-cache suite passed on the merge.
  • Point 1: fixed in 766ae27. The missing-cache report reads the cache manager through grailsCacheManagerExpression(), and your feature is in ShadowedPropertiesSpec as you wrote it. It fails without the line in both modes and passes with it.
  • Following @jamesfredley's thread: in 98ed17c the helper and the key-generator read use this.grailsCacheManager and this.customCacheKeyGenerator instead of the getters. A class that declares its own private or protected field of either name got the trait's getter, so it cached in the bean, where 8.0.0 used its field. So the old read resolved to the getters only where the class declares no such field. Both constants are used again.
  • The spec: f86a22c and 320d430 test classes that declare those members, or whose superclass does. They call every annotation with and without the parameters, including an eviction by key passed a null key generator.
  • Tests: all grails-cache tests pass, as do the grails-test-examples/cache app's test and integrationTest and app1's CachingSpec.

@jamesfredley jamesfredley 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 items this review found have been addressed. A class uses its own cache manager, and a parameter of that name no longer hides it.

@sbglasius sbglasius 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.

This is an AI assisted review

Reading both through this fixes the shadowing cleanly, and the spec covers every annotation, both compile modes, and own vs. superclass fields. LGTM.

@matrei matrei 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.

Second round, at 320d430 (base 8.0.x). It merges cleanly with today's 8.0.x (99aedeb), which changed nothing in grails-cache after your merge.

Round 1's point is fixed: 766ae27 reports the class's cache manager, and the feature from my review is in. James's case also holds. In round 1 I wrote that the old read resolved to the same getters wherever no parameter has those names. That was wrong for a class that declares its own field. With the getters of 766ae27, a class with its own private or protected grailsCacheManager cached in the bean, and one with its own private customCacheKeyGenerator made its keys with the trait's generator, without any parameter of those names. 98ed17c fixes that. Thanks for following it up.

What I ran, on Java 21, with the build cache off:

  • The suite: the grails-cache tests at the head, 142, all pass.
  • ShadowedPropertiesSpec with the older transformations:
    • With those of 8.0.x, all 20 features fail.
    • With those of 766ae27, the 6 features for a private or protected cache manager field and a private key generator field fail.
  • A temporary spec that compiles 17 class shapes, statically and dynamically. Each one calls @Cacheable with a key closure and without, @CachePut, @CacheEvict by key and of every entry, and a method whose cache is missing. It calls them once without parameters and once with parameters named grailsCacheManager and customCacheKeyGenerator, passed another cache manager and another key generator. It records which cache manager each call acts on, which key generator makes the keys, and which cache manager is reported.
    • The shapes: besides the rows of ShadowedPropertiesSpec, it has a protected key generator field, a private static cache manager field, a getter the class declares for each of the two, a superclass's key generator fields and property, and a superclass's getter.
    • Without the parameters, the head gives the same results as 8.0.x in all 34 cases.
    • With the parameters, 8.0.x acts on the cache manager and the key generator passed. The head acts as it does without them.
    • With the getters of 766ae27, the 10 cases of a private, protected or static field went to the bean or to the trait's key generator.

CI: 89 checks pass, 9 skipped.

Optional

  1. The Javadoc of grailsCacheManagerExpression() and the description say "a field of that name the class declares, otherwise what GrailsCacheManagerAware holds, through its getter". A class that declares its own getGrailsCacheManager(), without a field, caches with what that getter returns, here and on 8.0.x. The same goes for getCustomCacheKeyGenerator(). So maybe: "otherwise its getter, which is the trait's unless the class declares its own".
  2. The propX import goes after params.

@codeconsole
codeconsole merged commit aa4de16 into apache:8.0.x Nov 25, 2026
9 of 88 checks passed
@testlens-app

testlens-app Bot commented Nov 25, 2026

Copy link
Copy Markdown

🔎 No tests executed 🔎

🏷️ Commit: 910124b
▶️ Tests: 0 executed
⚪️ Checks: 95/95 completed


Learn more about TestLens at testlens.app/docs.

@codeconsole

Copy link
Copy Markdown
Contributor Author

@matrei Thanks for the second review. Both optional suggestions are addressed in 910124b. The Javadoc and PR description now explain that property resolution can use a class-defined getter, with the trait providing the default getter. The propX import now follows params. Both changes are included in the squash merge aa4de16.

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.

4 participants