Skip to content

Commit f431ee7

Browse files
committed
[#72] Add ClassLoader to CompileOptions in order to allow for overriding the ClassLoader
1 parent 7c27785 commit f431ee7

9 files changed

Lines changed: 141 additions & 13 deletions

File tree

jOOR-java-6/src/main/java/org/joor/Compile.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@
270270

271271

272272

273-
<<<<<<< HEAD
274273

275274

276275

@@ -312,5 +311,8 @@
312311

313312

314313

315-
=======
316-
>>>>>>> refs/remotes/origin/master
314+
315+
316+
317+
318+

jOOR-java-6/src/main/java/org/joor/CompileOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161

6262

6363

64+
65+
66+
67+
68+
69+
70+
71+
6472

6573

6674

jOOR-java-6/src/test/java/org/joor/test/CompileOptionsTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,39 @@
196196

197197

198198

199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
212+
213+
214+
215+
216+
217+
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+
230+
231+
199232

200233

201234

jOOR-java-8/src/main/java/org/joor/Compile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ static Class<?> compile(String className, String content, CompileOptions compile
5959

6060
static Class<?> compile(String className, String content, CompileOptions compileOptions, boolean expectResult) {
6161
Lookup lookup = MethodHandles.lookup();
62-
ClassLoader cl = lookup.lookupClass().getClassLoader();
62+
ClassLoader cl = compileOptions.classLoader != null
63+
? compileOptions.classLoader
64+
: lookup.lookupClass().getClassLoader();
6365

6466
try {
6567
return cl.loadClass(className);

jOOR-java-8/src/main/java/org/joor/CompileOptions.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,40 @@ public final class CompileOptions {
2929

3030
final List<? extends Processor> processors;
3131
final List<String> options;
32+
final ClassLoader classLoader;
3233

3334
public CompileOptions() {
3435
this(
3536
Collections.emptyList(),
36-
Collections.emptyList()
37+
Collections.emptyList(),
38+
null
3739
);
3840
}
3941

4042
private CompileOptions(
4143
List<? extends Processor> processors,
42-
List<String> options
44+
List<String> options,
45+
ClassLoader classLoader
4346
) {
4447
this.processors = processors;
4548
this.options = options;
49+
this.classLoader = classLoader;
4650
}
4751

4852
public final CompileOptions processors(Processor... newProcessors) {
4953
return processors(Arrays.asList(newProcessors));
5054
}
5155

5256
public final CompileOptions processors(List<? extends Processor> newProcessors) {
53-
return new CompileOptions(newProcessors, options);
57+
return new CompileOptions(newProcessors, options, classLoader);
5458
}
5559

5660
public final CompileOptions options(String... newOptions) {
5761
return options(Arrays.asList(newOptions));
5862
}
5963

6064
public final CompileOptions options(List<String> newOptions) {
61-
return new CompileOptions(processors, newOptions);
65+
return new CompileOptions(processors, newOptions, classLoader);
6266
}
6367

6468
final boolean hasOption(String opt) {
@@ -68,5 +72,9 @@ final boolean hasOption(String opt) {
6872

6973
return false;
7074
}
75+
76+
public final CompileOptions classLoader(ClassLoader newClassLoader) {
77+
return new CompileOptions(processors, options, newClassLoader);
78+
}
7179
}
7280

jOOR-java-8/src/test/java/org/joor/test/CompileOptionsTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import static org.junit.Assert.assertTrue;
2222
import static org.junit.Assert.fail;
2323

24+
import java.io.Serializable;
2425
import java.util.Collections;
2526
import java.util.Set;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2628

2729
import javax.annotation.processing.Completion;
2830
import javax.annotation.processing.ProcessingEnvironment;
@@ -44,6 +46,37 @@
4446
*/
4547
public class CompileOptionsTest {
4648

49+
@Test
50+
public void testCompileWithCustomClassLoader() throws Exception {
51+
AtomicBoolean called = new AtomicBoolean();
52+
CompileOptions co = new CompileOptions().classLoader(new ClassLoader() {
53+
54+
// This wasn't called in Java 8 yet (?)
55+
protected Class<?> findClass(String name) {
56+
called.set(true);
57+
return Object.class;
58+
}
59+
60+
public Class<?> loadClass(String name) {
61+
called.set(true);
62+
return Object.class;
63+
}
64+
});
65+
66+
String className = "com.example.CompileWithCustomClassLoader";
67+
String classCode =
68+
"package com.example;\n" +
69+
"class CompileWithCustomClassLoader implements java.io.Serializable {}\n";
70+
71+
Object o1 = Reflect.compile(className, classCode).create().get();
72+
assertEquals(className, o1.getClass().getName());
73+
assertTrue(o1 instanceof Serializable);
74+
75+
Object o2 = Reflect.compile(className, classCode, co).create().get();
76+
assertTrue(called.get());
77+
assertEquals("java.lang.Object", o2.getClass().getName());
78+
assertFalse(o2 instanceof Serializable);
79+
}
4780
@Test
4881
public void testCompileWithExtraOptions() {
4982
assertEquals("org.joor.test.Source7OK", Reflect.compile(

jOOR/src/main/java/org/joor/Compile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ static Class<?> compile(String className, String content, CompileOptions compile
5959

6060
static Class<?> compile(String className, String content, CompileOptions compileOptions, boolean expectResult) {
6161
Lookup lookup = MethodHandles.lookup();
62-
ClassLoader cl = lookup.lookupClass().getClassLoader();
62+
ClassLoader cl = compileOptions.classLoader != null
63+
? compileOptions.classLoader
64+
: lookup.lookupClass().getClassLoader();
6365

6466
try {
6567
return cl.loadClass(className);

jOOR/src/main/java/org/joor/CompileOptions.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,40 @@ public final class CompileOptions {
2929

3030
final List<? extends Processor> processors;
3131
final List<String> options;
32+
final ClassLoader classLoader;
3233

3334
public CompileOptions() {
3435
this(
3536
Collections.emptyList(),
36-
Collections.emptyList()
37+
Collections.emptyList(),
38+
null
3739
);
3840
}
3941

4042
private CompileOptions(
4143
List<? extends Processor> processors,
42-
List<String> options
44+
List<String> options,
45+
ClassLoader classLoader
4346
) {
4447
this.processors = processors;
4548
this.options = options;
49+
this.classLoader = classLoader;
4650
}
4751

4852
public final CompileOptions processors(Processor... newProcessors) {
4953
return processors(Arrays.asList(newProcessors));
5054
}
5155

5256
public final CompileOptions processors(List<? extends Processor> newProcessors) {
53-
return new CompileOptions(newProcessors, options);
57+
return new CompileOptions(newProcessors, options, classLoader);
5458
}
5559

5660
public final CompileOptions options(String... newOptions) {
5761
return options(Arrays.asList(newOptions));
5862
}
5963

6064
public final CompileOptions options(List<String> newOptions) {
61-
return new CompileOptions(processors, newOptions);
65+
return new CompileOptions(processors, newOptions, classLoader);
6266
}
6367

6468
final boolean hasOption(String opt) {
@@ -68,5 +72,9 @@ final boolean hasOption(String opt) {
6872

6973
return false;
7074
}
75+
76+
public final CompileOptions classLoader(ClassLoader newClassLoader) {
77+
return new CompileOptions(processors, options, newClassLoader);
78+
}
7179
}
7280
/* [/java-8] */

jOOR/src/test/java/org/joor/test/CompileOptionsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import static org.junit.Assert.assertTrue;
2222
import static org.junit.Assert.fail;
2323

24+
import java.io.Serializable;
2425
import java.util.Collections;
2526
import java.util.Set;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2628

2729
import javax.annotation.processing.Completion;
2830
import javax.annotation.processing.ProcessingEnvironment;
@@ -44,6 +46,36 @@
4446
*/
4547
public class CompileOptionsTest {
4648

49+
@Test
50+
public void testCompileWithCustomClassLoader() throws Exception {
51+
AtomicBoolean called = new AtomicBoolean();
52+
CompileOptions co = new CompileOptions().classLoader(new ClassLoader() {
53+
54+
protected Class<?> findClass(String name) {
55+
called.set(true);
56+
return Object.class;
57+
}
58+
59+
public Class<?> loadClass(String name) {
60+
called.set(true);
61+
return Object.class;
62+
}
63+
});
64+
65+
String className = "com.example.CompileWithCustomClassLoader";
66+
String classCode =
67+
"package com.example;\n" +
68+
"class CompileWithCustomClassLoader implements java.io.Serializable {}\n";
69+
70+
Object o1 = Reflect.compile(className, classCode).create().get();
71+
assertEquals(className, o1.getClass().getName());
72+
assertTrue(o1 instanceof Serializable);
73+
74+
Object o2 = Reflect.compile(className, classCode, co).create().get();
75+
assertTrue(called.get());
76+
assertEquals("java.lang.Object", o2.getClass().getName());
77+
assertFalse(o2 instanceof Serializable);
78+
}
4779
@Test
4880
public void testCompileWithExtraOptions() {
4981
assertEquals("org.joor.test.Source7OK", Reflect.compile(

0 commit comments

Comments
 (0)