[code-reflection] RFR: Method code model uniquness [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Apr 29 14:13:07 UTC 2025


On Tue, 29 Apr 2025 12:10:34 GMT, Mourad Abbay <mabbay at openjdk.org> wrote:

>> Multiple `Method` objects that represent the same java method, have different code models. This is because every time we call `Class.getMethod` or `Class.getDeclaredMethod` we get a newly created instance. One possible solution is to attach the model to the root `Method` object.
>
> Mourad Abbay has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Ensure instances that reflect the same method have the same model

src/java.base/share/classes/java/lang/reflect/Method.java line 233:

> 231:     /* package */
> 232:     Optional<?> setCodeModelIfNeeded(Function<Method, Optional<?>> modelFactory) {
> 233:         Optional<?> localRef = root.codeModel;

I was looking at `Method` and found this method that does more or less a similar operation:


// Accessor for generic info repository
    @Override
    MethodRepository getGenericInfo() {
        var genericInfo = this.genericInfo;
        if (genericInfo == null) {
            var root = this.root;
            if (root != null) {
                genericInfo = root.getGenericInfo();
            } else {
                genericInfo = MethodRepository.make(getGenericSignature(), getFactory());
            }
            this.genericInfo = genericInfo;
        }
        return genericInfo;
    }


Should we copy/reuse same logic?

-------------

PR Review Comment: https://git.openjdk.org/babylon/pull/415#discussion_r2066617876


More information about the babylon-dev mailing list