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

Various suggestions #1277

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 @@ -350,6 +350,24 @@ private static boolean isPropertyInRoot(final String property, final String root
return false;
}

public <T> T getLeafValue(final String path,
final Class<T> valueRawType,
final Class<? extends Converter<T>> valueConvertWith) {
usedProperties.add(path);
Converter<T> valueConverter = valueConvertWith == null ? config.requireConverter(valueRawType)
: getConverterInstance(valueConvertWith);
return config.getValue(path, valueConverter);
}

public <T> Optional<T> getLeafOptionalValue(final String path,
final Class<T> valueRawType,
final Class<? extends Converter<T>> valueConvertWith) {
usedProperties.add(path);
Converter<T> valueConverter = valueConvertWith == null ? config.requireConverter(valueRawType)
: getConverterInstance(valueConvertWith);
return config.getOptionalValue(path, valueConverter);
}

@SuppressWarnings("unchecked")
public class ObjectCreator<T> {
private T root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.security.PrivilegedAction;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -109,6 +110,7 @@ public class ConfigMappingGenerator {
private static final String I_STRING_BUILDER = getInternalName(StringBuilder.class);
private static final String I_STRING = getInternalName(String.class);
private static final String I_ITERABLE = getInternalName(Iterable.class);
private static final String I_OPTIONAL = getInternalName(Optional.class);

private static final int V_THIS = 0;
private static final int V_MAPPING_CONTEXT = 1;
Expand Down Expand Up @@ -435,21 +437,49 @@ private static void addProperties(
ctor.visitTryCatchBlock(_try, _catch, _catch, I_RUNTIME_EXCEPTION);

appendPropertyName(ctor, property);
ctor.visitVarInsn(ALOAD, V_THIS);
ctor.visitTypeInsn(NEW, I_OBJECT_CREATOR);
ctor.visitInsn(DUP);
ctor.visitVarInsn(ALOAD, V_MAPPING_CONTEXT);
ctor.visitVarInsn(ALOAD, V_STRING_BUILDER);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "toString", "()L" + I_STRING + ';', false);
ctor.visitMethodInsn(INVOKESPECIAL, I_OBJECT_CREATOR, "<init>", "(L" + I_MAPPING_CONTEXT + ";L" + I_STRING + ";)V",
false);
if (property.isLeaf() || property.isPrimitive()) {
// try
ctor.visitLabel(_try);
ctor.visitVarInsn(ALOAD, V_THIS);
ctor.visitVarInsn(ALOAD, V_MAPPING_CONTEXT);
ctor.visitVarInsn(ALOAD, V_STRING_BUILDER);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "toString", "()L" + I_STRING + ';', false);

Class<?> rawType = property.isLeaf() ? property.asLeaf().getValueRawType()
: property.asPrimitive().getBoxType();
ctor.visitLdcInsn(Type.getType(rawType));
if (property.hasConvertWith() || property.isLeaf() && property.asLeaf().hasConvertWith()) {
ctor.visitLdcInsn(getType(
property.isLeaf() ? property.asLeaf().getConvertWith() : property.asPrimitive().getConvertWith()));
} else {
ctor.visitInsn(ACONST_NULL);
}
if (property.isOptional()) {
ctor.visitMethodInsn(INVOKEVIRTUAL, I_MAPPING_CONTEXT, "getLeafOptionalValue",
"(L" + I_STRING + ";L" + I_CLASS + ";L" + I_CLASS + ";)L" + I_OPTIONAL + ";", false);
} else {
ctor.visitMethodInsn(INVOKEVIRTUAL, I_MAPPING_CONTEXT, "getLeafValue",
"(L" + I_STRING + ";L" + I_CLASS + ";L" + I_CLASS + ";)L" + I_OBJECT + ";", false);
}
} else {
ctor.visitVarInsn(ALOAD, V_THIS);
ctor.visitTypeInsn(NEW, I_OBJECT_CREATOR);
ctor.visitInsn(DUP);
ctor.visitVarInsn(ALOAD, V_MAPPING_CONTEXT);
ctor.visitVarInsn(ALOAD, V_STRING_BUILDER);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_STRING_BUILDER, "toString", "()L" + I_STRING + ';', false);
ctor.visitMethodInsn(INVOKESPECIAL, I_OBJECT_CREATOR, "<init>",
"(L" + I_MAPPING_CONTEXT + ";L" + I_STRING + ";)V",
false);

// try
ctor.visitLabel(_try);

// try
ctor.visitLabel(_try);
generateComplexProperty(ctor, property);

generateProperty(ctor, property);
ctor.visitMethodInsn(INVOKEVIRTUAL, I_OBJECT_CREATOR, "get", "()L" + I_OBJECT + ";", false);
}

ctor.visitMethodInsn(INVOKEVIRTUAL, I_OBJECT_CREATOR, "get", "()L" + I_OBJECT + ";", false);
if (property.isPrimitive()) {
PrimitiveProperty primitive = property.asPrimitive();
ctor.visitTypeInsn(CHECKCAST, getInternalName(primitive.getBoxType()));
Expand Down Expand Up @@ -491,7 +521,7 @@ private static void addProperties(
}
}

private static void generateProperty(final MethodVisitor ctor, final Property property) {
private static void generateComplexProperty(final MethodVisitor ctor, final Property property) {
if (property.isLeaf() || property.isPrimitive() || property.isLeaf() && property.isOptional()) {
Class<?> rawType = property.isLeaf() ? property.asLeaf().getValueRawType() : property.asPrimitive().getBoxType();
ctor.visitLdcInsn(Type.getType(rawType));
Expand Down Expand Up @@ -645,7 +675,7 @@ private static void generateProperty(final MethodVisitor ctor, final Property pr
ctor.visitLdcInsn(getType(collectionProperty.getCollectionRawType()));
ctor.visitMethodInsn(INVOKEVIRTUAL, I_OBJECT_CREATOR, "optionalCollection",
"(L" + I_CLASS + ";)L" + I_OBJECT_CREATOR + ";", false);
generateProperty(ctor, collectionProperty.getElement());
generateComplexProperty(ctor, collectionProperty.getElement());
}
} else {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -677,13 +707,13 @@ private static void unwrapProperty(final MethodVisitor ctor, final Property prop
ctor.visitMethodInsn(INVOKEVIRTUAL, I_OBJECT_CREATOR, "map",
"(L" + I_CLASS + ";L" + I_CLASS + ";L" + I_STRING + ";L" + I_ITERABLE + ";)L" + I_OBJECT_CREATOR + ";",
false);
generateProperty(ctor, mapProperty.getValueProperty());
generateComplexProperty(ctor, mapProperty.getValueProperty());
} else if (property.isCollection()) {
CollectionProperty collectionProperty = property.asCollection();
ctor.visitLdcInsn(getType(collectionProperty.getCollectionRawType()));
ctor.visitMethodInsn(INVOKEVIRTUAL, I_OBJECT_CREATOR, "collection", "(L" + I_CLASS + ";)L" + I_OBJECT_CREATOR + ";",
false);
generateProperty(ctor, collectionProperty.getElement());
generateComplexProperty(ctor, collectionProperty.getElement());
} else {
throw new UnsupportedOperationException();
}
Expand Down
Loading
Loading