Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[querybean-generator] Generate lookup for modules #3580

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.ebean.querybean.generator;

import static java.util.function.Predicate.not;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Set;

import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.FileObject;

/** Write the source code for the factory. */
class LookupWriter {
private LookupWriter() {}

private static final String METAINF_SERVICES_LOOKUP =
"META-INF/services/io.ebean.config.LookupProvider";

private static final String FILE_STRING =
"package %s;\n"
+ "\n"
+ "import java.lang.invoke.MethodHandles;\n"
+ "\n"
+ "import io.ebean.config.LookupProvider;\n"
+ "\n"
+ "public class EbeanMethodLookup implements LookupProvider {\n"
+ "\n"
+ " private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();\n"
+ "\n"
+ " @Override\n"
+ " public MethodHandles.Lookup provideLookup() {\n"
+ " return LOOKUP;\n"
+ " }\n"
+ "}";

static void write(
ProcessingContext processingContext,
Elements util,
Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {

var module =
annotations.stream()
.map(roundEnv::getElementsAnnotatedWith)
.filter(not(Collection::isEmpty))
.findAny()
.map(s -> s.iterator().next())
.map(util::getModuleOf)
.orElse(null);

if (module != null && !module.isUnnamed()) {
var moduleNameString = module.getQualifiedName().toString();

var pkg = moduleNameString + ".lookup";
String fqn = pkg + ".EbeanMethodLookup";
try {
var javaFileObject = processingContext.createWriter(fqn);

var writer = new Append(javaFileObject.openWriter());

writer.append(FILE_STRING, pkg);
writer.close();
writeServicesFile(processingContext, fqn);
} catch (IOException e) {
processingContext.logError(null, "Failed to write lookup class " + e.getMessage());
}
}
}

private static void writeServicesFile(ProcessingContext processingContext, String fqn)
throws IOException {

FileObject jfo = processingContext.createMetaInfWriter(METAINF_SERVICES_LOOKUP);
if (jfo != null) {
Writer writer = jfo.openWriter();
writer.write(fqn);
writer.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class Processor extends AbstractProcessor implements Constants {

private ProcessingContext processingContext;

public Processor() {
}
private boolean wroteLookup;

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
Expand Down Expand Up @@ -56,6 +55,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
String msg = "Ebean APT generated %s query beans, loaded %s others - META-INF/ebean-generated-info.mf entity-packages: %s";
processingContext.logNote(msg, count, loaded, processingContext.getAllEntityPackages());
}
if (!wroteLookup) {
wroteLookup = true;
LookupWriter.write(processingContext, processingEnv.getElementUtils(), annotations, roundEnv);
}
return true;
}

Expand Down