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

HttpClientRequest.method() + format + tidy #300

Merged
merged 1 commit into from
Oct 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {
this.errorMapper = context.errorMapper();
}

@Override
public String method() {
return method;
}
Expand Down Expand Up @@ -808,7 +809,7 @@ boolean isSkipAuthToken() {
return skipAuthToken;
}

private class ListenerEvent implements RequestListener.Event {
private class ListenerEvent implements RequestListener.Event {

@Override
public long responseTimeMicros() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
@SupportedOptions({"useJavax", "useSingleton", "instrumentRequests","disableDirectWrites"})
public abstract class BaseProcessor extends AbstractProcessor {

String contextPathString;
protected String contextPathString;

Map<String, String> packagePaths= new HashMap<>();
protected Map<String, String> packagePaths = new HashMap<>();

@Override
public SourceVersion getSupportedSourceVersion() {
Expand All @@ -50,7 +50,6 @@ public synchronized void init(ProcessingEnvironment processingEnv) {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) {

var pathElements = round.getElementsAnnotatedWith(typeElement(PathPrism.PRISM_TYPE));

if (contextPathString == null) {
Expand All @@ -74,9 +73,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
readSecuritySchemes(round);
}

final Set<? extends Element> controllers =
round.getElementsAnnotatedWith(typeElement(ControllerPrism.PRISM_TYPE));
for (final Element controller : controllers) {
for (final Element controller : round.getElementsAnnotatedWith(typeElement(ControllerPrism.PRISM_TYPE))) {
writeAdapter(controller);
}

Expand All @@ -87,35 +84,26 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}

private void readOpenApiDefinition(RoundEnvironment round) {
final Set<? extends Element> 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<? extends Element> 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<? extends Element> 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);
}
}

Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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).
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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<OpenAPIResponsePrism> buildApiResponses() {
Expand All @@ -89,11 +89,10 @@ private List<OpenAPIResponsePrism> buildApiResponses() {
}

private void buildApiResponsesFor(Element element, ArrayList<OpenAPIResponsePrism> 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));
}
Expand Down Expand Up @@ -182,7 +181,6 @@ private boolean initDocHidden() {
}

private boolean initHasValid() {

return findAnnotation(ValidPrism::getOptionalOn).isPresent();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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())) {
Expand Down Expand Up @@ -323,14 +317,13 @@ public List<OpenAPIResponsePrism> 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down