Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved support for defaults and indexed properties in ConfigMappings. #550

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private static void addProperties(
if (property.isCollection() || realProperty.isCollection() && optional) {
ctor.visitVarInsn(ALOAD, V_THIS);
// append property name
boolean restoreLength = appendPropertyName(ctor, mapping, property);
boolean restoreLength = appendPropertyName(ctor, property);

ctor.visitVarInsn(ALOAD, V_MAPPING_CONTEXT);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_MAPPING_CONTEXT, "getConfig", "()L" + I_SMALLRYE_CONFIG + ';', false);
Expand Down Expand Up @@ -456,29 +456,64 @@ private static void addProperties(
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "length", "()I", false);
ctor.visitVarInsn(ISTORE, 9);

// append collection index
ctor.visitVarInsn(ALOAD, V_STRING_BUILDER);
ctor.visitLdcInsn('[');
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append", "(C)L" + I_STRING_BUILDER + ';',
false);
// construct collection index
ctor.visitTypeInsn(NEW, I_STRING_BUILDER);
ctor.visitInsn(DUP);
ctor.visitMethodInsn(INVOKESPECIAL, I_STRING_BUILDER, "<init>", "()V", false);
ctor.visitLdcInsn("[");
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_STRING + ";)L" + I_STRING_BUILDER + ";", false);
ctor.visitVarInsn(ALOAD, 8);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_OBJECT + ";)L" + I_STRING_BUILDER + ';', false);
ctor.visitLdcInsn(']');
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append", "(C)L" + I_STRING_BUILDER + ';',
"(L" + I_OBJECT + ";)L" + I_STRING_BUILDER + ";", false);
ctor.visitLdcInsn("]");
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_STRING + ";)L" + I_STRING_BUILDER + ";", false);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "toString", "()L" + I_STRING + ";",
false);
ctor.visitVarInsn(ASTORE, 10);

// append collection index
ctor.visitVarInsn(ALOAD, V_STRING_BUILDER);
ctor.visitVarInsn(ALOAD, 10);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_STRING + ";)L" + I_STRING_BUILDER + ";", false);
ctor.visitInsn(POP);

// add Group with indexed properties as a Collection element
ctor.visitVarInsn(ALOAD, 5);
// create group
ctor.visitVarInsn(ALOAD, V_MAPPING_CONTEXT);
ctor.visitLdcInsn(
getType(realProperty.asCollection().getElement().asGroup().getGroupType().getInterfaceType()));
ctor.visitMethodInsn(INVOKEVIRTUAL, I_MAPPING_CONTEXT, "constructGroup",
"(L" + I_CLASS + ";)L" + I_OBJECT + ';', false);
ctor.visitVarInsn(ASTORE, 11);

// add group to collection
ctor.visitVarInsn(ALOAD, 5);
ctor.visitTypeInsn(CHECKCAST, I_COLLECTION);
ctor.visitVarInsn(ALOAD, 11);
ctor.visitMethodInsn(INVOKEINTERFACE, I_COLLECTION, "add", "(L" + I_OBJECT + ";)Z", true);
ctor.visitInsn(POP);

// register indexed enclosing element
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);
ctor.visitLdcInsn(Type.getType(mapping.getInterfaceType()));
ctor.visitTypeInsn(NEW, I_STRING_BUILDER);
ctor.visitInsn(DUP);
ctor.visitMethodInsn(INVOKESPECIAL, I_STRING_BUILDER, "<init>", "()V", false);
ctor.visitLdcInsn(property.getPropertyName());
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_STRING + ";)L" + I_STRING_BUILDER + ";", false);
ctor.visitVarInsn(ALOAD, 10);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "append",
"(L" + I_STRING + ";)L" + I_STRING_BUILDER + ";", false);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "toString", "()L" + I_STRING + ";", false);
ctor.visitVarInsn(ALOAD, V_THIS);
ctor.visitVarInsn(ALOAD, 11);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_MAPPING_CONTEXT, "registerEnclosedField",
"(L" + I_CLASS + ";L" + I_STRING + ";L" + I_OBJECT + ";L" + I_OBJECT + ";)V",
false);

// reset sb without index
ctor.visitVarInsn(ALOAD, V_STRING_BUILDER);
ctor.visitVarInsn(ILOAD, 9);
Expand Down Expand Up @@ -595,7 +630,7 @@ private static void addProperties(
fio.visitLabel(_done);
} else if (property.isGroup()) {
// stack: -
boolean restoreLength = appendPropertyName(ctor, mapping, property);
boolean restoreLength = appendPropertyName(ctor, property);
// stack: -
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);
// stack: ctxt
Expand Down Expand Up @@ -627,7 +662,7 @@ private static void addProperties(
// stack: -
ctor.visitVarInsn(Opcodes.ALOAD, V_THIS);
// stack: this
boolean restoreLength = appendPropertyName(ctor, mapping, property);
boolean restoreLength = appendPropertyName(ctor, property);
ctor.visitVarInsn(Opcodes.ALOAD, V_MAPPING_CONTEXT);
ctor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, I_MAPPING_CONTEXT, "getConfig", "()L" + I_SMALLRYE_CONFIG + ';',
false);
Expand Down Expand Up @@ -753,8 +788,7 @@ private static void addProperties(
}
}

private static boolean appendPropertyName(final MethodVisitor ctor, final ConfigMappingInterface mapping,
final Property property) {
private static boolean appendPropertyName(final MethodVisitor ctor, final Property property) {
if (property.isParentPropertyName()) {
return false;
}
Expand Down
Loading