From 35e67a80f13506b09ce1e08793ceff2b6937d073 Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:17:05 -0400 Subject: [PATCH] remove avaje spi reliance --- .../services/io.avaje.jsonb.JsonbComponent | 1 - pom.xml | 2 +- validator-generator/pom.xml | 2 +- .../generator/ProcessingContext.java | 61 ++++++++++++++----- .../generator/SimpleComponentWriter.java | 6 +- .../generator/ValidationProcessor.java | 19 +++++- validator-http-plugin/pom.xml | 7 --- .../http/HttpValidatorProvider.java | 2 - .../src/main/java/module-info.java | 1 - .../io.avaje.inject.spi.InjectExtension | 1 + validator-inject-plugin/pom.xml | 7 --- .../inject/spi/DefaultValidatorProvider.java | 2 - .../src/main/java/module-info.java | 1 - .../io.avaje.inject.spi.InjectExtension | 1 + 14 files changed, 70 insertions(+), 43 deletions(-) delete mode 100644 blackbox-test/src/main/resources/META-INF/services/io.avaje.jsonb.JsonbComponent create mode 100644 validator-http-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension create mode 100644 validator-inject-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension diff --git a/blackbox-test/src/main/resources/META-INF/services/io.avaje.jsonb.JsonbComponent b/blackbox-test/src/main/resources/META-INF/services/io.avaje.jsonb.JsonbComponent deleted file mode 100644 index d7ce42d4..00000000 --- a/blackbox-test/src/main/resources/META-INF/services/io.avaje.jsonb.JsonbComponent +++ /dev/null @@ -1 +0,0 @@ -org.example.customer.customtype.CustomTypeComponent diff --git a/pom.xml b/pom.xml index b12fe368..849e3530 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ 17 10.0-RC7 2.0-RC2 - 1.11 + 2.0-RC1 diff --git a/validator-generator/pom.xml b/validator-generator/pom.xml index 4aed46cb..8fffad10 100644 --- a/validator-generator/pom.xml +++ b/validator-generator/pom.xml @@ -13,7 +13,7 @@ validator generator annotation processor generating validation adapters - 1.26 + 1.27 diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/ProcessingContext.java b/validator-generator/src/main/java/io/avaje/validation/generator/ProcessingContext.java index ed284c58..cca5b02d 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/ProcessingContext.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/ProcessingContext.java @@ -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 { @@ -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 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 = @@ -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() @@ -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) { @@ -121,8 +125,33 @@ private static boolean resource(String relativeName, String replace) { } } + static Set 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); + } } diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java index 91c28a6f..8ab5d12d 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java @@ -29,7 +29,7 @@ void initialise() throws IOException { fileObject = createSourceFile(name); } if (!metaData.isEmpty()) { - ProcessingContext.validateModule(name); + ProcessingContext.validateModule(); } } @@ -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)); } } } diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/ValidationProcessor.java b/validator-generator/src/main/java/io/avaje/validation/generator/ValidationProcessor.java index 41d0f4d7..dbaa492f 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/ValidationProcessor.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/ValidationProcessor.java @@ -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 { @@ -74,7 +75,7 @@ private void readModule() { @Override public boolean process(Set 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); @@ -93,6 +94,8 @@ public boolean process(Set 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()); @@ -326,4 +329,16 @@ private void writeParamProvider(ExecutableElement typeElement) { logError("Error writing ValidationAdapter for %s %s", beanReader, e); } } + + private void registerSPI(Set 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"); + } } diff --git a/validator-http-plugin/pom.xml b/validator-http-plugin/pom.xml index 01034618..91b55380 100644 --- a/validator-http-plugin/pom.xml +++ b/validator-http-plugin/pom.xml @@ -20,13 +20,6 @@ true - - io.avaje - avaje-spi-service - ${spi.version} - true - - io.avaje avaje-validator-inject-plugin diff --git a/validator-http-plugin/src/main/java/io/avaje/validation/http/HttpValidatorProvider.java b/validator-http-plugin/src/main/java/io/avaje/validation/http/HttpValidatorProvider.java index d2f35993..018af172 100644 --- a/validator-http-plugin/src/main/java/io/avaje/validation/http/HttpValidatorProvider.java +++ b/validator-http-plugin/src/main/java/io/avaje/validation/http/HttpValidatorProvider.java @@ -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; @@ -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(); diff --git a/validator-http-plugin/src/main/java/module-info.java b/validator-http-plugin/src/main/java/module-info.java index 254a4c53..13a1ef7b 100644 --- a/validator-http-plugin/src/main/java/module-info.java +++ b/validator-http-plugin/src/main/java/module-info.java @@ -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; } diff --git a/validator-http-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension b/validator-http-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension new file mode 100644 index 00000000..f52be6c8 --- /dev/null +++ b/validator-http-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension @@ -0,0 +1 @@ +io.avaje.validation.http.HttpValidatorProvider diff --git a/validator-inject-plugin/pom.xml b/validator-inject-plugin/pom.xml index 79a6b8dc..181f28b2 100644 --- a/validator-inject-plugin/pom.xml +++ b/validator-inject-plugin/pom.xml @@ -35,13 +35,6 @@ test - - io.avaje - avaje-spi-service - ${spi.version} - true - - io.avaje junit diff --git a/validator-inject-plugin/src/main/java/io/avaje/validation/inject/spi/DefaultValidatorProvider.java b/validator-inject-plugin/src/main/java/io/avaje/validation/inject/spi/DefaultValidatorProvider.java index 7ec6ac1a..8d32f3f5 100644 --- a/validator-inject-plugin/src/main/java/io/avaje/validation/inject/spi/DefaultValidatorProvider.java +++ b/validator-inject-plugin/src/main/java/io/avaje/validation/inject/spi/DefaultValidatorProvider.java @@ -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 diff --git a/validator-inject-plugin/src/main/java/module-info.java b/validator-inject-plugin/src/main/java/module-info.java index 72349716..611db964 100644 --- a/validator-inject-plugin/src/main/java/module-info.java +++ b/validator-inject-plugin/src/main/java/module-info.java @@ -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; } diff --git a/validator-inject-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension b/validator-inject-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension new file mode 100644 index 00000000..cf2556a1 --- /dev/null +++ b/validator-inject-plugin/src/main/resources/META-INF/services/io.avaje.inject.spi.InjectExtension @@ -0,0 +1 @@ +io.avaje.validation.inject.spi.DefaultValidatorProvider