From bf843145080aadafff71b58f5a6fcb0b6adb2cc1 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Thu, 11 Oct 2018 14:04:45 +0200 Subject: [PATCH] [#7924] Add a CompileOptions argument object to pass annotation processors and other things to Reflect.compile() --- .../java/org/jooq/tools/reflect/Compile.java | 10 +++- .../jooq/tools/reflect/CompileOptions.java | 50 +++++++++++++++++++ .../java/org/jooq/tools/reflect/Reflect.java | 29 ++++++++++- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/tools/reflect/CompileOptions.java diff --git a/jOOQ/src/main/java/org/jooq/tools/reflect/Compile.java b/jOOQ/src/main/java/org/jooq/tools/reflect/Compile.java index 1b739939ff..bbe53cf515 100644 --- a/jOOQ/src/main/java/org/jooq/tools/reflect/Compile.java +++ b/jOOQ/src/main/java/org/jooq/tools/reflect/Compile.java @@ -33,6 +33,7 @@ import java.util.List; import javax.tools.FileObject; import javax.tools.ForwardingJavaFileManager; import javax.tools.JavaCompiler; +import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaFileManager; import javax.tools.SimpleJavaFileObject; import javax.tools.StandardJavaFileManager; @@ -46,7 +47,7 @@ import javax.tools.ToolProvider; */ class Compile { - static Class compile(String className, String content) { + static Class compile(String className, String content, CompileOptions compileOptions) { Lookup lookup = MethodHandles.lookup(); ClassLoader cl = lookup.lookupClass().getClassLoader(); @@ -82,7 +83,12 @@ class Compile { } options.addAll(Arrays.asList("-classpath", classpath.toString())); - compiler.getTask(out, fileManager, null, options, null, files).call(); + CompilationTask task = compiler.getTask(out, fileManager, null, options, null, files); + + if (!compileOptions.processors.isEmpty()) + task.setProcessors(compileOptions.processors); + + task.call(); if (fileManager.o == null) throw new ReflectException("Compilation error: " + out); diff --git a/jOOQ/src/main/java/org/jooq/tools/reflect/CompileOptions.java b/jOOQ/src/main/java/org/jooq/tools/reflect/CompileOptions.java new file mode 100644 index 0000000000..0d69aea32e --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/tools/reflect/CompileOptions.java @@ -0,0 +1,50 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jooq.tools.reflect; + +import java.util.Arrays; + + + +import java.util.Collections; +import java.util.List; + +import javax.annotation.processing.Processor; + +/** + * @author Lukas Eder + */ +public final class CompileOptions { + + final List processors; + + public CompileOptions() { + this( + Collections.emptyList() + ); + } + + private CompileOptions(List processors) { + this.processors = processors; + } + + public CompileOptions processors(Processor... processors) { + return processors(Arrays.asList(processors)); + } + + public CompileOptions processors(List processors) { + return new CompileOptions(processors); + } +} + diff --git a/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java b/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java index ee5d431db3..ce8b424cf5 100644 --- a/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java +++ b/jOOQ/src/main/java/org/jooq/tools/reflect/Reflect.java @@ -74,7 +74,32 @@ public class Reflect { * @throws ReflectException if anything went wrong compiling the class. */ public static Reflect compile(String name, String content) throws ReflectException { - return on(Compile.compile(name, content)); + return compile(name, content, new CompileOptions()); + } + + /** + * Compile a class at runtime and reflect on it. + *

+ * For example: + *

+     * Supplier<String> supplier = Reflect.compile(
+     *   "org.joor.Test",
+     *   "package org.joor;\n" +
+     *   "class Test implements java.util.function.Supplier<String> {\n" +
+     *   "  public String get() {\n" +
+     *   "    return \"Hello World!\";\n" +
+     *   "  }\n" +
+     *   "}\n").create().get();
+     * 
+ * + * @param name The qualified class name + * @param content The source code for the class + * @param options compiler options + * @return A wrapped {@link Class} + * @throws ReflectException if anything went wrong compiling the class. + */ + public static Reflect compile(String name, String content, CompileOptions options) throws ReflectException { + return on(Compile.compile(name, content, options)); } @@ -743,7 +768,7 @@ public class Reflect { */ @Override public String toString() { - return object.toString(); + return String.valueOf(object); } // ---------------------------------------------------------------------