2323import java .lang .reflect .Method ;
2424import java .lang .reflect .Modifier ;
2525import java .lang .reflect .Proxy ;
26+ import java .util .ArrayList ;
2627import java .util .Arrays ;
2728import java .util .LinkedHashMap ;
29+ import java .util .List ;
2830import java .util .Map ;
29- import java .util .Objects ;
3031import java .util .Optional ;
3132
3233/**
@@ -61,14 +62,14 @@ public class Reflect {
6162 * <p>
6263 * For example:
6364 * <code><pre>
64- * Supplier<String> supplier = Reflect.compile(
65- * " org.joor.Test",
66- * "package org.joor;\n" +
67- * "class Test implements java.util.function.Supplier< String> {\n" +
68- * " public String get() {\n" +
69- * " return \"Hello World!\";\n" +
70- * " }\n" +
71- * "}\n ").create().get();
65+ * Supplier<String> supplier = Reflect.compile("org.joor.Test", """
66+ * package org.joor;
67+ * class Test implements java.util.function.Supplier<String> {
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<String> supplier = Reflect.compile(
89- * " org.joor.Test",
90- * "package org.joor;\n" +
91- * "class Test implements java.util.function.Supplier< String> {\n" +
92- * " public String get() {\n" +
93- * " return \"Hello World!\";\n" +
94- * " }\n" +
95- * "}\n ").create().get();
89+ * Supplier<String> supplier = Reflect.compile("org.joor.Test", """
90+ * package org.joor;
91+ * class Test implements java.util.function.Supplier<String> {
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<String> supplier = Reflect.compile("org.joor.Test", """
119+ * package org.joor;
120+ * @MyAnnotation
121+ * class Test implements java.util.function.Supplier<String> {
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<String> supplier = Reflect.compile("org.joor.Test", """
147+ * package org.joor;
148+ * @MyAnnotation
149+ * class Test implements java.util.function.Supplier<String> {
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