From b7993ccdf8c2fba4e3144051acce24dea9f8546e Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 5 Oct 2023 22:24:38 +1300 Subject: [PATCH] HttpClientRequest.method() + format + tidy --- .../avaje/http/client/DHttpClientRequest.java | 3 +- .../avaje/http/client/HttpClientRequest.java | 7 +++ .../http/generator/core/BaseProcessor.java | 56 +++++++------------ .../http/generator/core/ControllerReader.java | 41 ++++++-------- .../http/generator/core/package-info.java | 26 ++------- 5 files changed, 53 insertions(+), 80 deletions(-) diff --git a/http-client/src/main/java/io/avaje/http/client/DHttpClientRequest.java b/http-client/src/main/java/io/avaje/http/client/DHttpClientRequest.java index a6c22bdf..9e6526eb 100644 --- a/http-client/src/main/java/io/avaje/http/client/DHttpClientRequest.java +++ b/http-client/src/main/java/io/avaje/http/client/DHttpClientRequest.java @@ -70,6 +70,7 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse { this.errorMapper = context.errorMapper(); } + @Override public String method() { return method; } @@ -808,7 +809,7 @@ boolean isSkipAuthToken() { return skipAuthToken; } -private class ListenerEvent implements RequestListener.Event { + private class ListenerEvent implements RequestListener.Event { @Override public long responseTimeMicros() { diff --git a/http-client/src/main/java/io/avaje/http/client/HttpClientRequest.java b/http-client/src/main/java/io/avaje/http/client/HttpClientRequest.java index a760e2e6..7e6ed976 100644 --- a/http-client/src/main/java/io/avaje/http/client/HttpClientRequest.java +++ b/http-client/src/main/java/io/avaje/http/client/HttpClientRequest.java @@ -179,6 +179,13 @@ public interface HttpClientRequest { */ HttpClientRequest url(String url); + /** + * The Http Verb (GET, POST, PUT etc) of this request. + * + * @return The Http Verb of this request. + */ + String method(); + /** * The URL for this request including the query parameters. * diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/BaseProcessor.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/BaseProcessor.java index b898ddab..595014b7 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/BaseProcessor.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/BaseProcessor.java @@ -25,9 +25,9 @@ @SupportedOptions({"useJavax", "useSingleton", "instrumentRequests","disableDirectWrites"}) public abstract class BaseProcessor extends AbstractProcessor { - String contextPathString; + protected String contextPathString; - Map packagePaths= new HashMap<>(); + protected Map packagePaths = new HashMap<>(); @Override public SourceVersion getSupportedSourceVersion() { @@ -50,7 +50,6 @@ public synchronized void init(ProcessingEnvironment processingEnv) { @Override public boolean process(Set annotations, RoundEnvironment round) { - var pathElements = round.getElementsAnnotatedWith(typeElement(PathPrism.PRISM_TYPE)); if (contextPathString == null) { @@ -74,9 +73,7 @@ public boolean process(Set annotations, RoundEnvironment readSecuritySchemes(round); } - final Set controllers = - round.getElementsAnnotatedWith(typeElement(ControllerPrism.PRISM_TYPE)); - for (final Element controller : controllers) { + for (final Element controller : round.getElementsAnnotatedWith(typeElement(ControllerPrism.PRISM_TYPE))) { writeAdapter(controller); } @@ -87,35 +84,26 @@ public boolean process(Set annotations, RoundEnvironment } private void readOpenApiDefinition(RoundEnvironment round) { - final Set elements = - round.getElementsAnnotatedWith(typeElement(OpenAPIDefinitionPrism.PRISM_TYPE)); - for (final Element element : elements) { + for (final Element element : round.getElementsAnnotatedWith(typeElement(OpenAPIDefinitionPrism.PRISM_TYPE))) { doc().readApiDefinition(element); } } private void readTagDefinitions(RoundEnvironment round) { - Set elements = - round.getElementsAnnotatedWith(typeElement(TagPrism.PRISM_TYPE)); - for (final Element element : elements) { + for (final Element element : round.getElementsAnnotatedWith(typeElement(TagPrism.PRISM_TYPE))) { doc().addTagDefinition(element); } - - elements = round.getElementsAnnotatedWith(typeElement(TagsPrism.PRISM_TYPE)); - for (final Element element : elements) { + for (final Element element : round.getElementsAnnotatedWith(typeElement(TagsPrism.PRISM_TYPE))) { doc().addTagsDefinition(element); } } private void readSecuritySchemes(RoundEnvironment round) { - Set elements = round.getElementsAnnotatedWith(typeElement(SecuritySchemePrism.PRISM_TYPE)); - for (final Element element : elements) { - doc().addSecurityScheme(element); + for (final Element element : round.getElementsAnnotatedWith(typeElement(SecuritySchemePrism.PRISM_TYPE))) { + doc().addSecurityScheme(element); } - - elements = round.getElementsAnnotatedWith(typeElement(SecuritySchemesPrism.PRISM_TYPE)); - for (final Element element : elements) { - doc().addSecuritySchemes(element); + for (final Element element : round.getElementsAnnotatedWith(typeElement(SecuritySchemesPrism.PRISM_TYPE))) { + doc().addSecuritySchemes(element); } } @@ -125,28 +113,26 @@ private void writeOpenAPI() { private void writeAdapter(Element controller) { if (controller instanceof TypeElement) { - - var packageFQN = elements().getPackageOf(controller).getQualifiedName().toString(); - var contextPath = - Util.combinePath( - contextPathString, - packagePaths.entrySet().stream() - .filter(k -> packageFQN.startsWith(k.getKey())) - .map(Entry::getValue) - .reduce(Util::combinePath) - .orElse(null)); - - final ControllerReader reader = new ControllerReader((TypeElement) controller, contextPath); + final var packageFQN = elements().getPackageOf(controller).getQualifiedName().toString(); + final var contextPath = Util.combinePath(contextPathString, packagePath(packageFQN)); + final var reader = new ControllerReader((TypeElement) controller, contextPath); reader.read(true); try { writeControllerAdapter(reader); } catch (final Throwable e) { - e.printStackTrace(); logError(reader.beanType(), "Failed to write $Route class " + e); } } } + private String packagePath(String packageFQN) { + return packagePaths.entrySet().stream() + .filter(k -> packageFQN.startsWith(k.getKey())) + .map(Entry::getValue) + .reduce(Util::combinePath) + .orElse(null); + } + /** * Write the adapter code for the given controller. */ diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java index 3aebc012..3914821c 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.Set; @@ -27,6 +26,7 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementFilter; + /** * Reads the type information for the Controller (bean). */ @@ -62,7 +62,7 @@ public ControllerReader(TypeElement beanType) { public ControllerReader(TypeElement beanType, String contextPath) { this.beanType = beanType; - this.contextPath=contextPath; + this.contextPath = contextPath; this.interfaces = initInterfaces(beanType); this.interfaceMethods = initInterfaceMethods(); this.roles = buildRoles(); @@ -73,10 +73,10 @@ public ControllerReader(TypeElement beanType, String contextPath) { this.producesPrism = initProduces(); this.apiResponses = buildApiResponses(); hasInstrument = - instrumentAllWebMethods() - || findAnnotation(InstrumentServerContextPrism::getOptionalOn) - .map(x -> true) - .orElse(false); + instrumentAllWebMethods() + || findAnnotation(InstrumentServerContextPrism::getOptionalOn) + .map(x -> true) + .orElse(false); } private List buildApiResponses() { @@ -89,11 +89,10 @@ private List buildApiResponses() { } private void buildApiResponsesFor(Element element, ArrayList responses) { - OpenAPIResponsesPrism.getOptionalOn(element).stream() - .map(OpenAPIResponsesPrism::value) - .flatMap(List::stream) - .forEach(responses::add); + .map(OpenAPIResponsesPrism::value) + .flatMap(List::stream) + .forEach(responses::add); responses.addAll(OpenAPIResponsePrism.getAllInstancesOn(element)); } @@ -182,7 +181,6 @@ private boolean initDocHidden() { } private boolean initHasValid() { - return findAnnotation(ValidPrism::getOptionalOn).isPresent(); } @@ -263,7 +261,7 @@ private void readSuper(TypeElement beanType) { final TypeMirror superclass = beanType.getSuperclass(); if (superclass.getKind() != TypeKind.NONE) { final DeclaredType declaredType = (DeclaredType) superclass; - final Element superElement = asElement(superclass); + final TypeElement superElement = asElement(superclass); if (!"java.lang.Object".equals(superElement.toString())) { for (final Element element : superElement.getEnclosedElements()) { if (element.getKind() == ElementKind.METHOD) { @@ -272,17 +270,13 @@ private void readSuper(TypeElement beanType) { readField(element); } } - if (superElement instanceof TypeElement) { - readSuper((TypeElement) superElement); - } + readSuper(superElement); } } } /** * Read methods from interfaces taking into account generics. - * - * @param interfaceElement */ private void readInterfaces(TypeElement interfaceElement) { for (final var element : ElementFilter.methodsIn(interfaceElement.getEnclosedElements())) { @@ -323,14 +317,13 @@ public List openApiResponses() { } public String path() { - var path = - findAnnotation(WebAPIPrism::getOptionalOn) - .map(WebAPIPrism::value) - .filter(not(String::isBlank)) - .or(() -> findAnnotation(PathPrism::getOptionalOn).map(PathPrism::value)) - .map(Util::trimPath) - .orElse(null); + findAnnotation(WebAPIPrism::getOptionalOn) + .map(WebAPIPrism::value) + .filter(not(String::isBlank)) + .or(() -> findAnnotation(PathPrism::getOptionalOn).map(PathPrism::value)) + .map(Util::trimPath) + .orElse(null); return Util.combinePath(contextPath, path); } diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java index fa52792d..129ae8cc 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java @@ -1,12 +1,6 @@ /** Generate the prisms to access annotation info */ -@GeneratePrism( - value = io.avaje.http.api.Controller.class, - publicAccess = true, - superInterfaces = WebAPIPrism.class) -@GeneratePrism( - value = io.avaje.http.api.Client.class, - publicAccess = true, - superInterfaces = WebAPIPrism.class) +@GeneratePrism(value = io.avaje.http.api.Controller.class, publicAccess = true, superInterfaces = WebAPIPrism.class) +@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true, superInterfaces = WebAPIPrism.class) @GeneratePrism(value = io.avaje.http.api.BeanParam.class, publicAccess = true) @GeneratePrism(value = io.avaje.http.api.Ignore.class, publicAccess = true) @GeneratePrism(value = io.avaje.http.api.QueryParam.class, publicAccess = true) @@ -31,18 +25,10 @@ @GeneratePrism(value = io.swagger.v3.oas.annotations.OpenAPIDefinition.class, publicAccess = true) @GeneratePrism(value = io.swagger.v3.oas.annotations.tags.Tag.class, publicAccess = true) @GeneratePrism(value = io.swagger.v3.oas.annotations.tags.Tags.class, publicAccess = true) -@GeneratePrism( - value = io.swagger.v3.oas.annotations.security.SecurityScheme.class, - publicAccess = true) -@GeneratePrism( - value = io.swagger.v3.oas.annotations.security.SecuritySchemes.class, - publicAccess = true) -@GeneratePrism( - value = io.swagger.v3.oas.annotations.security.SecurityRequirement.class, - publicAccess = true) -@GeneratePrism( - value = io.swagger.v3.oas.annotations.security.SecurityRequirements.class, - publicAccess = true) +@GeneratePrism(value = io.swagger.v3.oas.annotations.security.SecurityScheme.class, publicAccess = true) +@GeneratePrism(value = io.swagger.v3.oas.annotations.security.SecuritySchemes.class, publicAccess = true) +@GeneratePrism(value = io.swagger.v3.oas.annotations.security.SecurityRequirement.class, publicAccess = true) +@GeneratePrism(value = io.swagger.v3.oas.annotations.security.SecurityRequirements.class, publicAccess = true) @GeneratePrism(value = io.avaje.http.api.OpenAPIResponse.class, publicAccess = true) @GeneratePrism(value = io.avaje.http.api.OpenAPIResponses.class, publicAccess = true) @GeneratePrism(value = io.swagger.v3.oas.annotations.Hidden.class, publicAccess = true)