[#7924] Add a CompileOptions argument object to pass annotation processors and other things to Reflect.compile()

This commit is contained in:
lukaseder 2018-10-11 14:04:45 +02:00
parent 94fa22ba92
commit bf84314508
3 changed files with 85 additions and 4 deletions

View File

@ -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);

View File

@ -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<? extends Processor> processors;
public CompileOptions() {
this(
Collections.emptyList()
);
}
private CompileOptions(List<? extends Processor> processors) {
this.processors = processors;
}
public CompileOptions processors(Processor... processors) {
return processors(Arrays.asList(processors));
}
public CompileOptions processors(List<? extends Processor> processors) {
return new CompileOptions(processors);
}
}

View File

@ -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.
* <p>
* For example:
* <code><pre>
* Supplier&lt;String> supplier = Reflect.compile(
* "org.joor.Test",
* "package org.joor;\n" +
* "class Test implements java.util.function.Supplier&lt;String> {\n" +
* " public String get() {\n" +
* " return \"Hello World!\";\n" +
* " }\n" +
* "}\n").create().get();
* </pre></code>
*
* @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);
}
// ---------------------------------------------------------------------