Skip to content

[jex-generator] use body stream methods #578

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 3 commits into from
Mar 17, 2025
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 @@ -311,7 +311,7 @@ private void readSuper(TypeElement beanType) {
if (superclass.getKind() != TypeKind.NONE) {
final DeclaredType declaredType = (DeclaredType) superclass;
final TypeElement superElement = asElement(superclass);
if (!"java.lang.Object".equals(superElement.toString())) {
if (!"java.lang.Object".equals(superElement.getQualifiedName().toString())) {
for (final Element element : superElement.getEnclosedElements()) {
if (element.getKind() == ElementKind.METHOD) {
readMethod((ExecutableElement) element, declaredType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.generator.jex;

import java.util.List;
import java.util.stream.Collectors;

import io.avaje.http.generator.core.Append;
import io.avaje.http.generator.core.Constants;
Expand All @@ -9,6 +10,7 @@
import io.avaje.http.generator.core.PlatformAdapter;
import io.avaje.http.generator.core.ProcessingContext;
import io.avaje.http.generator.core.UType;
import io.avaje.http.generator.core.Util;

class JexAdapter implements PlatformAdapter {

Expand All @@ -31,19 +33,59 @@ public boolean isBodyMethodParam() {

@Override
public String bodyAsClass(UType type) {

if ("java.io.InputStream".equals(type.full())) {
return "ctx.bodyAsInputStream()";
} else if ("java.lang.String".equals(type.full())) {
return "ctx.body()";
} else if ("byte[]".equals(type.full())) {
return "ctx.bodyAsBytes()";
} else if (type.isGeneric() && ProcessingContext.useJsonb()) {
return "ctx.<%s>bodyAsType(%s)".formatted(type.shortType(), writeJsonbType(type));
} else if (ProcessingContext.useJsonb()) {
return type.shortName() + "JsonType.fromJson(ctx.bodyAsInputStream())";
return "ctx.bodyAsClass(%s.class)".formatted(type.shortType());
}
return "ctx.bodyAsClass(" + type.mainType() + ".class)";
}

public static String writeJsonbType(UType type) {
var writer = new StringBuilder();

switch (type.mainType()) {
case "java.util.List":
writeType(type.paramRaw(), writer);
writer.append(".list()");
break;
case "java.util.Set":
writeType(type.paramRaw(), writer);
writer.append(".set()");
break;
case "java.util.Map":
writeType(type.paramRaw(), writer);
writer.append(".map()");
break;
default: {
if (type.mainType().contains("java.util")) {
throw new UnsupportedOperationException(
"Only java.util Map, Set and List are supported JsonB Controller Collection Types");
}
writeType(type, writer);
}
}
return writer.toString();
}

static void writeType(UType type, StringBuilder writer) {
final var params =
type.params().stream()
.map(Util::shortName)
.map(s -> "?".equals(s) ? "Object" : s)
.collect(Collectors.joining(".class, "));

writer.append(
"Types.newParameterizedType(%s.class, %s.class)"
.formatted(Util.shortName(type.mainType()), params));
}

@Override
public String indent() {
return " ";
Expand Down