Skip to content

Commit

Permalink
Mark Generated Avaje HttpClient implementations as Closeable (#734)
Browse files Browse the repository at this point in the history
* mark generated avaje clients as autocloseable

* Format only

---------

Co-authored-by: Rob Bygrave <[email protected]>
  • Loading branch information
SentryMan and rbygrave authored Nov 19, 2024
1 parent cfc8399 commit aac8204
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
9 changes: 9 additions & 0 deletions inject-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
<artifactId>avaje-spi-service</artifactId>
<version>2.8</version>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-api</artifactId>
<version>2.8</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>io.avaje</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ final class Constants {
static final String QUALIFIER = "jakarta.inject.Qualifier";
static final String NAMED = "jakarta.inject.Named";

static final String PATH = "io.avaje.http.api.Path";
static final String CONTROLLER = "io.avaje.http.api.Controller";
static final String HTTP_GENERATED = "io.avaje.http.api.Generated";

static final String AT_SINGLETON = "@Singleton";
static final String AT_PROXY = "@Proxy";
static final String AT_GENERATED = "@Generated(\"io.avaje.inject.generator\")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class IncludeAnnotations {

static {
EXCLUDED_ANNOTATIONS.add(Constants.KOTLIN_METADATA);
EXCLUDED_ANNOTATIONS.add(Constants.PATH);
EXCLUDED_ANNOTATIONS.add(PathPrism.PRISM_TYPE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
AssistFactoryPrism.PRISM_TYPE,
ComponentPrism.PRISM_TYPE,
Constants.TESTSCOPE,
Constants.CONTROLLER,
ControllerPrism.PRISM_TYPE,
ExternalPrism.PRISM_TYPE,
FactoryPrism.PRISM_TYPE,
ImportPrism.PRISM_TYPE,
Expand Down Expand Up @@ -150,7 +150,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

readImported(importedElements(roundEnv));

maybeElements(roundEnv, Constants.CONTROLLER).ifPresent(this::readBeans);
maybeElements(roundEnv, ControllerPrism.PRISM_TYPE).ifPresent(this::readBeans);
maybeElements(roundEnv, ProxyPrism.PRISM_TYPE).ifPresent(this::readBeans);
maybeElements(roundEnv, AssistFactoryPrism.PRISM_TYPE).ifPresent(this::readAssisted);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.avaje.inject.generator.APContext.logWarn;
import static io.avaje.inject.generator.APContext.types;
import static io.avaje.inject.generator.ProcessingContext.asElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -50,27 +51,28 @@ final class TypeExtendsReader {
this.baseTypeIsInterface = baseType.getKind() == ElementKind.INTERFACE;
this.publicAccess = baseType.getModifiers().contains(Modifier.PUBLIC);
this.proxyBean = proxyBean;
this.controller = hasAnnotation(Constants.CONTROLLER);
this.controller = ControllerPrism.isPresent(baseType);
this.closeable = closeableClient(baseType);
this.autoProvide = autoProvide();
}

/**
* generated Avaje Http Clients are autoCloseable on JDK 21+
*/
private boolean closeableClient(TypeElement baseType) {
return ClientPrism.isPresent(baseType)
&& APContext.typeElement("io.avaje.http.client.HttpClient").getInterfaces().stream()
.map(Object::toString)
.anyMatch(AutoCloseable.class.getCanonicalName()::equals);
}

private boolean autoProvide() {
return publicAccess
&& !controller
&& !FactoryPrism.isPresent(baseType)
&& !ProxyPrism.isPresent(baseType);
}

private boolean hasAnnotation(String annotationFQN) {
for (final var m : baseType.getAnnotationMirrors()) {
final CharSequence mfqn = ((TypeElement) m.getAnnotationType().asElement()).getQualifiedName();
if (annotationFQN.contentEquals(mfqn)) {
return true;
}
}
return false;
}

UType baseType() {
return baseUType;
}
Expand Down Expand Up @@ -124,7 +126,7 @@ List<UType> autoProvides() {
// http controller, or request scoped controller via BeanFactory
return List.of();
}
if (hasAnnotation(Constants.HTTP_GENERATED)) {
if (HttpGeneratedPrism.isPresent(baseType)) {
// http route
return providesTypes.stream()
.filter(ut -> ROUTE_TYPES.contains(ut.mainType()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
@GeneratePrism(Scope.class)
@GeneratePrism(Secondary.class)
@GeneratePrism(io.avaje.spi.ServiceProvider.class)
@GeneratePrism(io.avaje.http.api.Client.class)
@GeneratePrism(io.avaje.http.api.Controller.class)
@GeneratePrism(io.avaje.http.api.Path.class)
@GeneratePrism(name = "HttpGeneratedPrism", value = io.avaje.http.api.Generated.class)
package io.avaje.inject.generator;

import io.avaje.inject.*;
Expand Down
1 change: 1 addition & 0 deletions inject-generator/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
requires io.avaje.inject.aop;
requires io.avaje.inject.events;

requires static io.avaje.http.api;
requires static io.avaje.prism;
requires static io.avaje.spi;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void exclude_di_annotations() {
assertFalse(include(Constants.FACTORY));
assertFalse(include(Constants.PRIMARY));
assertFalse(include(Constants.SECONDARY));
assertFalse(include(Constants.PATH));
assertFalse(include(PathPrism.PRISM_TYPE));
}

}

0 comments on commit aac8204

Please sign in to comment.