Skip to content

Commit 20bf9f9

Browse files
committed
[#120] support for -proc:only compiler option
- Reflect.compile() must not return null, but throw if there's no output - Reflect.process() is new API that implements -proc:only semantics - CompileOptions::hasOption should be package private - Update project formatting settings - Reverted unrelated change about diagnostic feedback
1 parent b3321ed commit 20bf9f9

14 files changed

Lines changed: 411 additions & 87 deletions

File tree

jOOR-java-6/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<plugin>
4141
<groupId>org.apache.maven.plugins</groupId>
4242
<artifactId>maven-compiler-plugin</artifactId>
43-
<version>3.8.0</version>
43+
<version>3.10.1</version>
4444
<configuration>
4545
<fork>true</fork>
4646
<maxmem>512m</maxmem>

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

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

295295

296296

297+
298+
299+
300+
301+
302+
303+
304+
297305

298306

299307

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

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

5454

5555

56+
57+
58+
59+
60+
61+
62+
63+
5664

5765

5866

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.lang.reflect.Method;
2424
import java.lang.reflect.Modifier;
2525
import java.lang.reflect.Proxy;
26+
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.LinkedHashMap;
29+
import java.util.List;
2830
import java.util.Map;
2931
// ...
3032

@@ -84,6 +86,70 @@ public class Reflect {
8486

8587

8688

89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
87153

88154

89155

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,41 @@
1313
*/
1414
package org.joor.test;
1515

16-
import java.io.Serializable;
17-
import java.util.Collections;
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
1851

1952

2053

jOOR-java-8/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<plugin>
4141
<groupId>org.apache.maven.plugins</groupId>
4242
<artifactId>maven-compiler-plugin</artifactId>
43-
<version>3.8.0</version>
43+
<version>3.10.1</version>
4444
<configuration>
4545
<fork>true</fork>
4646
<maxmem>512m</maxmem>

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
class Compile {
5555

5656
static Class<?> compile(String className, String content, CompileOptions compileOptions) {
57+
return compile(className, content, compileOptions, true);
58+
}
59+
60+
static Class<?> compile(String className, String content, CompileOptions compileOptions, boolean expectResult) {
5761
Lookup lookup = MethodHandles.lookup();
5862
ClassLoader cl = lookup.lookupClass().getClassLoader();
5963

@@ -103,9 +107,9 @@ static Class<?> compile(String className, String content, CompileOptions compile
103107
task.call();
104108

105109
if (fileManager.isEmpty()) {
106-
if (compileOptions.hashOption("-proc:only")) {
110+
if (!expectResult)
107111
return null;
108-
}
112+
109113
throw new ReflectException("Compilation error: " + out);
110114
}
111115

@@ -266,21 +270,21 @@ Class<?> loadAndReturnMainClass(String mainClassName, ThrowingBiFunction<String,
266270

267271
// Try at most n times
268272
for (int i1 = 0; i1 < n1 && !queue.isEmpty(); i1++) {
269-
int n2 = queue.size();
273+
int n2 = queue.size();
270274

271-
for (int i2 = 0; i2 < n2; i2 ++) {
272-
Entry<String, byte[]> entry = queue.pop();
275+
for (int i2 = 0; i2 < n2; i2++) {
276+
Entry<String, byte[]> entry = queue.pop();
273277

274-
try {
275-
Class<?> c = definer.apply(entry.getKey(), entry.getValue());
278+
try {
279+
Class<?> c = definer.apply(entry.getKey(), entry.getValue());
276280

277-
if (mainClassName.equals(entry.getKey()))
278-
result = c;
279-
}
280-
catch (ReflectException e) {
281-
queue.offer(entry);
282-
}
283-
}
281+
if (mainClassName.equals(entry.getKey()))
282+
result = c;
283+
}
284+
catch (ReflectException e) {
285+
queue.offer(entry);
286+
}
287+
}
284288
}
285289

286290
return result;

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public final class CompileOptions {
2929

3030
final List<? extends Processor> processors;
31-
final List<String> options;
31+
final List<String> options;
3232

3333
public CompileOptions() {
3434
this(
@@ -61,12 +61,11 @@ public final CompileOptions options(List<String> newOptions) {
6161
return new CompileOptions(processors, newOptions);
6262
}
6363

64-
public boolean hashOption(String opt) {
65-
for (String option : options) {
66-
if (option.equalsIgnoreCase(opt)) {
64+
final boolean hasOption(String opt) {
65+
for (String option : options)
66+
if (option.equalsIgnoreCase(opt))
6767
return true;
68-
}
69-
}
68+
7069
return false;
7170
}
7271
}

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

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import java.lang.reflect.Method;
2424
import java.lang.reflect.Modifier;
2525
import java.lang.reflect.Proxy;
26+
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.LinkedHashMap;
29+
import java.util.List;
2830
import java.util.Map;
29-
import java.util.Objects;
3031
import java.util.Optional;
3132

3233
/**
@@ -61,14 +62,14 @@ public class Reflect {
6162
* <p>
6263
* For example:
6364
* <code><pre>
64-
* Supplier&lt;String&gt; supplier = Reflect.compile(
65-
* "org.joor.Test",
66-
* "package org.joor;\n" +
67-
* "class Test implements java.util.function.Supplier&lt;String&gt; {\n" +
68-
* " public String get() {\n" +
69-
* " return \"Hello World!\";\n" +
70-
* " }\n" +
71-
* "}\n").create().get();
65+
* Supplier&lt;String&gt; supplier = Reflect.compile("org.joor.Test", """
66+
* package org.joor;
67+
* class Test implements java.util.function.Supplier&lt;String&gt; {
68+
* public String get() {
69+
* return "Hello World!";
70+
* }
71+
* }
72+
* """).create().get();
7273
* </pre></code>
7374
*
7475
* @param name The qualified class name
@@ -85,14 +86,14 @@ public static Reflect compile(String name, String content) throws ReflectExcepti
8586
* <p>
8687
* For example:
8788
* <code><pre>
88-
* Supplier&lt;String&gt; supplier = Reflect.compile(
89-
* "org.joor.Test",
90-
* "package org.joor;\n" +
91-
* "class Test implements java.util.function.Supplier&lt;String&gt; {\n" +
92-
* " public String get() {\n" +
93-
* " return \"Hello World!\";\n" +
94-
* " }\n" +
95-
* "}\n").create().get();
89+
* Supplier&lt;String&gt; supplier = Reflect.compile("org.joor.Test", """
90+
* package org.joor;
91+
* class Test implements java.util.function.Supplier&lt;String&gt; {
92+
* public String get() {
93+
* return "Hello World!";
94+
* }
95+
* }
96+
* """).create().get();
9697
* </pre></code>
9798
*
9899
* @param name The qualified class name
@@ -105,6 +106,70 @@ public static Reflect compile(String name, String content, CompileOptions option
105106
return onClass(Compile.compile(name, content, options));
106107
}
107108

109+
/**
110+
* Annotation-process a class at runtime.
111+
* <p>
112+
* This works like {@link #compile(String, String)}, but adds the
113+
* <code>-proc:only</code> {@link CompileOptions} and thus does not produce any
114+
* compilation output.
115+
* <p>
116+
* For example:
117+
* <code><pre>
118+
* Supplier&lt;String&gt; supplier = Reflect.compile("org.joor.Test", """
119+
* package org.joor;
120+
* @MyAnnotation
121+
* class Test implements java.util.function.Supplier&lt;String&gt; {
122+
* public String get() {
123+
* return "Hello World!";
124+
* }
125+
* }
126+
* """).create().get();
127+
* </pre></code>
128+
*
129+
* @param name The qualified class name
130+
* @param content The source code for the class
131+
* @throws ReflectException if anything went wrong compiling the class.
132+
*/
133+
public static void process(String name, String content) throws ReflectException {
134+
process(name, content, new CompileOptions());
135+
}
136+
137+
/**
138+
* Annotation-process a class at runtime.
139+
* <p>
140+
* This works like {@link #compile(String, String)}, but adds the
141+
* <code>-proc:only</code> {@link CompileOptions} and thus does not produce any
142+
* compilation output.
143+
* <p>
144+
* For example:
145+
* <code><pre>
146+
* Supplier&lt;String&gt; supplier = Reflect.compile("org.joor.Test", """
147+
* package org.joor;
148+
* @MyAnnotation
149+
* class Test implements java.util.function.Supplier&lt;String&gt; {
150+
* public String get() {
151+
* return "Hello World!";
152+
* }
153+
* }
154+
* """).create().get();
155+
* </pre></code>
156+
*
157+
* @param name The qualified class name
158+
* @param content The source code for the class
159+
* @param options compiler options
160+
* @return A wrapped {@link Class}
161+
* @throws ReflectException if anything went wrong compiling the class.
162+
*/
163+
public static void process(String name, String content, CompileOptions options) throws ReflectException {
164+
if (!options.hasOption("-proc:only")) {
165+
List<String> o = new ArrayList<>(options.options);
166+
o.add("-proc:only");
167+
options = options.options(o);
168+
}
169+
170+
Compile.compile(name, content, options, false);
171+
}
172+
108173

109174
/**
110175
* Wrap a class name.
@@ -199,7 +264,7 @@ public static Reflect onClass(String name, ClassLoader classLoader) throws Refle
199264
* @return A wrapped class object, to be used for further reflection.
200265
*/
201266
public static Reflect onClass(Class<?> clazz) {
202-
return Objects.isNull(clazz) ? null : new Reflect(clazz);
267+
return new Reflect(clazz);
203268
}
204269

205270
/**

0 commit comments

Comments
 (0)