From 06f4006ebdb0f4f38aeeb423950810badc50af66 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 28 Nov 2024 17:21:16 +0100 Subject: [PATCH] Qute: fix perf optimization in ReflectionValueResolver - fixes #44797 --- .../io/quarkus/qute/MethodsCandidate.java | 2 +- .../quarkus/qute/ReflectionResolverTest.java | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/independent-projects/qute/core/src/main/java/io/quarkus/qute/MethodsCandidate.java b/independent-projects/qute/core/src/main/java/io/quarkus/qute/MethodsCandidate.java index 6a6ea99d24e2e..10004a56e421e 100644 --- a/independent-projects/qute/core/src/main/java/io/quarkus/qute/MethodsCandidate.java +++ b/independent-projects/qute/core/src/main/java/io/quarkus/qute/MethodsCandidate.java @@ -62,7 +62,7 @@ public CompletionStage getValue(Object instance) { public boolean isShared(EvalContext context) { if (methods.size() == 1) { for (Expression param : context.getParams()) { - if (param.isLiteral()) { + if (!param.isLiteral()) { return false; } } diff --git a/independent-projects/qute/core/src/test/java/io/quarkus/qute/ReflectionResolverTest.java b/independent-projects/qute/core/src/test/java/io/quarkus/qute/ReflectionResolverTest.java index 3ade80aa110ce..8703056047a4a 100644 --- a/independent-projects/qute/core/src/test/java/io/quarkus/qute/ReflectionResolverTest.java +++ b/independent-projects/qute/core/src/test/java/io/quarkus/qute/ReflectionResolverTest.java @@ -57,13 +57,24 @@ public void testStaticMembersIgnored() { @Test public void testCachedResolver() { Template template = Engine.builder().addDefaults().addValueResolver(new ReflectionValueResolver()).build() - .parse("{foo.name}"); - Expression exp = template.findExpression(e -> e.toOriginalString().equals("foo.name")); - PartImpl part = (PartImpl) exp.getParts().get(1); - assertNull(part.cachedResolver); - assertEquals("box", template.data("foo", new Foo("box")).render()); - assertNotNull(part.cachedResolver); - assertTrue(part.cachedResolver instanceof ReflectionValueResolver.AccessorResolver); + .parse("{foo.name}::{foo.name.repeat(5)}::{foo.name.repeat(n)}"); + Expression fooName = template.findExpression(e -> e.toOriginalString().equals("foo.name")); + Expression fooNameRepeat5 = template.findExpression(e -> e.toOriginalString().equals("foo.name.repeat(5)")); + Expression fooNameRepeatN = template.findExpression(e -> e.toOriginalString().equals("foo.name.repeat(n)")); + PartImpl fooNamePart = (PartImpl) fooName.getParts().get(1); + PartImpl fooNameRepeat5Part = (PartImpl) fooNameRepeat5.getParts().get(2); + PartImpl fooNameRepeatNPart = (PartImpl) fooNameRepeatN.getParts().get(2); + assertNull(fooNamePart.cachedResolver); + assertNull(fooNameRepeat5Part.cachedResolver); + assertNull(fooNameRepeatNPart.cachedResolver); + assertEquals("box::boxboxboxboxbox::box", template.data("foo", new Foo("box"), "n", 1).render()); + assertEquals("box::boxboxboxboxbox::boxbox", template.data("foo", new Foo("box"), "n", 2).render()); + assertNotNull(fooNamePart.cachedResolver); + assertNotNull(fooNameRepeat5Part.cachedResolver); + assertNotNull(fooNameRepeatNPart.cachedResolver); + assertTrue(fooNamePart.cachedResolver instanceof ReflectionValueResolver.AccessorResolver); + assertTrue(fooNameRepeat5Part.cachedResolver instanceof ReflectionValueResolver.AccessorResolver); + assertTrue(fooNameRepeatNPart.cachedResolver instanceof ReflectionValueResolver.CandidateResolver); } public static class Foo {