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

Support the Avaje Build Plugin #189

Merged
merged 2 commits into from
Feb 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Build](https://github.com/avaje/avaje-validator/actions/workflows/build.yml/badge.svg)](https://github.com/avaje/avaje-validator/actions/workflows/build.yml)
[![native image build](https://github.com/avaje/avaje-validator/actions/workflows/native-image.yml/badge.svg)](https://github.com/avaje/avaje-validator/actions/workflows/native-image.yml)
[![Maven Central : avaje-validator](https://img.shields.io/maven-central/v/io.avaje/avaje-validator.svg?label=Maven%20Central)](https://maven-badges.herokuapp.com/maven-central/io.avaje/avaje-validator)
[![javadoc](https://javadoc.io/badge2/io.avaje/avaje-config/javadoc.svg?color=purple)](https://javadoc.io/doc/io.avaje/avaje-config/latest/io.avaje.config/io/avaje/config/package-summary.html)
[![javadoc](https://javadoc.io/badge2/io.avaje/avaje-validator/javadoc.svg?color=purple)](https://javadoc.io/doc/io.avaje/avaje-validator/latest/io.avaje-validator/io/avaje/validator/package-summary.html)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-inject/blob/master/LICENSE)
Reflection-free pojo validation via apt source code generation. A light (~120kb + generated code) source code generation style alternative to Hibernate Validation. (code generation vs reflection)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import static io.avaje.validation.generator.APContext.getProjectModuleElement;
import static io.avaje.validation.generator.APContext.logError;
import static io.avaje.validation.generator.APContext.logWarn;
import static java.util.stream.Collectors.toSet;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import java.net.URI;
import java.net.URISyntaxException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ModuleElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

import io.avaje.validation.generator.ModuleInfoReader.Requires;

final class ProcessingContext {

private static final ThreadLocal<Ctx> CTX = new ThreadLocal<>();
Expand Down Expand Up @@ -65,22 +66,32 @@ static void validateModule(String fqn) {
var warnHttp = CTX.get().warnHttp;

try (var reader = getModuleInfoReader()) {

var moduleInfo = new ModuleInfoReader(module, reader);
var buildPluginAvailable = buildPluginAvailable();
var requireSet =
moduleInfo.requires().stream()
.map(Requires::getDependency)
.map(m -> m.getQualifiedName().toString())
.collect(toSet());

boolean noHttpPlugin =
injectPresent && warnHttp && !moduleInfo.containsOnModulePath("io.avaje.validation.http");
injectPresent
&& (!buildPluginAvailable || !requireSet.contains("io.avaje.http.api"))
&& warnHttp
&& !moduleInfo.containsOnModulePath("io.avaje.validation.http");

boolean noInjectPlugin =
noHttpPlugin
&& injectPresent
&& (!buildPluginAvailable || !requireSet.contains("io.avaje.validation"))
&& !moduleInfo.containsOnModulePath("io.avaje.validation.plugin");

var noProvides =
moduleInfo.provides().stream()
.flatMap(s -> s.implementations().stream())
.noneMatch(s -> s.contains(fqn));

if (noProvides) {
if (!buildPluginAvailable && noProvides) {
logError(
module,
"Missing `provides io.avaje.validation.Validator.GeneratedComponent with %s;`",
Expand All @@ -105,11 +116,27 @@ static void validateModule(String fqn) {
}
}

static ModuleElement getModuleElement(Element e) {
if (e == null || e instanceof ModuleElement) {
return (ModuleElement) e;
private static boolean buildPluginAvailable() {

return resource("target/avaje-plugin-exists.txt", "/target/classes")
|| resource("build/avaje-plugin-exists.txt", "/build/classes/java/main");
}

private static boolean resource(String relativeName, String replace) {
try (var inputStream =
new URI(
filer()
.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName)
.toUri()
.toString()
.replace(replace, ""))
.toURL()
.openStream()) {

return inputStream.available() > 0;
} catch (IOException | URISyntaxException e) {
return false;
}
return getModuleElement(e.getEnclosingElement());
}

static void clear() {
Expand Down
Loading