|
| 1 | +package io.ebean.querybean.generator; |
| 2 | + |
| 3 | +import static java.util.function.Predicate.not; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.Writer; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import javax.annotation.processing.RoundEnvironment; |
| 11 | +import javax.lang.model.element.TypeElement; |
| 12 | +import javax.lang.model.util.Elements; |
| 13 | +import javax.tools.FileObject; |
| 14 | + |
| 15 | +/** Write the source code for the factory. */ |
| 16 | +class LookupWriter { |
| 17 | + private LookupWriter() {} |
| 18 | + |
| 19 | + private static final String METAINF_SERVICES_LOOKUP = |
| 20 | + "META-INF/services/io.ebean.config.LookupProvider"; |
| 21 | + |
| 22 | + private static final String FILE_STRING = |
| 23 | + "package %s;\n" |
| 24 | + + "\n" |
| 25 | + + "import java.lang.invoke.MethodHandles;\n" |
| 26 | + + "\n" |
| 27 | + + "import io.ebean.config.LookupProvider;\n" |
| 28 | + + "\n" |
| 29 | + + "public class EbeanMethodLookup implements LookupProvider {\n" |
| 30 | + + "\n" |
| 31 | + + " private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();\n" |
| 32 | + + "\n" |
| 33 | + + " @Override\n" |
| 34 | + + " public MethodHandles.Lookup provideLookup() {\n" |
| 35 | + + " return LOOKUP;\n" |
| 36 | + + " }\n" |
| 37 | + + "}"; |
| 38 | + |
| 39 | + static void write( |
| 40 | + ProcessingContext processingContext, |
| 41 | + Elements util, |
| 42 | + Set<? extends TypeElement> annotations, |
| 43 | + RoundEnvironment roundEnv) { |
| 44 | + |
| 45 | + var module = |
| 46 | + annotations.stream() |
| 47 | + .map(roundEnv::getElementsAnnotatedWith) |
| 48 | + .filter(not(Collection::isEmpty)) |
| 49 | + .findAny() |
| 50 | + .map(s -> s.iterator().next()) |
| 51 | + .map(util::getModuleOf) |
| 52 | + .orElse(null); |
| 53 | + |
| 54 | + if (module != null && !module.isUnnamed()) { |
| 55 | + var moduleNameString = module.getQualifiedName().toString(); |
| 56 | + |
| 57 | + var pkg = moduleNameString + ".lookup"; |
| 58 | + String fqn = pkg + ".EbeanMethodLookup"; |
| 59 | + try { |
| 60 | + var javaFileObject = processingContext.createWriter(fqn); |
| 61 | + |
| 62 | + var writer = new Append(javaFileObject.openWriter()); |
| 63 | + |
| 64 | + writer.append(FILE_STRING, pkg); |
| 65 | + writer.close(); |
| 66 | + writeServicesFile(processingContext, fqn); |
| 67 | + } catch (IOException e) { |
| 68 | + processingContext.logError(null, "Failed to write lookup class " + e.getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static void writeServicesFile(ProcessingContext processingContext, String fqn) |
| 74 | + throws IOException { |
| 75 | + |
| 76 | + FileObject jfo = processingContext.createMetaInfWriter(METAINF_SERVICES_LOOKUP); |
| 77 | + if (jfo != null) { |
| 78 | + Writer writer = jfo.openWriter(); |
| 79 | + writer.write(fqn); |
| 80 | + writer.close(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments