Search in sources :

Example 6 with CodeBuilder

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());
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 7 with CodeBuilder

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();
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 8 with CodeBuilder

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();
}
Also used : Field(java.lang.reflect.Field) QueryParam(core.framework.api.web.service.QueryParam) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 9 with CodeBuilder

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();
}
Also used : TemplateContext(core.framework.impl.template.TemplateContext) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 10 with CodeBuilder

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;
}
Also used : Field(java.lang.reflect.Field) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

CodeBuilder (core.framework.impl.asm.CodeBuilder)23 Field (java.lang.reflect.Field)9 Type (java.lang.reflect.Type)3 PathParam (core.framework.api.web.service.PathParam)2 QueryParam (core.framework.api.web.service.QueryParam)2 Column (core.framework.db.Column)2 LocalDate (java.time.LocalDate)2 NotNull (core.framework.api.validate.NotNull)1 Path (core.framework.api.web.service.Path)1 PrimaryKey (core.framework.db.PrimaryKey)1 Table (core.framework.db.Table)1 HTTPMethod (core.framework.http.HTTPMethod)1 DynamicInstanceBuilder (core.framework.impl.asm.DynamicInstanceBuilder)1 TemplateContext (core.framework.impl.template.TemplateContext)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 BigDecimal (java.math.BigDecimal)1 Function (java.util.function.Function)1 ObjectId (org.bson.types.ObjectId)1 Logger (org.slf4j.Logger)1