You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by rz...@apache.org on 2026/08/29 18:13:38 UTC

(openjpa) 01/01: [OPENJPA-2958] Add ImplHelper.registerUnenhancedInstance()

This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch OPENJPA-2958
in repository https://gitbox.apache.org/repos/asf/openjpa.git

commit 6691297c05b3565bef4579d38644142c9b0aab1a
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Sat Aug 29 20:03:32 2026 +0200

    [OPENJPA-2958] Add ImplHelper.registerUnenhancedInstance()
    
    BrokerImpl reached into the public mutable ImplHelper._unenhancedInstanceMap
    to register the persistence capable instance of an unenhanced one. Add a
    method for it and route all three write sites through it, so the registry
    is no longer written to from outside the class that owns it. Behaviour is
    unchanged: the method is the same overwriting put.
    
    The field is deprecated to point callers at the method. It stays public:
    its type is a reference type, so it is never inlined into callers, and
    making it private would turn into a NoSuchFieldError at runtime for
    anything compiled against 4.1.x rather than a compile error on upgrade.
    Making it private belongs in a major release.
---
 .../java/org/apache/openjpa/kernel/BrokerImpl.java |   2 +-
 .../java/org/apache/openjpa/util/ImplHelper.java   |  44 ++++++++-
 .../util/TestImplHelperUnenhancedInstance.java     | 102 +++++++++++++++++++++
 3 files changed, 144 insertions(+), 4 deletions(-)

diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
index c6915ba67..cbfa1f8ae 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
@@ -3094,7 +3094,7 @@ public class BrokerImpl implements Broker, FindCallbacks, Cloneable, Serializabl
             // embedded copy mapping so that getStateManagerImpl can find
             // the SM when the owner's enhanced field provides the raw record
             if (obj != null && meta.isRecord()) {
-                ImplHelper._unenhancedInstanceMap.put(obj, copy);
+                ImplHelper.registerUnenhancedInstance(obj, copy);
             }
 
             return sm;
diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java
index fbac49890..d6bd53ebc 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ImplHelper.java
@@ -61,7 +61,16 @@ public class ImplHelper {
     private static final Map _assignableTypes =
         new ConcurrentReferenceHashMap(ReferenceStrength.WEAK, ReferenceStrength.HARD);
 
-    // map of all new unenhanced instances active in this classloader
+    /**
+     * Map of all new unenhanced instances active in this classloader, keyed
+     * by object identity. Keys are held weakly, values strongly.
+     *
+     * @deprecated use
+     * {@link #registerUnenhancedInstance(Object, PersistenceCapable)} to add
+     * mappings and {@link #toPersistenceCapable(Object, Object)} to resolve
+     * them.
+     */
+    @Deprecated
     public static final Map _unenhancedInstanceMap =
         new ConcurrentReferenceHashMap(ReferenceStrength.WEAK, ReferenceStrength.HARD) {
 
@@ -313,14 +322,43 @@ public class ImplHelper {
                 return null;
 
             pc = new ReflectingPersistenceCapable(o, conf);
-            _unenhancedInstanceMap.put(o, pc);
+            registerUnenhancedInstance(o, pc);
             return pc;
         }
     }
 
+    /**
+     * Registers the {@link PersistenceCapable} instance that manages the state
+     * of the given raw, unenhanced instance, so that subsequent calls to
+     * {@link #toPersistenceCapable(Object, Object)} for that instance resolve
+     * to <code>pc</code> instead of creating a new
+     * {@link org.apache.openjpa.enhance.ReflectingPersistenceCapable}.
+     * <p>
+     * Instances are keyed by identity rather than by {@link Object#equals},
+     * so value-based types such as records can be used as keys. Any mapping
+     * already present for the instance is replaced. <code>pc</code> is not
+     * required to report <code>instance</code> as its managed instance; for a
+     * record embeddable it is the embedded copy carrying the state manager.
+     *
+     * @param instance the raw, unenhanced managed instance
+     * @param pc the persistence-capable instance to resolve it to
+     * @since 4.2.0
+     */
+    public static void registerUnenhancedInstance(Object instance,
+        PersistenceCapable pc) {
+        _unenhancedInstanceMap.put(instance, pc);
+    }
+
+    /**
+     * Re-registers a {@link ReflectingPersistenceCapable} against the
+     * instance it manages, for example after deserialization.
+     *
+     * @param pc the persistence-capable instance to register
+     * @see #registerUnenhancedInstance(Object, PersistenceCapable)
+     */
     public static void registerPersistenceCapable(
         ReflectingPersistenceCapable pc) {
-        _unenhancedInstanceMap.put(pc.getManagedInstance(), pc);
+        registerUnenhancedInstance(pc.getManagedInstance(), pc);
     }
 
     /**
diff --git a/openjpa-kernel/src/test/java/org/apache/openjpa/util/TestImplHelperUnenhancedInstance.java b/openjpa-kernel/src/test/java/org/apache/openjpa/util/TestImplHelperUnenhancedInstance.java
new file mode 100644
index 000000000..f03fe6f43
--- /dev/null
+++ b/openjpa-kernel/src/test/java/org/apache/openjpa/util/TestImplHelperUnenhancedInstance.java
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import org.apache.openjpa.enhance.PersistenceCapable;
+import org.junit.Test;
+
+/**
+ * Pins the registry contract of
+ * {@link ImplHelper#registerUnenhancedInstance(Object, PersistenceCapable)}.
+ * <p>
+ * The registrations are read back through the registry itself: resolving them
+ * through {@link ImplHelper#toPersistenceCapable(Object, Object)} would need a
+ * configuration and manageable metadata, which is beyond a unit test here.
+ */
+@SuppressWarnings("deprecation")
+public class TestImplHelperUnenhancedInstance {
+
+    record Point(int x, int y) {
+    }
+
+    /**
+     * Records are the reason this registry exists, and two equal records must
+     * not share one registration.
+     */
+    @Test
+    public void testKeysByIdentityNotEquality() {
+        Point p1 = new Point(3, 4);
+        Point p2 = new Point(3, 4);
+        assertEquals(p1, p2);
+
+        PersistenceCapable pc1 = newPersistenceCapable();
+        PersistenceCapable pc2 = newPersistenceCapable();
+        ImplHelper.registerUnenhancedInstance(p1, pc1);
+        ImplHelper.registerUnenhancedInstance(p2, pc2);
+
+        assertSame(pc1, ImplHelper._unenhancedInstanceMap.get(p1));
+        assertSame(pc2, ImplHelper._unenhancedInstanceMap.get(p2));
+        assertNull(ImplHelper._unenhancedInstanceMap.get(new Point(3, 4)));
+    }
+
+    @Test
+    public void testLastRegistrationWins() {
+        Point p = new Point(5, 6);
+        PersistenceCapable pcA = newPersistenceCapable();
+        PersistenceCapable pcB = newPersistenceCapable();
+
+        ImplHelper.registerUnenhancedInstance(p, pcA);
+        ImplHelper.registerUnenhancedInstance(p, pcB);
+        assertSame(pcB, ImplHelper._unenhancedInstanceMap.get(p));
+    }
+
+    /**
+     * A do-nothing {@link PersistenceCapable} stub; no mock framework is
+     * available on this module's test classpath.
+     */
+    private static PersistenceCapable newPersistenceCapable() {
+        InvocationHandler handler = new InvocationHandler() {
+
+            @Override
+            public Object invoke(Object proxy, Method method, Object[] args) {
+                switch (method.getName()) {
+                    case "equals":
+                        return proxy == args[0];
+                    case "hashCode":
+                        return System.identityHashCode(proxy);
+                    case "toString":
+                        return "PersistenceCapable stub";
+                    default:
+                        return null;
+                }
+            }
+        };
+        return (PersistenceCapable) Proxy.newProxyInstance(
+            PersistenceCapable.class.getClassLoader(),
+            new Class<?>[]{ PersistenceCapable.class }, handler);
+    }
+}