forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qute type-safe validation improvements
- validate expressions that start with a namespace coming from a TemplateData annotation - validate nested "inject:" namespace expressions, ie. used as method params - resolves quarkusio#20621 - also introduce io.quarkus.qute.TemplateData.SIMPLENAME and fix TemplateData javadoc
- Loading branch information
Showing
11 changed files
with
513 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
448 changes: 315 additions & 133 deletions
448
extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
...sions/qute/deployment/src/main/java/io/quarkus/qute/deployment/TemplateDataBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.quarkus.qute.deployment; | ||
|
||
import java.util.Arrays; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jboss.jandex.AnnotationTarget; | ||
import org.jboss.jandex.AnnotationTarget.Kind; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.FieldInfo; | ||
import org.jboss.jandex.MethodInfo; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
final class TemplateDataBuildItem extends MultiBuildItem { | ||
|
||
private final ClassInfo targetClass; | ||
private final String namespace; | ||
private final String[] ignore; | ||
private final Pattern[] ignorePatterns; | ||
private final boolean ignoreSuperclasses; | ||
private final boolean properties; | ||
|
||
public TemplateDataBuildItem(ClassInfo targetClass, String namespace, String[] ignore, boolean ignoreSuperclasses, | ||
boolean properties) { | ||
this.targetClass = targetClass; | ||
this.namespace = namespace; | ||
this.ignore = ignore; | ||
this.ignoreSuperclasses = ignoreSuperclasses; | ||
this.properties = properties; | ||
if (ignore.length > 0) { | ||
ignorePatterns = new Pattern[ignore.length]; | ||
for (int i = 0; i < ignore.length; i++) { | ||
ignorePatterns[i] = Pattern.compile(ignore[i]); | ||
} | ||
} else { | ||
ignorePatterns = null; | ||
} | ||
} | ||
|
||
public ClassInfo getTargetClass() { | ||
return targetClass; | ||
} | ||
|
||
public boolean hasNamespace() { | ||
return namespace != null; | ||
} | ||
|
||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
public String[] getIgnore() { | ||
return ignore; | ||
} | ||
|
||
public boolean isIgnoreSuperclasses() { | ||
return ignoreSuperclasses; | ||
} | ||
|
||
public boolean isProperties() { | ||
return properties; | ||
} | ||
|
||
boolean filter(AnnotationTarget target) { | ||
String name = null; | ||
if (target.kind() == Kind.METHOD) { | ||
MethodInfo method = target.asMethod(); | ||
if (properties && !method.parameters().isEmpty()) { | ||
return false; | ||
} | ||
name = method.name(); | ||
} else if (target.kind() == Kind.FIELD) { | ||
FieldInfo field = target.asField(); | ||
name = field.name(); | ||
} | ||
if (ignorePatterns != null) { | ||
for (int i = 0; i < ignorePatterns.length; i++) { | ||
if (ignorePatterns[i].matcher(name).matches()) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TemplateDataBuildItem [targetClass=" + targetClass + ", namespace=" + namespace + ", ignore=" | ||
+ Arrays.toString(ignore) + ", ignorePatterns=" + Arrays.toString(ignorePatterns) + ", ignoreSuperclasses=" | ||
+ ignoreSuperclasses + ", properties=" + properties + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../qute/deployment/src/test/java/io/quarkus/qute/deployment/TemplateDataValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkus.qute.deployment; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
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.TemplateData; | ||
import io.quarkus.qute.TemplateException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class TemplateDataValidationTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(MyClass.class) | ||
.addAsResource(new StringAsset( | ||
"{foo_My:BAZ}"), | ||
"templates/foo.txt")) | ||
.assertException(t -> { | ||
Throwable e = t; | ||
TemplateException te = null; | ||
while (e != null) { | ||
if (e instanceof TemplateException) { | ||
te = (TemplateException) e; | ||
break; | ||
} | ||
e = e.getCause(); | ||
} | ||
assertNotNull(te); | ||
assertTrue(te.getMessage().contains("Found template problems (1)"), te.getMessage()); | ||
assertTrue(te.getMessage().contains("foo_My:BAZ"), te.getMessage()); | ||
}); | ||
|
||
@Test | ||
public void test() { | ||
fail(); | ||
} | ||
|
||
@TemplateData(namespace = "foo_My") | ||
public static class MyClass { | ||
|
||
public static final String FOO = "foo"; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters