Skip to content

Commit

Permalink
Qute ValueResolverGenerator - also use gizmo switch for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Jan 31, 2023
1 parent 9609ed2 commit c93ba50
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> matchedNames = new HashSet<>();

for (MethodKey methodKey : noParamMethods) {
// No params - just invoke the method if the name matches
MethodInfo method = methodKey.method;
Expand Down Expand Up @@ -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<String> matching;
if (matchedNames.add(field.name())) {
matching = List.of(getterName, field.name());
} else {
matching = List.of(getterName);
}
Consumer<BytecodeCreator> invokeMethod = new Consumer<BytecodeCreator>() {
@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<BytecodeCreator> invokeMethod = new Consumer<BytecodeCreator>() {
@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);
}
}
}

Expand Down

0 comments on commit c93ba50

Please sign in to comment.