@@ -27,7 +27,7 @@ public interface ClassLoadingStrategy {
27
27
/**
28
28
* This class contains implementations of default class loading strategies.
29
29
*/
30
- static enum Default implements ClassLoadingStrategy {
30
+ static enum Default implements WithDefaultProtectionDomain {
31
31
32
32
/**
33
33
* This strategy creates a new {@link net.bytebuddy.dynamic.loading.ByteArrayClassLoader} with the given
@@ -40,7 +40,17 @@ static enum Default implements ClassLoadingStrategy {
40
40
WRAPPER {
41
41
@ Override
42
42
public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
43
- return ByteArrayClassLoader .load (classLoader , types , ByteArrayClassLoader .PersistenceHandler .LATENT , PARENT_FIRST );
43
+ return ByteArrayClassLoader .load (classLoader ,
44
+ types ,
45
+ DEFAULT_PROTECTION_DOMAIN ,
46
+ ByteArrayClassLoader .PersistenceHandler .LATENT , PARENT_FIRST );
47
+ }
48
+
49
+ @ Override
50
+ public ClassLoadingStrategy withProtectionDomain (ProtectionDomain protectionDomain ) {
51
+ return new ProtectionDomainWrapper (protectionDomain ,
52
+ ByteArrayClassLoader .PersistenceHandler .LATENT ,
53
+ PARENT_FIRST );
44
54
}
45
55
},
46
56
@@ -52,7 +62,18 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
52
62
WRAPPER_PERSISTENT {
53
63
@ Override
54
64
public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
55
- return ByteArrayClassLoader .load (classLoader , types , ByteArrayClassLoader .PersistenceHandler .MANIFEST , PARENT_FIRST );
65
+ return ByteArrayClassLoader .load (classLoader ,
66
+ types ,
67
+ DEFAULT_PROTECTION_DOMAIN ,
68
+ ByteArrayClassLoader .PersistenceHandler .MANIFEST ,
69
+ PARENT_FIRST );
70
+ }
71
+
72
+ @ Override
73
+ public ClassLoadingStrategy withProtectionDomain (ProtectionDomain protectionDomain ) {
74
+ return new ProtectionDomainWrapper (protectionDomain ,
75
+ ByteArrayClassLoader .PersistenceHandler .MANIFEST ,
76
+ PARENT_FIRST );
56
77
}
57
78
},
58
79
@@ -70,7 +91,18 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
70
91
CHILD_FIRST {
71
92
@ Override
72
93
public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
73
- return ByteArrayClassLoader .load (classLoader , types , ByteArrayClassLoader .PersistenceHandler .LATENT , PARENT_LAST );
94
+ return ByteArrayClassLoader .load (classLoader ,
95
+ types ,
96
+ DEFAULT_PROTECTION_DOMAIN ,
97
+ ByteArrayClassLoader .PersistenceHandler .LATENT ,
98
+ PARENT_LAST );
99
+ }
100
+
101
+ @ Override
102
+ public ClassLoadingStrategy withProtectionDomain (ProtectionDomain protectionDomain ) {
103
+ return new ProtectionDomainWrapper (protectionDomain ,
104
+ ByteArrayClassLoader .PersistenceHandler .LATENT ,
105
+ PARENT_LAST );
74
106
}
75
107
},
76
108
@@ -82,7 +114,18 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
82
114
CHILD_FIRST_PERSISTENT {
83
115
@ Override
84
116
public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
85
- return ByteArrayClassLoader .load (classLoader , types , ByteArrayClassLoader .PersistenceHandler .MANIFEST , PARENT_LAST );
117
+ return ByteArrayClassLoader .load (classLoader ,
118
+ types ,
119
+ DEFAULT_PROTECTION_DOMAIN ,
120
+ ByteArrayClassLoader .PersistenceHandler .MANIFEST ,
121
+ PARENT_LAST );
122
+ }
123
+
124
+ @ Override
125
+ public ClassLoadingStrategy withProtectionDomain (ProtectionDomain protectionDomain ) {
126
+ return new ProtectionDomainWrapper (protectionDomain ,
127
+ ByteArrayClassLoader .PersistenceHandler .MANIFEST ,
128
+ PARENT_LAST );
86
129
}
87
130
},
88
131
@@ -100,53 +143,146 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
100
143
public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
101
144
return ClassLoaderByteArrayInjector .inject (new ClassLoaderByteArrayInjector (classLoader ), types );
102
145
}
146
+
147
+ @ Override
148
+ public ClassLoadingStrategy withProtectionDomain (ProtectionDomain protectionDomain ) {
149
+ return new ProtectionDomainInjection (protectionDomain );
150
+ }
103
151
};
104
152
105
153
/**
106
154
* An identifier for a class loading-order for making the code more readable.
107
155
*/
108
156
private static final boolean PARENT_LAST = true , PARENT_FIRST = false ;
109
- }
110
-
111
- /**
112
- * A class loading strategy which applies a class loader injection while applying a given
113
- * {@link java.security.ProtectionDomain}.
114
- */
115
- public static class ProtectionDomainInjection implements ClassLoadingStrategy {
116
157
117
158
/**
118
- * The protection domain to apply .
159
+ * A convenience reference that references the default protection domain which is {@code null} .
119
160
*/
120
- private final ProtectionDomain protectionDomain ;
161
+ private static final ProtectionDomain DEFAULT_PROTECTION_DOMAIN = null ;
121
162
122
163
/**
123
- * Creates a new protection domain injection class loading strategy.
124
- *
125
- * @param protectionDomain The protection domain to apply.
164
+ * A class loading strategy which applies a class loader injection while applying a given
165
+ * {@link java.security.ProtectionDomain} on class injection.
126
166
*/
127
- protected ProtectionDomainInjection (ProtectionDomain protectionDomain ) {
128
- this .protectionDomain = protectionDomain ;
129
- }
167
+ protected static class ProtectionDomainInjection implements ClassLoadingStrategy {
130
168
131
- @ Override
132
- public Map < TypeDescription , Class <?>> load ( ClassLoader classLoader , Map < TypeDescription , byte []> types ) {
133
- return ClassLoaderByteArrayInjector . inject ( new ClassLoaderByteArrayInjector ( classLoader , protectionDomain ), types );
134
- }
169
+ /**
170
+ * The protection domain to apply.
171
+ */
172
+ private final ProtectionDomain protectionDomain ;
135
173
136
- @ Override
137
- public boolean equals (Object other ) {
138
- return this == other || !(other == null || getClass () != other .getClass ())
139
- && protectionDomain .equals (((ProtectionDomainInjection ) other ).protectionDomain );
140
- }
174
+ /**
175
+ * Creates a new protection domain injection class loading strategy.
176
+ *
177
+ * @param protectionDomain The protection domain to apply.
178
+ */
179
+ protected ProtectionDomainInjection (ProtectionDomain protectionDomain ) {
180
+ this .protectionDomain = protectionDomain ;
181
+ }
182
+
183
+ @ Override
184
+ public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
185
+ return ClassLoaderByteArrayInjector .inject (new ClassLoaderByteArrayInjector (classLoader , protectionDomain ), types );
186
+ }
187
+
188
+ @ Override
189
+ public boolean equals (Object other ) {
190
+ return this == other || !(other == null || getClass () != other .getClass ())
191
+ && protectionDomain .equals (((ProtectionDomainInjection ) other ).protectionDomain );
192
+ }
193
+
194
+ @ Override
195
+ public int hashCode () {
196
+ return protectionDomain .hashCode ();
197
+ }
141
198
142
- @ Override
143
- public int hashCode () {
144
- return protectionDomain .hashCode ();
199
+ @ Override
200
+ public String toString () {
201
+ return "ClassLoadingStrategy.Default.ProtectionDomainInjection{protectionDomain=" + protectionDomain + '}' ;
202
+ }
145
203
}
146
204
147
- @ Override
148
- public String toString () {
149
- return "ClassLoadingStrategy.ProtectionDomainInjection{protectionDomain=" + protectionDomain + '}' ;
205
+ /**
206
+ * A class loading strategy which creates a wrapping class loader while applying a given
207
+ * {@link java.security.ProtectionDomain} on class loading.
208
+ */
209
+ protected static class ProtectionDomainWrapper implements ClassLoadingStrategy {
210
+
211
+ /**
212
+ * The protection domain to apply.
213
+ */
214
+ private final ProtectionDomain protectionDomain ;
215
+
216
+ /**
217
+ * The persistence handler to apply.
218
+ */
219
+ private final ByteArrayClassLoader .PersistenceHandler persistenceHandler ;
220
+
221
+ /**
222
+ * {@code true} if the created class loader should apply child-first semantics.
223
+ */
224
+ private final boolean childFirst ;
225
+
226
+ /**
227
+ * Creates a new protection domain specific class loading wrapper.
228
+ *
229
+ * @param protectionDomain The protection domain to apply.
230
+ * @param persistenceHandler The persistence handler to apply.
231
+ * @param childFirst {@code true} if the created class loader should apply child-first semantics.
232
+ */
233
+ public ProtectionDomainWrapper (ProtectionDomain protectionDomain ,
234
+ ByteArrayClassLoader .PersistenceHandler persistenceHandler ,
235
+ boolean childFirst ) {
236
+ this .protectionDomain = protectionDomain ;
237
+ this .persistenceHandler = persistenceHandler ;
238
+ this .childFirst = childFirst ;
239
+ }
240
+
241
+ @ Override
242
+ public Map <TypeDescription , Class <?>> load (ClassLoader classLoader , Map <TypeDescription , byte []> types ) {
243
+ return ByteArrayClassLoader .load (classLoader , types , protectionDomain , persistenceHandler , childFirst );
244
+ }
245
+
246
+ @ Override
247
+ public boolean equals (Object other ) {
248
+ if (this == other ) return true ;
249
+ if (other == null || getClass () != other .getClass ()) return false ;
250
+ ProtectionDomainWrapper that = (ProtectionDomainWrapper ) other ;
251
+ return childFirst == that .childFirst
252
+ && persistenceHandler == that .persistenceHandler
253
+ && protectionDomain .equals (that .protectionDomain );
254
+ }
255
+
256
+ @ Override
257
+ public int hashCode () {
258
+ int result = protectionDomain .hashCode ();
259
+ result = 31 * result + (childFirst ? 1 : 0 );
260
+ result = 31 * result + persistenceHandler .hashCode ();
261
+ return result ;
262
+ }
263
+
264
+ @ Override
265
+ public String toString () {
266
+ return "ClassLoadingStrategy.Default.ProtectionDomainWrapper{" +
267
+ "protectionDomain=" + protectionDomain +
268
+ ", childFirst=" + childFirst +
269
+ ", persistenceHandler=" + persistenceHandler +
270
+ '}' ;
271
+ }
150
272
}
151
273
}
274
+
275
+ /**
276
+ * A {@link net.bytebuddy.dynamic.ClassLoadingStrategy} that applies a default {@link java.security.ProtectionDomain}.
277
+ */
278
+ static interface WithDefaultProtectionDomain extends ClassLoadingStrategy {
279
+
280
+ /**
281
+ * Overrides the implicitly set default {@link java.security.ProtectionDomain} with an explicit one.
282
+ *
283
+ * @param protectionDomain The protection domain to apply.
284
+ * @return This class loading strategy with an explicitly set {@link java.security.ProtectionDomain}.
285
+ */
286
+ ClassLoadingStrategy withProtectionDomain (ProtectionDomain protectionDomain );
287
+ }
152
288
}
0 commit comments