Skip to content

Answer the persistent class's metaclass from an initialized proxy - #17172

Open
jdaugherty wants to merge 1 commit into
apache:8.0.xfrom
jdaugherty:fix/initialized-proxy-metaclass
Open

jdaugherty wants to merge 1 commit into
apache:8.0.xfrom
jdaugherty:fix/initialized-proxy-metaclass

Conversation

@jdaugherty

Copy link
Copy Markdown
Contributor

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:

@Entity
abstract class Shape {
    String name
    static transients = ['kind']
    abstract String getKind()
    String describe() { "a shape named $name" }
}

@Entity
class Circle extends Shape {
    String getKind() { 'circle' }
    String describe() { "a circle named $name" }
}

@Entity
class Drawing {
    Shape shape
}

Shape shape = Drawing.first().shape   // a proxy of Shape whose entity is a Circle
shape.name                            // fine: declared on Shape
shape.kind                            // ClassCastException: Shape$HibernateProxy$... cannot be cast to Circle
shape.describe()                      // the same

Through InvokerHelper, as in a Spock condition, the failure is IllegalArgumentException: object is not an instance of declaring class.

Groovy resolves a call on a GroovyObject through the receiver's getMetaClass(). 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 invokes Circle's methods on the proxy, which extends Shape and is not a Circle. Methods declared only on Shape work, because the proxy is an instance of Shape.

ByteBuddyGroovyInterceptor now 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, and handleUninitialized() 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.
  • The same spec on 7.0.x, adapted to that branch's test manager: all three features fail, with invokedynamic and with classic call sites.
  • GroovyProxyInterceptorLogicSpec: new features for handleMetaClass() 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.
  • Not run: ./gradlew build --rerun-tasks.

Contributor Checklist

Issue and Scope

  • This PR is linked to an existing issue that has been acknowledged or approved by the project team. If no approved issue exists, please give background on why this change is necessary. Tickets are preferred for release change log history.
    • No issue: the background is above.
  • This PR addresses the complete scope of the linked issue. Partial implementations or unfinished work should not be submitted for review.
  • This PR contains a single, focused change. Unrelated changes should be submitted as separate pull requests.
  • This PR targets the correct branch for the type of change:
    • Patch release branches (e.g., 7.0.x): Bug fixes only. No new features or API changes.
    • Minor release branches (e.g., 7.1.x): New features are welcome, but breaking existing APIs must be avoided.
    • Major release branches (e.g., 8.0.x): Reserved for major changes. Breaking API changes are permitted.

Code Quality

  • I have added or updated tests that cover the changes introduced in this PR. All code contributions are expected to include appropriate test coverage.
  • I have verified that all existing tests pass by running ./gradlew build --rerun-tasks.
    • The affected modules' check tasks passed, as listed above. CI will run the full build.
  • My code follows the project's code style guidelines. The affected modules' check tasks passed.
  • This PR does not include mass reformatting, style-only changes, or large-scale refactoring unless it was explicitly approved in the linked issue. Unsolicited reformatting will not be accepted.

Licensing and Attribution

  • All contributed code is provided under the Apache License 2.0, and new source files include the appropriate Apache license header.
  • I have the necessary rights to submit this contribution and confirm it is my own original work (see Legal Notice).

Documentation

  • If this PR introduces user-facing changes, I have included or updated the relevant documentation. — Not applicable: no documented behavior changes.
  • If this PR adds a new feature, I have updated the What's New section of the Grails Guide. — Not applicable: bug fix.
  • If this PR introduces breaking changes or changes that require user action during an upgrade, I have updated the Upgrade Notes for the corresponding version in the Grails Guide. — Not applicable: no user action is required.
  • The PR description clearly explains what was changed and why.

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.
@testlens-app

testlens-app Bot commented Nov 25, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 7956991
▶️ Tests: 104346 executed
⚪️ Checks: 95/95 completed


Learn more about TestLens at testlens.app/docs.

This branch has not been deployed

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant