Skip to content

Commit

Permalink
Fix JandexUtil#getBoxedTypeName() and move it to Qute extension
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Oct 13, 2022
1 parent a15594d commit 4608cc5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
Expand Up @@ -350,32 +350,6 @@ public static boolean isSubclassOf(IndexView index, ClassInfo info, DotName pare
return isSubclassOf(index, superClass, parentName);
}

@SuppressWarnings("incomplete-switch")
public static String getBoxedTypeName(Type type) {
switch (type.kind()) {
case PRIMITIVE:
switch (type.asPrimitiveType().primitive()) {
case BOOLEAN:
return "java.lang.Boolean";
case BYTE:
return "java.lang.Byte";
case CHAR:
return "java.lang.Character";
case DOUBLE:
return "java.lang.Double";
case FLOAT:
return "java.lang.Float";
case INT:
return "java.lang.Integer";
case LONG:
return "java.lang.Long";
case SHORT:
return "java.lang.Short";
}
}
return type.toString();
}

private static class ClassNotIndexedException extends RuntimeException {

private final DotName dotName;
Expand Down
Expand Up @@ -85,7 +85,6 @@
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.util.JandexUtil;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.maven.dependency.Dependency;
Expand Down Expand Up @@ -368,7 +367,7 @@ && isNotLocatedByCustomTemplateLocator(locatorPatternsBuildItem.getLocationPatte
throw new TemplateException("Parameter names not recorded for " + classInfo.name()
+ ": compile the class with -parameters");
}
bindings.put(name, JandexUtil.getBoxedTypeName(type));
bindings.put(name, getCheckedTemplateParameterTypeName(type));
parameterNames.add(name);
}
AnnotationValue requireTypeSafeExpressions = annotation.value(CHECKED_TEMPLATE_REQUIRE_TYPE_SAFE);
Expand Down Expand Up @@ -553,7 +552,7 @@ public Optional<Variant> getVariant() {
String name = MessageBundleProcessor.getParameterName(method, it.previousIndex());
msgBundleTemplateIdToParamDecl
.computeIfAbsent(messageBundleMethod.getTemplateId(), s -> new HashMap<>())
.put(name, new MethodParameterDeclaration(JandexUtil.getBoxedTypeName(paramType), name));
.put(name, new MethodParameterDeclaration(getCheckedTemplateParameterTypeName(paramType), name));
}
}

Expand All @@ -568,7 +567,7 @@ public void beforeParsing(ParserHelper parserHelper) {
// Set the bindings for globals first so that type-safe templates can override them
for (TemplateGlobalBuildItem global : globals) {
parserHelper.addParameter(global.getName(),
JandexUtil.getBoxedTypeName(global.getVariableType()).toString());
getCheckedTemplateParameterTypeName(global.getVariableType()).toString());
}

addMethodParamsToParserHelper(parserHelper, pathToPathWithoutSuffix.get(templateId),
Expand Down Expand Up @@ -716,6 +715,43 @@ void validateCheckedFragments(List<CheckedFragmentValidationBuildItem> validatio
}
}

@SuppressWarnings("incomplete-switch")
private static String getCheckedTemplateParameterTypeName(Type type) {
switch (type.kind()) {
case PARAMETERIZED_TYPE:
return getCheckedTemplateParameterParameterizedTypeName((ParameterizedType) type);
case ARRAY:
// in the case of an array, we get back to using Type#toString()
// otherwise, we end up with java.lang.[I] for int[]
return type.toString();
}
return type.name().toString();
}

private static String getCheckedTemplateParameterParameterizedTypeName(ParameterizedType parameterizedType) {
StringBuilder builder = new StringBuilder();

if (parameterizedType.owner() != null) {
builder.append(parameterizedType.owner().name());
builder.append('.');
builder.append(parameterizedType.name().local());
} else {
builder.append(parameterizedType.name());
}

List<Type> arguments = parameterizedType.arguments();
if (arguments.size() > 0) {
builder.append('<');
builder.append(getCheckedTemplateParameterTypeName(arguments.get(0)));
for (int i = 1; i < arguments.size(); i++) {
builder.append(", ").append(getCheckedTemplateParameterTypeName(arguments.get(i)));
}
builder.append('>');
}

return builder.toString();
}

private List<ParameterDeclaration> mergeParamDeclarations(List<ParameterDeclaration> parameterDeclarations,
Map<String, MethodParameterDeclaration> paramNameToDeclaration) {
if (paramNameToDeclaration != null) {
Expand Down

0 comments on commit 4608cc5

Please sign in to comment.