Skip to content

Commit

Permalink
remove avaje spi reliance
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Jun 25, 2024
1 parent eb88d47 commit 35e67a8
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 43 deletions.

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<maven.compiler.release>17</maven.compiler.release>
<inject.version>10.0-RC7</inject.version>
<http.version>2.0-RC2</http.version>
<spi.version>1.11</spi.version>
<spi.version>2.0-RC1</spi.version>
</properties>

<modules>
Expand Down
2 changes: 1 addition & 1 deletion validator-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<name>validator generator</name>
<description>annotation processor generating validation adapters</description>
<properties>
<avaje.prisms.version>1.26</avaje.prisms.version>
<avaje.prisms.version>1.27</avaje.prisms.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import static io.avaje.validation.generator.APContext.logWarn;
import static java.util.stream.Collectors.toSet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

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

final class ProcessingContext {
Expand All @@ -23,14 +28,13 @@ private static final class Ctx {
private final String diAnnotation;
private final boolean warnHttp;
private final boolean injectPresent;
private final boolean spiPresent;
private boolean validated;
private final Set<String> serviceSet = new TreeSet<>();

Ctx(ProcessingEnvironment env) {
var elements = env.getElementUtils();

this.injectPresent = elements.getTypeElement(Constants.COMPONENT) != null;
this.warnHttp = elements.getTypeElement("io.avaje.http.api.Controller") != null;
this.spiPresent = elements.getTypeElement("io.avaje.spi.internal.ServiceProcessor") != null;

final var jakarta = elements.getTypeElement(Constants.SINGLETON_JAKARTA) != null;
diAnnotation =
Expand All @@ -48,28 +52,24 @@ static void init(ProcessingEnvironment processingEnv) {
}

static FileObject createMetaInfWriterFor(String interfaceType) throws IOException {
var serviceFile =
CTX.get().spiPresent
? interfaceType.replace("META-INF/services/", "META-INF/generated-services/")
: interfaceType;

return filer().createResource(StandardLocation.CLASS_OUTPUT, "", serviceFile);
return filer().createResource(StandardLocation.CLASS_OUTPUT, "", interfaceType);
}

static String diAnnotation() {
return CTX.get().diAnnotation;
}

static void validateModule(String fqn) {
static void validateModule() {
var module = getProjectModuleElement();
if (module != null && !CTX.get().validated && !module.isUnnamed()) {

CTX.get().validated = true;
if (module != null && !module.isUnnamed()) {
var injectPresent = CTX.get().injectPresent;
var warnHttp = CTX.get().warnHttp;

try (var reader = getModuleInfoReader()) {
var moduleInfo = new ModuleInfoReader(module, reader);

moduleInfo.validateServices("io.avaje.validation.spi.ValidationExtension", CTX.get().serviceSet);

var buildPluginAvailable = buildPluginAvailable();
var requireSet =
moduleInfo.requires().stream()
Expand All @@ -90,9 +90,13 @@ static void validateModule(String fqn) {
&& !moduleInfo.containsOnModulePath("io.avaje.validation.plugin");

if (noHttpPlugin) {
logWarn(module, "`requires io.avaje.validation.http` must be explicity added or else avaje-inject may fail to detect the default http validator, validator, and method AOP validator", fqn);
logWarn(
module,
"`requires io.avaje.validation.http` must be explicity added or else avaje-inject may fail to detect the default http validator, validator, and method AOP validator");
} else if (noInjectPlugin) {
logWarn(module, "`requires io.avaje.validation.plugin` must be explicity added or else avaje-inject may fail to detect the default validator and method AOP validator", fqn);
logWarn(
module,
"`requires io.avaje.validation.plugin` must be explicity added or else avaje-inject may fail to detect the default validator and method AOP validator");
}

} catch (Exception e) {
Expand Down Expand Up @@ -121,8 +125,33 @@ private static boolean resource(String relativeName, String replace) {
}
}

static Set<String> readExistingMetaInfServices() {
System.out.println("GET GOIT" );
var services = CTX.get().serviceSet;
try (final var file =
APContext.filer()
.getResource(StandardLocation.CLASS_OUTPUT, "", Constants.META_INF_COMPONENT)
.toUri()
.toURL()
.openStream();
final var buffer = new BufferedReader(new InputStreamReader(file)); ) {

String line;
while ((line = buffer.readLine()) != null) {
line.replaceAll("\\s", "").replace(",", "\n").lines().forEach(services::add);
}
} catch (Exception e) {
// not a critical error
}
return services;
}

static void clear() {
CTX.remove();
APContext.clear();
}

public static void addValidatorSpi(String spi) {
CTX.get().serviceSet.add(spi);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void initialise() throws IOException {
fileObject = createSourceFile(name);
}
if (!metaData.isEmpty()) {
ProcessingContext.validateModule(name);
ProcessingContext.validateModule();
}
}

Expand All @@ -48,10 +48,12 @@ void write() throws IOException {
}

void writeMetaInf() throws IOException {
var services = ProcessingContext.readExistingMetaInfServices();
final FileObject fileObject = createMetaInfWriterFor(Constants.META_INF_COMPONENT);
if (fileObject != null) {
try (Writer writer = fileObject.openWriter()) {
writer.write(metaData.fullName());
services.add(metaData.fullName());
writer.write(String.join("\n", services));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
JakartaConstraintPrism.PRISM_TYPE,
JavaxConstraintPrism.PRISM_TYPE,
CrossParamConstraintPrism.PRISM_TYPE,
ValidMethodPrism.PRISM_TYPE
ValidMethodPrism.PRISM_TYPE,
"io.avaje.spi.ServiceProvider"
})
public final class ValidationProcessor extends AbstractProcessor {

Expand Down Expand Up @@ -74,7 +75,7 @@ private void readModule() {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) {
APContext.setProjectModuleElement(annotations, round);
APContext.setProjectModuleElement(annotations, round);
readModule();
getElements(round, AvajeConstraintPrism.PRISM_TYPE).ifPresent(this::writeConstraintAdapters);
getElements(round, JavaxConstraintPrism.PRISM_TYPE).ifPresent(this::writeConstraintAdapters);
Expand All @@ -93,6 +94,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
.map(ElementFilter::methodsIn)
.ifPresent(this::writeParamProviderForMethod);
getElements(round, ImportValidPojoPrism.PRISM_TYPE).ifPresent(this::writeAdaptersForImported);
getElements(round, "io.avaje.spi.ServiceProvider").ifPresent(this::registerSPI);

initialiseComponent();
cascadeTypes();
writeComponent(round.processingOver());
Expand Down Expand Up @@ -326,4 +329,16 @@ private void writeParamProvider(ExecutableElement typeElement) {
logError("Error writing ValidationAdapter for %s %s", beanReader, e);
}
}

private void registerSPI(Set<? extends Element> beans) {
ElementFilter.typesIn(beans).stream()
.filter(this::isExtension)
.map(TypeElement::getQualifiedName)
.map(Object::toString)
.forEach(ProcessingContext::addValidatorSpi);
}

private boolean isExtension(TypeElement te) {
return APContext.isAssignable(te, "io.avaje.validation.spi.ValidationExtension");
}
}
7 changes: 0 additions & 7 deletions validator-http-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-spi-service</artifactId>
<version>${spi.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-validator-inject-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.avaje.validation.http;

import io.avaje.inject.BeanScopeBuilder;
import io.avaje.spi.ServiceProvider;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -10,7 +9,6 @@
/**
* Plugin for avaje inject that provides a default Http Validator instance.
*/
@ServiceProvider
public final class HttpValidatorProvider implements io.avaje.inject.spi.InjectPlugin {

private static final Class<?> VALIDATOR_HTTP_CLASS = avajeHttpOnClasspath();
Expand Down
1 change: 0 additions & 1 deletion validator-http-plugin/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

requires transitive io.avaje.validation.plugin;
requires transitive io.avaje.http.api;
requires static io.avaje.spi;

provides io.avaje.inject.spi.InjectExtension with io.avaje.validation.http.HttpValidatorProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.avaje.validation.http.HttpValidatorProvider
7 changes: 0 additions & 7 deletions validator-inject-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-spi-service</artifactId>
<version>${spi.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import io.avaje.inject.aop.AspectProvider;
import io.avaje.inject.spi.GenericType;
import io.avaje.inject.spi.InjectPlugin;
import io.avaje.spi.ServiceProvider;
import io.avaje.validation.ValidMethod;
import io.avaje.validation.Validator;
import io.avaje.validation.adapter.MethodAdapterProvider;
import io.avaje.validation.inject.aspect.AOPMethodValidator;

/** Plugin for avaje inject that provides a default Validator instance. */
@ServiceProvider
public final class DefaultValidatorProvider implements InjectPlugin {

@Override
Expand Down
1 change: 0 additions & 1 deletion validator-inject-plugin/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
requires transitive io.avaje.validation;
requires transitive io.avaje.inject;
requires transitive io.avaje.inject.aop;
requires static io.avaje.spi;

provides io.avaje.inject.spi.InjectExtension with io.avaje.validation.inject.spi.DefaultValidatorProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.avaje.validation.inject.spi.DefaultValidatorProvider

0 comments on commit 35e67a8

Please sign in to comment.