Skip to content

Format code in client generator only #432

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

Merged
merged 1 commit into from
May 8, 2024
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 @@ -42,6 +42,7 @@ public static void writeAnnotations(Append writer, Element element, String inden
}
}

@SuppressWarnings("unchecked")
private static void writeVal(final StringBuilder sb, final AnnotationValue annotationValue) {
final var value = annotationValue.getValue();
// handle array values
Expand All @@ -50,7 +51,6 @@ private static void writeVal(final StringBuilder sb, final AnnotationValue annot
boolean first = true;

for (final AnnotationValue listValue : (List<AnnotationValue>) value) {

if (!first) {
sb.append(", ");
}
Expand All @@ -61,16 +61,13 @@ private static void writeVal(final StringBuilder sb, final AnnotationValue annot
sb.append("}");
// Handle enum values
} else if (value instanceof VariableElement) {

final var element = (VariableElement) value;

final var type = UType.parse(element.asType());
sb.append(type.full() + "." + element.toString());
sb.append(type.full()).append('.').append(element);
// handle annotation values
} else if (value instanceof AnnotationMirror) {

} else if (value instanceof AnnotationMirror) {
final var mirror = (AnnotationMirror) value;

final String annotationName = mirror.getAnnotationType().toString();
sb.append("@").append(annotationName).append("(");
boolean first = true;
Expand All @@ -83,10 +80,9 @@ private static void writeVal(final StringBuilder sb, final AnnotationValue annot
writeVal(sb, entry.getValue());
first = false;
}

sb.append(")");
} else {
sb.append(annotationValue.toString());
sb.append(annotationValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ final class ClientMethodWriter {

this.presetHeaders =
Stream.concat(
HeadersPrism.getOptionalOn(element).stream(),
HeadersPrism.getOptionalOn(element.getEnclosingElement()).stream())
.map(HeadersPrism::value)
.map(List::stream)
.flatMap(Function.identity())
.peek(
s -> {
HeadersPrism.getOptionalOn(element).stream(),
HeadersPrism.getOptionalOn(element.getEnclosingElement()).stream())
.map(HeadersPrism::value)
.map(List::stream)
.flatMap(Function.identity())
.peek(s -> {
if (!s.contains(":")) {
logError(element, "@Headers value must have a \":\"", method);
}
})
.map(s -> s.split(":", 2))
.filter(a -> a.length == 2)
.map(a -> Map.entry(a[0].trim(), a[1].trim())).collect(toList());
.map(s -> s.split(":", 2))
.filter(a -> a.length == 2)
.map(a -> Map.entry(a[0].trim(), a[1].trim())).collect(toList());
}

void addImportTypes(ControllerReader reader) {
Expand Down Expand Up @@ -357,26 +356,18 @@ private void writeBody() {
private void writeErrorMapper() {
method.throwsList().stream()
.map(ProcessingContext::asElement)
.filter(
e ->
isAssignable2Interface(
e.getQualifiedName().toString(), "java.lang.RuntimeException"))
.filter(
e ->
ElementFilter.constructorsIn(e.getEnclosedElements()).stream()
.filter(c -> c.getParameters().size() == 1)
.map(c -> c.getParameters().get(0).asType().toString())
.map(Util::trimAnnotations)
.anyMatch("io.avaje.http.client.HttpException"::equals))
.filter(e -> isAssignable2Interface(e.getQualifiedName().toString(), "java.lang.RuntimeException"))
.filter(e ->
ElementFilter.constructorsIn(e.getEnclosedElements()).stream()
.filter(c -> c.getParameters().size() == 1)
.map(c -> c.getParameters().get(0).asType().toString())
.map(Util::trimAnnotations)
.anyMatch("io.avaje.http.client.HttpException"::equals))
.findFirst()
.map(TypeElement::getQualifiedName)
.map(Object::toString)
.map(Util::shortName)
.ifPresent(
exception ->
writer
.append(" .errorMapper(%s::new)", exception)
.eol());
.ifPresent(exception -> writer.append(" .errorMapper(%s::new)", exception).eol());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.avaje.http.generator.core.PlatformAdapter;
import io.avaje.http.generator.core.UType;

class ClientPlatformAdapter implements PlatformAdapter {
final class ClientPlatformAdapter implements PlatformAdapter {

@Override
public boolean isContextType(String rawType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ private void readModule() {

private void writeForImported(Element importedElement) {
ImportPrism.getInstanceOn(importedElement).types().stream()
.map(ProcessingContext::asElement)
.filter(Objects::nonNull)
.forEach(this::writeClient);
.map(ProcessingContext::asElement)
.filter(Objects::nonNull)
.forEach(this::writeClient);
}

private void writeClient(Element controller) {
Expand All @@ -94,7 +94,6 @@ private void writeClient(Element controller) {
try {
metaData.add(writeClientAdapter(reader));
} catch (final Exception e) {
e.printStackTrace();
logError(reader.beanType(), "Failed to write client class " + e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Write Http client adapter.
*/
class ClientWriter extends BaseControllerWriter {
final class ClientWriter extends BaseControllerWriter {

private static final String HTTP_CLIENT = "io.avaje.http.client.HttpClient";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ void initialiseFullName() {
fullName();
}

boolean contains(String type) {
return generatedClients.contains(type);
}

void add(String type) {
generatedClients.add(type);
}
Expand All @@ -40,10 +36,6 @@ String fullName() {
return fullName;
}

String packageName() {
return TopPackage.packageOf(fullName());
}

List<String> all() {
return generatedClients;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void read() {
private void readMetaData(TypeElement moduleType) {
for (final AnnotationMirror annotationMirror : moduleType.getAnnotationMirrors()) {
MetaDataPrism.getOptional(annotationMirror).map(MetaDataPrism::value).stream()
.flatMap(List::stream)
.map(TypeMirror::toString)
.forEach(componentMetaData::add);
.flatMap(List::stream)
.map(TypeMirror::toString)
.forEach(componentMetaData::add);
}
}

Expand Down Expand Up @@ -83,7 +83,6 @@ private List<String> loadMetaInf() {
logDebug("FilerException reading services file");

} catch (final Exception e) {
e.printStackTrace();
logWarn("Error reading services file: " + e.getMessage());
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Known response types for Http Client methods.
*/
class KnownResponse {
final class KnownResponse {

private final Map<String, String> map = new HashMap<>();

Expand Down