Skip to content

Commit d2507dd

Browse files
author
rafwin
committed
Added full support for optionally specifying protection domains on the shipped class loader strategies.
1 parent e9667ee commit d2507dd

8 files changed

+324
-50
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/ClassLoadingStrategy.java

+171-35
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface ClassLoadingStrategy {
2727
/**
2828
* This class contains implementations of default class loading strategies.
2929
*/
30-
static enum Default implements ClassLoadingStrategy {
30+
static enum Default implements WithDefaultProtectionDomain {
3131

3232
/**
3333
* This strategy creates a new {@link net.bytebuddy.dynamic.loading.ByteArrayClassLoader} with the given
@@ -40,7 +40,17 @@ static enum Default implements ClassLoadingStrategy {
4040
WRAPPER {
4141
@Override
4242
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);
4454
}
4555
},
4656

@@ -52,7 +62,18 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
5262
WRAPPER_PERSISTENT {
5363
@Override
5464
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);
5677
}
5778
},
5879

@@ -70,7 +91,18 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
7091
CHILD_FIRST {
7192
@Override
7293
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);
74106
}
75107
},
76108

@@ -82,7 +114,18 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
82114
CHILD_FIRST_PERSISTENT {
83115
@Override
84116
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);
86129
}
87130
},
88131

@@ -100,53 +143,146 @@ public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDesc
100143
public Map<TypeDescription, Class<?>> load(ClassLoader classLoader, Map<TypeDescription, byte[]> types) {
101144
return ClassLoaderByteArrayInjector.inject(new ClassLoaderByteArrayInjector(classLoader), types);
102145
}
146+
147+
@Override
148+
public ClassLoadingStrategy withProtectionDomain(ProtectionDomain protectionDomain) {
149+
return new ProtectionDomainInjection(protectionDomain);
150+
}
103151
};
104152

105153
/**
106154
* An identifier for a class loading-order for making the code more readable.
107155
*/
108156
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 {
116157

117158
/**
118-
* The protection domain to apply.
159+
* A convenience reference that references the default protection domain which is {@code null}.
119160
*/
120-
private final ProtectionDomain protectionDomain;
161+
private static final ProtectionDomain DEFAULT_PROTECTION_DOMAIN = null;
121162

122163
/**
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.
126166
*/
127-
protected ProtectionDomainInjection(ProtectionDomain protectionDomain) {
128-
this.protectionDomain = protectionDomain;
129-
}
167+
protected static class ProtectionDomainInjection implements ClassLoadingStrategy {
130168

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;
135173

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+
}
141198

142-
@Override
143-
public int hashCode() {
144-
return protectionDomain.hashCode();
199+
@Override
200+
public String toString() {
201+
return "ClassLoadingStrategy.Default.ProtectionDomainInjection{protectionDomain=" + protectionDomain + '}';
202+
}
145203
}
146204

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+
}
150272
}
151273
}
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+
}
152288
}

0 commit comments

Comments
 (0)