1010import java .lang .reflect .Method ;
1111import java .util .ArrayList ;
1212import java .util .List ;
13+ import java .util .function .BiFunction ;
1314import java .util .function .Function ;
1415
16+ import net .bytebuddy .NamingStrategy ;
17+ import net .bytebuddy .description .type .TypeDescription ;
1518import org .hibernate .HibernateException ;
1619import org .hibernate .bytecode .enhance .internal .bytebuddy .EnhancerImplConstants ;
1720import org .hibernate .bytecode .enhance .spi .EnhancerConstants ;
@@ -94,7 +97,9 @@ public ByteBuddyState() {
9497 * @param cacheKey The cache key.
9598 * @param makeProxyFunction A function building the proxy.
9699 * @return The loaded proxy class.
100+ * @deprecated Use {@link #loadProxy(Class, String, BiFunction)} instead.
97101 */
102+ @ Deprecated (forRemoval = true , since = "6.6" )
98103 public Class <?> loadProxy (Class <?> referenceClass , TypeCache .SimpleKey cacheKey ,
99104 Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
100105 return load ( referenceClass , proxyCache , cacheKey , makeProxyFunction );
@@ -107,12 +112,40 @@ public Class<?> loadProxy(Class<?> referenceClass, TypeCache.SimpleKey cacheKey,
107112 * @param cacheKey The cache key.
108113 * @param makeProxyFunction A function building the proxy.
109114 * @return The loaded proxy class.
115+ * @deprecated Use {@link #loadBasicProxy(Class, String, BiFunction)} instead.
110116 */
117+ @ Deprecated (forRemoval = true , since = "6.6" )
111118 Class <?> loadBasicProxy (Class <?> referenceClass , TypeCache .SimpleKey cacheKey ,
112119 Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
113120 return load ( referenceClass , basicProxyCache , cacheKey , makeProxyFunction );
114121 }
115122
123+ /**
124+ * Load a proxy as generated by the {@link ProxyFactory}.
125+ *
126+ * @param referenceClass The main class to proxy - might be an interface.
127+ * @param proxyClassName The proxy class name.
128+ * @param makeProxyFunction A function building the proxy.
129+ * @return The loaded proxy class.
130+ */
131+ public Class <?> loadProxy (Class <?> referenceClass , String proxyClassName ,
132+ BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeProxyFunction ) {
133+ return load ( referenceClass , proxyClassName , makeProxyFunction );
134+ }
135+
136+ /**
137+ * Load a proxy as generated by the {@link BasicProxyFactory}.
138+ *
139+ * @param referenceClass The main class to proxy - might be an interface.
140+ * @param proxyClassName The proxy class name.
141+ * @param makeProxyFunction A function building the proxy.
142+ * @return The loaded proxy class.
143+ */
144+ Class <?> loadBasicProxy (Class <?> referenceClass , String proxyClassName ,
145+ BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeProxyFunction ) {
146+ return load ( referenceClass , proxyClassName , makeProxyFunction );
147+ }
148+
116149 /**
117150 * Load a class generated by ByteBuddy.
118151 *
@@ -180,6 +213,31 @@ void clearState() {
180213 basicProxyCache .clear ();
181214 }
182215
216+ private Class <?> load (Class <?> referenceClass , String proxyClassName , BiFunction <ByteBuddy , NamingStrategy , DynamicType .Builder <?>> makeProxyFunction ) {
217+ try {
218+ return referenceClass .getClassLoader ().loadClass ( proxyClassName );
219+ }
220+ catch (ClassNotFoundException e ) {
221+ // Ignore
222+ }
223+ try {
224+ return make ( makeProxyFunction .apply ( byteBuddy , new FixedNamingStrategy ( proxyClassName ) ) )
225+ .load (
226+ referenceClass .getClassLoader (),
227+ resolveClassLoadingStrategy ( referenceClass )
228+ )
229+ .getLoaded ();
230+ }
231+ catch (LinkageError e ) {
232+ try {
233+ return referenceClass .getClassLoader ().loadClass ( proxyClassName );
234+ }
235+ catch (ClassNotFoundException ex ) {
236+ throw new RuntimeException ( "Couldn't load or define class [" + proxyClassName + "]" , e );
237+ }
238+ }
239+ }
240+
183241 private Class <?> load (Class <?> referenceClass , TypeCache <TypeCache .SimpleKey > cache ,
184242 TypeCache .SimpleKey cacheKey , Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
185243 return cache .findOrInsert (
@@ -325,4 +383,16 @@ private static ClassLoadingStrategy<ClassLoader> resolveClassLoadingStrategy(Cla
325383 }
326384 }
327385
386+ private static class FixedNamingStrategy extends NamingStrategy .AbstractBase {
387+ private final String className ;
388+
389+ public FixedNamingStrategy (String className ) {
390+ this .className = className ;
391+ }
392+
393+ @ Override
394+ protected String name (TypeDescription typeDescription ) {
395+ return className ;
396+ }
397+ }
328398}
0 commit comments