Repository navigation
Answer the persistent class's metaclass from an initialized proxy - #17172
Open
jdaugherty wants to merge 1 commit into
Open
jdaugherty wants to merge 1 commit into
jdaugherty wants to merge 1 commit into
Conversation
A to-one association typed as the root of a hierarchy is loaded as a proxy of the root class. Once the
proxy was initialized, getMetaClass() was forwarded to the entity behind it and answered the metaclass
of the entity's own class, a subclass of the one the proxy extends. Dynamic Groovy then invoked that
subclass's methods on the proxy, which is not an instance of the subclass, so calling anything the
subclass implements or overrides failed with a ClassCastException or an IllegalArgumentException
("object is not an instance of declaring class").
The Groovy-aware interceptor now answers the persistent class's metaclass whether or not the proxy is
initialized, as it already did for an uninitialized proxy. Calls then go through the proxy's own
methods, which dispatch to the entity.
✅ All tests passed ✅🏷️ Commit: 7956991 Learn more about TestLens at testlens.app/docs. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A to-one association typed as the root of a hierarchy is loaded as a proxy of the root class. Dynamic Groovy code cannot call what a subclass implements or overrides through that proxy once it is initialized:
Through
InvokerHelper, as in a Spock condition, the failure isIllegalArgumentException: object is not an instance of declaring class.Groovy resolves a call on a
GroovyObjectthrough the receiver'sgetMetaClass(). The Groovy-aware interceptor answers the persistent class's metaclass while the proxy is uninitialized, but once it is initialized the call is forwarded to the entity, which answers the metaclass of its own class,Circle. Groovy then invokesCircle's methods on the proxy, which extendsShapeand is not aCircle. Methods declared only onShapework, because the proxy is an instance ofShape.ByteBuddyGroovyInterceptornow answers the persistent class's metaclass whether or not the proxy is initialized, in both the Hibernate 5 and the Hibernate 7 module. Calls then go through the proxy's own methods, which dispatch to the entity, so the subclass's implementation runs.GroovyProxyInterceptorLogic.handleMetaClass()holds the decision, andhandleUninitialized()shares its test for a metaclass request.This is not a regression in 8.0: the same spec fails on 7.0.x, where the stock Hibernate proxy forwards
getMetaClass()in the same way. 7.x has no Groovy-aware interceptor to make the change in, so this PR targets 8.0.x only.A method that only the subclass declares, with nothing on the persistent class, is still not callable through the proxy, as the persistent class's metaclass does not have it.
Validation
InitializedSubclassProxySpec, added to both modules: all three features fail before the change on Hibernate 5 with classic call sites and with-PgrailsIndy=true, and on Hibernate 7 with classic call sites; all three pass after it in both modules with classic call sites. Not run after the change with-PgrailsIndy=true.GroovyProxyInterceptorLogicSpec: new features forhandleMetaClass()in both modules../gradlew :grails-data-hibernate5-core:check: 1,225 tests, no failures, 36 skipped../gradlew :grails-data-hibernate7-core:check: 4,715 tests, no failures, 26 skipped.git diff --check../gradlew build --rerun-tasks.Contributor Checklist
Issue and Scope
7.0.x): Bug fixes only. No new features or API changes.7.1.x): New features are welcome, but breaking existing APIs must be avoided.8.0.x): Reserved for major changes. Breaking API changes are permitted.Code Quality
./gradlew build --rerun-tasks.checktasks passed, as listed above. CI will run the full build.checktasks passed.Licensing and Attribution
Documentation