Skip to content

Commit

Permalink
Qute parameter declarations - autoimport java.lang if needed
Browse files Browse the repository at this point in the history
- resolves quarkusio#20975
  • Loading branch information
mkouba committed Oct 26, 2021
1 parent e6275f1 commit 5995096
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ private static ClassInfo getClassInfo(String val, IndexView index, Function<Stri
DotName rawClassName = rawClassName(val);
ClassInfo clazz = index.getClassByName(rawClassName);
if (clazz == null) {
throw new TemplateException(
"Class [" + rawClassName + "] used in the parameter declaration in template ["
+ templateIdToPathFun.apply(expressionOrigin.getTemplateGeneratedId()) + "] on line "
+ expressionOrigin.getLine()
+ " was not found in the application index. Make sure it is spelled correctly.");
if (!rawClassName.toString().contains(".")) {
// Try the java.lang prefix for a name without package
rawClassName = DotName.createSimple(Types.JAVA_LANG_PREFIX + rawClassName.toString());
clazz = index.getClassByName(rawClassName);
}
if (clazz == null) {
throw new TemplateException(
"Class [" + rawClassName + "] used in the parameter declaration in template ["
+ templateIdToPathFun.apply(expressionOrigin.getTemplateGeneratedId()) + "] on line "
+ expressionOrigin.getLine()
+ " was not found in the application index. Make sure it is spelled correctly.");
}
}
return clazz;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

public final class Types {

static final String JAVA_LANG_PREFIX = "java.lang.";

static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> resolvedTypeParameters,
IndexView index) {
Set<Type> types = new HashSet<>();
Expand Down Expand Up @@ -76,11 +78,25 @@ static <T extends Type> Map<TypeVariable, Type> buildResolvedMap(List<T> resolve
}

static Type resolveTypeParam(Type typeParam, Map<TypeVariable, Type> resolvedTypeParameters, IndexView index) {
if (typeParam.kind() == Kind.TYPE_VARIABLE) {
if (typeParam.kind() == Kind.CLASS) {
ClassInfo classInfo = index.getClassByName(typeParam.name());
if (classInfo == null && !typeParam.name().toString().contains(".")) {
// If not indexed and no package then try the java.lang prefix
classInfo = index.getClassByName(DotName.createSimple(JAVA_LANG_PREFIX + typeParam.name().toString()));
if (classInfo != null) {
return Type.create(classInfo.name(), Kind.CLASS);
}
}
return typeParam;
} else if (typeParam.kind() == Kind.TYPE_VARIABLE) {
return resolvedTypeParameters.getOrDefault(typeParam, typeParam);
} else if (typeParam.kind() == Kind.PARAMETERIZED_TYPE) {
ParameterizedType parameterizedType = typeParam.asParameterizedType();
ClassInfo classInfo = index.getClassByName(parameterizedType.name());
if (classInfo == null && !parameterizedType.name().toString().contains(".")) {
// If not indexed and no package then try the java.lang prefix
classInfo = index.getClassByName(DotName.createSimple(JAVA_LANG_PREFIX + parameterizedType.name().toString()));
}
if (classInfo != null) {
List<TypeVariable> typeParameters = classInfo.typeParameters();
List<Type> arguments = parameterizedType.arguments();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.qute.deployment.typesafe;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.Template;
import io.quarkus.test.QuarkusUnitTest;

public class TypeSafeJavaLangTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(Movie.class)
.addAsResource(new StringAsset("{@String movie}"
+ "{@java.util.List<Integer> movies}"
+ "{#if movie.toLowerCase is 'foo'}"
+ "Foo movie!"
+ "{/if}"
+ "::{movies.get(0).intValue}"
+ ""), "templates/foo.html"));

@Inject
Template foo;

@Test
public void testValidation() {
assertEquals("Foo movie!::42",
foo.data("movie", "foo", "movies", List.of(42)).render());
}

}

0 comments on commit 5995096

Please sign in to comment.