25
25
import java .security .PrivilegedAction ;
26
26
import java .util .ArrayList ;
27
27
import java .util .List ;
28
+ import java .util .function .BiFunction ;
28
29
import java .util .function .Function ;
29
30
31
+ import net .bytebuddy .NamingStrategy ;
32
+ import net .bytebuddy .description .type .TypeDescription ;
30
33
import org .hibernate .HibernateException ;
31
34
import org .hibernate .bytecode .enhance .internal .bytebuddy .EnhancerImplConstants ;
32
35
import org .hibernate .bytecode .enhance .spi .EnhancerConstants ;
@@ -109,7 +112,9 @@ public ByteBuddyState() {
109
112
* @param cacheKey The cache key.
110
113
* @param makeProxyFunction A function building the proxy.
111
114
* @return The loaded proxy class.
115
+ * @deprecated Use {@link #loadProxy(Class, String, BiFunction)} instead.
112
116
*/
117
+ @ Deprecated (forRemoval = true , since = "6.6" )
113
118
public Class <?> loadProxy (Class <?> referenceClass , TypeCache .SimpleKey cacheKey ,
114
119
Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
115
120
return load ( referenceClass , proxyCache , cacheKey , makeProxyFunction );
@@ -122,12 +127,40 @@ public Class<?> loadProxy(Class<?> referenceClass, TypeCache.SimpleKey cacheKey,
122
127
* @param cacheKey The cache key.
123
128
* @param makeProxyFunction A function building the proxy.
124
129
* @return The loaded proxy class.
130
+ * @deprecated Use {@link #loadBasicProxy(Class, String, BiFunction)} instead.
125
131
*/
132
+ @ Deprecated (forRemoval = true , since = "6.6" )
126
133
Class <?> loadBasicProxy (Class <?> referenceClass , TypeCache .SimpleKey cacheKey ,
127
134
Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
128
135
return load ( referenceClass , basicProxyCache , cacheKey , makeProxyFunction );
129
136
}
130
137
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
+
131
164
/**
132
165
* Load a class generated by ByteBuddy.
133
166
*
@@ -195,6 +228,31 @@ void clearState() {
195
228
basicProxyCache .clear ();
196
229
}
197
230
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
+
198
256
private Class <?> load (Class <?> referenceClass , TypeCache <TypeCache .SimpleKey > cache ,
199
257
TypeCache .SimpleKey cacheKey , Function <ByteBuddy , DynamicType .Builder <?>> makeProxyFunction ) {
200
258
return cache .findOrInsert (
@@ -445,4 +503,16 @@ private static ClassLoadingStrategy<ClassLoader> resolveClassLoadingStrategy(Cla
445
503
}
446
504
}
447
505
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
+ }
448
518
}
0 commit comments