From c93ba507f7418f23cb314bf9a5eaf136b34a50ed Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 31 Jan 2023 12:56:17 +0100 Subject: [PATCH] Qute ValueResolverGenerator - also use gizmo switch for fields --- .../generatedresolvers/HierarchyTest.java | 3 + .../generator/ValueResolverGenerator.java | 68 +++++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/generatedresolvers/HierarchyTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/generatedresolvers/HierarchyTest.java index cd2eaf69664f8..a8288591c856d 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/generatedresolvers/HierarchyTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/generatedresolvers/HierarchyTest.java @@ -67,6 +67,9 @@ public String getName() { @TemplateData public static class Bar extends Foo { + // getName() takes precedence + public final String name = "Manny"; + @Override public String getName() { return "bar"; diff --git a/independent-projects/qute/generator/src/main/java/io/quarkus/qute/generator/ValueResolverGenerator.java b/independent-projects/qute/generator/src/main/java/io/quarkus/qute/generator/ValueResolverGenerator.java index 3e1a4d5456b0b..fe00046e02921 100644 --- a/independent-projects/qute/generator/src/main/java/io/quarkus/qute/generator/ValueResolverGenerator.java +++ b/independent-projects/qute/generator/src/main/java/io/quarkus/qute/generator/ValueResolverGenerator.java @@ -348,11 +348,13 @@ private boolean implementResolve(ClassCreator valueResolver, String clazzName, C } } - BytecodeCreator zeroParamsBranch = resolve.ifZero(paramsCount).trueBranch(); + if (!noParamMethods.isEmpty() || !fields.isEmpty()) { + + BytecodeCreator zeroParamsBranch = resolve.ifZero(paramsCount).trueBranch(); - if (!noParamMethods.isEmpty()) { Switch.StringSwitch nameSwitch = zeroParamsBranch.stringSwitch(name); Set matchedNames = new HashSet<>(); + for (MethodKey methodKey : noParamMethods) { // No params - just invoke the method if the name matches MethodInfo method = methodKey.method; @@ -390,34 +392,42 @@ public void accept(BytecodeCreator bc) { }; nameSwitch.caseOf(matchingNames, invokeMethod); } - } - for (FieldInfo field : fields) { - String getterName = fieldToGetterFun != null ? fieldToGetterFun.apply(field) : null; - if (getterName != null && noneMethodMatches(methods, getterName)) { - LOGGER.debugf("Forced getter added: %s", field); - BytecodeCreator getterMatch = zeroParamsBranch.createScope(); - // Match the getter name - BytecodeCreator notMatched = getterMatch.ifTrue(Gizmo.equals(getterMatch, getterMatch.load(getterName), - name)).falseBranch(); - // Match the property name - notMatched.ifTrue(Gizmo.equals(notMatched, notMatched.load(field.name()), - name)).falseBranch().breakScope(getterMatch); - ResultHandle value = getterMatch.invokeVirtualMethod( - MethodDescriptor.ofMethod(clazz.name().toString(), getterName, - DescriptorUtils.typeToString(field.type())), - base); - getterMatch.returnValue(getterMatch.invokeStaticMethod(Descriptors.COMPLETED_STAGE, value)); - } else { - LOGGER.debugf("Field added: %s", field); - // Match field name - BytecodeCreator fieldMatch = zeroParamsBranch - .ifTrue(Gizmo.equals(zeroParamsBranch, resolve.load(field.name()), name)) - .trueBranch(); - ResultHandle value = fieldMatch - .readInstanceField(FieldDescriptor.of(clazzName, field.name(), field.type().name().toString()), - base); - fieldMatch.returnValue(fieldMatch.invokeStaticMethod(Descriptors.COMPLETED_STAGE, value)); + for (FieldInfo field : fields) { + String getterName = fieldToGetterFun != null ? fieldToGetterFun.apply(field) : null; + if (getterName != null && noneMethodMatches(methods, getterName) && matchedNames.add(getterName)) { + LOGGER.debugf("Forced getter added: %s", field); + List matching; + if (matchedNames.add(field.name())) { + matching = List.of(getterName, field.name()); + } else { + matching = List.of(getterName); + } + Consumer invokeMethod = new Consumer() { + @Override + public void accept(BytecodeCreator bc) { + ResultHandle value = bc.invokeVirtualMethod( + MethodDescriptor.ofMethod(clazz.name().toString(), getterName, + DescriptorUtils.typeToString(field.type())), + base); + bc.returnValue(bc.invokeStaticMethod(Descriptors.COMPLETED_STAGE, value)); + } + }; + nameSwitch.caseOf(matching, invokeMethod); + } else if (matchedNames.add(field.name())) { + LOGGER.debugf("Field added: %s", field); + Consumer invokeMethod = new Consumer() { + @Override + public void accept(BytecodeCreator bc) { + ResultHandle value = bc.readInstanceField( + FieldDescriptor.of(clazzName, field.name(), field.type().name().toString()), + base); + ResultHandle ret = bc.invokeStaticMethod(Descriptors.COMPLETED_STAGE, value); + bc.returnValue(ret); + } + }; + nameSwitch.caseOf(field.name(), invokeMethod); + } } }