use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class ObjectValidatorBuilder method build.
public Optional<ObjectValidator> build() {
Class<?> targetClass = unwrapInstanceType();
if (Classes.instanceFields(targetClass).stream().noneMatch(this::hasValidationAnnotation))
return Optional.empty();
builder = new DynamicInstanceBuilder<>(ObjectValidator.class, targetClass.getTypeName() + "$ObjectValidator");
String method = validateObjectMethod(targetClass, null);
CodeBuilder builder = new CodeBuilder().append("public void validate(Object instance, {} errors, boolean partial) {\n", type(ValidationErrors.class));
if (GenericTypes.isList(instanceType)) {
builder.indent(1).append("java.util.List list = (java.util.List) instance;\n").indent(1).append("for (java.util.Iterator iterator = list.iterator(); iterator.hasNext(); ) {\n").indent(2).append("{} value = ({}) iterator.next();\n", type(targetClass), type(targetClass)).indent(2).append("if (value != null) {}(value, errors, partial);\n", method).indent(1).append("}\n");
} else {
builder.indent(1).append("{}(({}) instance, errors, partial);\n", method, type(instanceType));
}
builder.append('}');
this.builder.addMethod(builder.build());
return Optional.of(this.builder.build());
}
use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class TypescriptDefinitionBuilder method build.
public String build() {
CodeBuilder builder = new CodeBuilder();
namespaces.forEach((name, namespace) -> {
builder.append("export namespace ").append(name).append(" {\n");
buildTypes(builder, namespace.typeDefinitions);
buildEnums(builder, namespace.enumDefinitions);
buildServices(builder, namespace.serviceDefinitions);
builder.append("}\n");
});
return builder.build();
}
use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class QueryParamMapperBuilder method toParamsMethod.
private String toParamsMethod() {
CodeBuilder builder = new CodeBuilder().append("public java.util.Map toParams(Object value) {\n");
builder.indent(1).append("java.util.Map params = new java.util.HashMap();\n");
builder.indent(1).append("{} bean = ({})value;\n", type(beanClass), type(beanClass));
for (Field field : Classes.instanceFields(beanClass)) {
String fieldName = field.getName();
Class<?> fieldClass = field.getType();
String name = field.getDeclaredAnnotation(QueryParam.class).name();
if (String.class.equals(fieldClass)) {
builder.indent(1).append("params.put(\"{}\", bean.{});\n", name, fieldName);
} else {
builder.indent(1).append("params.put(\"{}\", {}.toString(bean.{}));\n", name, helper, fieldName);
}
}
builder.indent(1).append("return params;\n");
builder.append("}");
return builder.build();
}
use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class ExpressionBuilder method buildEval.
private String buildEval() {
CodeBuilder builder = new CodeBuilder();
builder.append("public Object eval({} context) {\n", type(TemplateContext.class));
String rootClassLiteral = type(context.rootClass);
builder.indent(1).append("{} $root = ({})context.root;\n", rootClassLiteral, rootClassLiteral);
context.paramClasses.forEach((name, paramClass) -> {
String paramClassLiteral = type(paramClass);
builder.indent(1).append("{} {} = ({})context.context(\"{}\");\n", paramClassLiteral, name, paramClassLiteral, name);
});
String translatedExpression = new ExpressionTranslator(token, context).translate();
builder.indent(1).append("return {};\n", translatedExpression);
builder.append("}");
return builder.build();
}
use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class EntityDecoderBuilder method decodeEntityMethod.
private String decodeEntityMethod(Class<?> entityClass) {
String methodName = decodeMethods.get(entityClass);
if (methodName != null)
return methodName;
methodName = "decode" + entityClass.getSimpleName() + (index++);
CodeBuilder builder = new CodeBuilder();
builder.append("public {} {}(org.bson.BsonReader reader, {} wrapper, String parentField) {\n", type(entityClass), methodName, type(BsonReaderWrapper.class)).indent(1).append("boolean hasContent = wrapper.startReadEntity(parentField);\n").indent(1).append("if (!hasContent) return null;\n").indent(1).append("{} entity = new {}();\n", type(entityClass), type(entityClass)).indent(1).append("reader.readStartDocument();\n").indent(1).append("while (reader.readBsonType() != org.bson.BsonType.END_OF_DOCUMENT) {\n").indent(2).append("String fieldName = reader.readName();\n").indent(2).append("String fieldPath = parentField + \".\" + fieldName;\n");
for (Field field : Classes.instanceFields(entityClass)) {
builder.indent(2).append("if ({}.equals(fieldName)) {\n", variable(mongoField(field)));
String variable = decodeValue(builder, field.getGenericType(), 3);
builder.indent(3).append("entity.{} = {};\n", field.getName(), variable);
builder.indent(3).append("continue;\n").indent(2).append("}\n");
}
builder.indent(2).append("logger.warn({}, fieldPath, reader.getCurrentBsonType());\n", variable("undefined field, field={}, type={}"));
builder.indent(2).append("reader.skipValue();\n");
builder.indent(1).append("}\n");
builder.indent(1).append("reader.readEndDocument();\n");
builder.indent(1).append("return entity;\n");
builder.append('}');
this.builder.addMethod(builder.build());
decodeMethods.put(entityClass, methodName);
return methodName;
}
Aggregations