2525import java .security .PrivilegedAction ;
2626import java .util .ArrayList ;
2727import java .util .List ;
28+ import java .util .function .BiFunction ;
2829import java .util .function .Function ;
2930
31+ import net .bytebuddy .NamingStrategy ;
32+ import net .bytebuddy .description .type .TypeDescription ;
3033import org .hibernate .HibernateException ;
3134import org .hibernate .bytecode .enhance .internal .bytebuddy .EnhancerImplConstants ;
3235import org .hibernate .bytecode .enhance .spi .EnhancerConstants ;
@@ -109,7 +112,9 @@ public ByteBuddyState() {
109112 * @param cacheKey The cache key.
110113 * @param makeProxyFunction A function building the proxy.
111114 * @return The loaded proxy class.
115+ * @deprecated Use {@link #loadProxy(Class, String, BiFunction)} instead.
112116 */
117+ @ Deprecated (forRemoval = true , since = "6.6" )
113118 public Class <?> loadProxy (Class <?> referenceClass , TypeCache .SimpleKey cacheKey ,
114119 Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
115120 return load ( referenceClass , proxyCache , cacheKey , makeProxyFunction );
@@ -122,12 +127,40 @@ public Class<?> loadProxy(Class<?> referenceClass, TypeCache.SimpleKey cacheKey,
122127 * @param cacheKey The cache key.
123128 * @param makeProxyFunction A function building the proxy.
124129 * @return The loaded proxy class.
130+ * @deprecated Use {@link #loadBasicProxy(Class, String, BiFunction)} instead.
125131 */
132+ @ Deprecated (forRemoval = true , since = "6.6" )
126133 Class <?> loadBasicProxy (Class <?> referenceClass , TypeCache .SimpleKey cacheKey ,
127134 Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
128135 return load ( referenceClass , basicProxyCache , cacheKey , makeProxyFunction );
129136 }
130137
138+ /**
139+ * Load a proxy as generated by the {@link ProxyFactory}.
140+ *
141+ * @param referenceClass The main class to proxy - might be an interface.
142+ * @param proxyClassName The proxy class name.
143+ * @param makeProxyFunction A function building the proxy.
144+ * @return The loaded proxy class.
145+ */
146+ public Class <?> loadProxy (Class <?> referenceClass , String proxyClassName ,
147+ BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeProxyFunction ) {
148+ return load ( referenceClass , proxyClassName , makeProxyFunction );
149+ }
150+
151+ /**
152+ * Load a proxy as generated by the {@link BasicProxyFactory}.
153+ *
154+ * @param referenceClass The main class to proxy - might be an interface.
155+ * @param proxyClassName The proxy class name.
156+ * @param makeProxyFunction A function building the proxy.
157+ * @return The loaded proxy class.
158+ */
159+ Class <?> loadBasicProxy (Class <?> referenceClass , String proxyClassName ,
160+ BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeProxyFunction ) {
161+ return load ( referenceClass , proxyClassName , makeProxyFunction );
162+ }
163+
131164 /**
132165 * Load a class generated by ByteBuddy.
133166 *
@@ -195,6 +228,31 @@ void clearState() {
195228 basicProxyCache .clear ();
196229 }
197230
231+ private Class <?> load (Class <?> referenceClass , String proxyClassName , BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeProxyFunction ) {
232+ try {
233+ return referenceClass .getClassLoader ().loadClass ( proxyClassName );
234+ }
235+ catch (ClassNotFoundException e ) {
236+ // Ignore
237+ }
238+ try {
239+ return make ( makeProxyFunction .apply ( byteBuddy , new FixedNamingStrategy ( proxyClassName ) ) )
240+ .load (
241+ referenceClass .getClassLoader (),
242+ resolveClassLoadingStrategy ( referenceClass )
243+ )
244+ .getLoaded ();
245+ }
246+ catch (LinkageError e ) {
247+ try {
248+ return referenceClass .getClassLoader ().loadClass ( proxyClassName );
249+ }
250+ catch (ClassNotFoundException ex ) {
251+ throw new RuntimeException ( "Couldn't load or define class [" + proxyClassName + "]" , e );
252+ }
253+ }
254+ }
255+
198256 private Class <?> load (Class <?> referenceClass , TypeCache <TypeCache .SimpleKey > cache ,
199257 TypeCache .SimpleKey cacheKey , Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
200258 return cache .findOrInsert (
@@ -445,4 +503,16 @@ private static ClassLoadingStrategy<ClassLoader> resolveClassLoadingStrategy(Cla
445503 }
446504 }
447505
506+ private static class FixedNamingStrategy extends NamingStrategy .AbstractBase {
507+ private final String className ;
508+
509+ public FixedNamingStrategy (String className ) {
510+ this .className = className ;
511+ }
512+
513+ @ Override
514+ protected String name (TypeDescription typeDescription ) {
515+ return className ;
516+ }
517+ }
448518}
0 commit comments