use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.
the class BeanValidatorBuilder method build.
@Nullable
public BeanValidator build() {
validate(beanClass);
if (Classes.instanceFields(beanClass).stream().noneMatch(this::hasValidationAnnotation))
return null;
builder = new DynamicInstanceBuilder<>(BeanValidator.class, beanClass.getSimpleName());
String method = validateMethod(beanClass, null);
var code = new CodeBuilder().append("public void validate(Object instance, {} errors, boolean partial) {\n", type(ValidationErrors.class));
code.indent(1).append("{}(({}) instance, errors, partial);\n", method, type(beanClass));
code.append('}');
builder.addMethod(code.build());
return builder.build();
}
use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.
the class WebServiceControllerBuilder method buildMethod.
private String buildMethod(boolean deprecated) {
var builder = new CodeBuilder();
builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
if (deprecated)
builder.indent(1).append("{}.logDeprecation({});\n", type(WebServiceController.class), variable(Methods.path(method)));
Annotation[][] annotations = method.getParameterAnnotations();
List<String> params = new ArrayList<>(annotations.length);
Class<?>[] paramClasses = method.getParameterTypes();
for (int i = 0; i < annotations.length; i++) {
Class<?> paramClass = paramClasses[i];
String paramClassLiteral = type(paramClass);
PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
if (pathParam != null) {
params.add("$" + pathParam.value());
if (String.class.equals(paramClass)) {
builder.indent(1).append("String ${} = request.pathParam(\"{}\");\n", pathParam.value(), pathParam.value());
} else if (Integer.class.equals(paramClass)) {
builder.indent(1).append("Integer ${} = {}.toInt(request.pathParam(\"{}\"));\n", pathParam.value(), type(PathParamHelper.class), pathParam.value());
} else if (Long.class.equals(paramClass)) {
builder.indent(1).append("Long ${} = {}.toLong(request.pathParam(\"{}\"));\n", pathParam.value(), type(PathParamHelper.class), pathParam.value());
} else if (paramClass.isEnum()) {
builder.indent(1).append("{} ${} = ({}){}.toEnum(request.pathParam(\"{}\"), {});\n", paramClassLiteral, pathParam.value(), paramClassLiteral, type(PathParamHelper.class), pathParam.value(), variable(paramClass));
} else {
throw new Error("not supported path param type, type=" + paramClass.getCanonicalName());
}
} else {
params.add("bean");
builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramClassLiteral, paramClassLiteral, variable(paramClass));
}
}
if (void.class == method.getReturnType()) {
builder.indent(1).append("delegate.{}(", method.getName());
} else {
builder.indent(1).append("{} response = delegate.{}(", type(method.getReturnType()), method.getName());
}
builder.appendCommaSeparatedValues(params).append(");\n");
if (void.class == method.getReturnType()) {
builder.indent(1).append("return {}.empty()", type(Response.class));
} else {
builder.indent(1).append("return {}.bean(response)", type(Response.class));
}
ResponseStatus status = method.getDeclaredAnnotation(ResponseStatus.class);
if (status == null) {
builder.append(";\n");
} else {
builder.append(".status({});\n", variable(status.value()));
}
builder.append("}");
return builder.build();
}
use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.
the class QueryParamReaderBuilder method fromParamsMethod.
private String fromParamsMethod() {
String beanClassLiteral = type(beanClass);
CodeBuilder builder = new CodeBuilder().append("public Object fromParams(java.util.Map params) {\n");
builder.indent(1).append("{} bean = new {}();\n", beanClassLiteral, beanClassLiteral);
for (Field field : Classes.instanceFields(beanClass)) {
String fieldName = field.getName();
Class<?> fieldClass = field.getType();
String name = field.getDeclaredAnnotation(QueryParam.class).name();
builder.indent(1).append("String ${} = (String)params.get(\"{}\");\n", fieldName, name);
// query param won't have null values, null means the key doesn't exist
builder.indent(1).append("if (${} != null) {\n", fieldName);
if (String.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toString(${});\n", fieldName, helper, fieldName);
} else if (Integer.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toInt(${});\n", fieldName, helper, fieldName);
} else if (Long.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toLong(${});\n", fieldName, helper, fieldName);
} else if (Double.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toDouble(${});\n", fieldName, helper, fieldName);
} else if (BigDecimal.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toBigDecimal(${});\n", fieldName, helper, fieldName);
} else if (Boolean.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toBoolean(${});\n", fieldName, helper, fieldName);
} else if (LocalDateTime.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toDateTime(${});\n", fieldName, helper, fieldName);
} else if (LocalDate.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toDate(${});\n", fieldName, helper, fieldName);
} else if (LocalTime.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toTime(${});\n", fieldName, helper, fieldName);
} else if (ZonedDateTime.class.equals(fieldClass)) {
builder.indent(2).append("bean.{} = {}.toZonedDateTime(${});\n", fieldName, helper, fieldName);
} else if (fieldClass.isEnum()) {
builder.indent(2).append("bean.{} = ({}){}.toEnum(${}, {});\n", fieldName, type(fieldClass), helper, fieldName, variable(fieldClass));
}
builder.indent(1).append("}\n", name);
}
builder.indent(1).append("return bean;\n");
builder.append("}");
return builder.build();
}
use of core.framework.internal.asm.CodeBuilder in project core-ng-project by neowu.
the class QueryParamWriterBuilder 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.internal.asm.CodeBuilder in project core-ng-project by neowu.
the class EntitySchemaGenerator method schemeStatements.
private List<String> schemeStatements() {
List<String> statements = Lists.newArrayList();
var builder = new CodeBuilder().append("CREATE TABLE ");
Table table = entityClass.getDeclaredAnnotation(Table.class);
builder.append(table.name()).append(" (");
List<String> primaryKeys = Lists.newArrayList();
for (Field field : Classes.instanceFields(entityClass)) {
Column column = field.getDeclaredAnnotation(Column.class);
PrimaryKey primaryKey = field.getDeclaredAnnotation(PrimaryKey.class);
builder.append(column.name()).append(' ');
builder.append(columnType(field.getType(), field.getDeclaredAnnotation(Size.class)));
if (primaryKey != null) {
if (primaryKey.autoIncrement())
builder.append(" AUTO_INCREMENT");
primaryKeys.add(column.name());
}
if (field.isAnnotationPresent(NotNull.class)) {
builder.append(" NOT NULL");
}
builder.append(", ");
}
builder.append("PRIMARY KEY(").appendCommaSeparatedValues(primaryKeys).append("))");
statements.add(builder.build());
return statements;
}
Aggregations