use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class WebServiceControllerBuilder method buildMethod.
private String buildMethod() {
CodeBuilder builder = new CodeBuilder();
builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
List<String> params = Lists.newArrayList();
Annotation[][] annotations = method.getParameterAnnotations();
Type[] paramTypes = method.getGenericParameterTypes();
for (int i = 0; i < annotations.length; i++) {
Type paramType = paramTypes[i];
String paramTypeLiteral = type(paramType);
PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
if (pathParam != null) {
params.add(pathParam.value());
builder.indent(1).append("{} {} = ({}) request.pathParam(\"{}\", {});\n", paramTypeLiteral, pathParam.value(), paramTypeLiteral, pathParam.value(), variable(paramType));
} else {
params.add("bean");
builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramTypeLiteral, paramTypeLiteral, variable(paramType));
}
}
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.equals(method.getReturnType())) {
builder.indent(1).append("return {}.empty().status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
} else {
builder.indent(1).append("return {}.bean(response).status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
}
builder.append("}");
return builder.build();
}
use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class ObjectValidatorBuilder method validateObjectMethod.
private String validateObjectMethod(Class<?> beanClass, String parentPath) {
String methodName = "validate" + beanClass.getSimpleName() + (index++);
CodeBuilder builder = new CodeBuilder().append("private void {}({} bean, {} errors, boolean partial) {\n", methodName, type(beanClass), type(ValidationErrors.class));
for (Field field : Classes.instanceFields(beanClass)) {
if (!hasValidationAnnotation(field))
continue;
validateAnnotations(field);
Type fieldType = field.getGenericType();
Class<?> fieldClass = GenericTypes.rawClass(fieldType);
String pathLiteral = variable(path(field, parentPath));
builder.indent(1).append("if (bean.{} == null) {\n", field.getName());
NotNull notNull = field.getDeclaredAnnotation(NotNull.class);
if (notNull != null)
builder.indent(2).append("if (!partial) errors.add({}, {});\n", pathLiteral, variable(notNull.message()));
builder.indent(1).append("} else {\n");
if (String.class.equals(fieldClass)) {
buildStringValidation(builder, field, pathLiteral);
} else if (GenericTypes.isList(fieldType)) {
buildListValidation(builder, field, pathLiteral, parentPath);
} else if (GenericTypes.isMap(fieldType)) {
buildMapValidation(builder, field, pathLiteral, parentPath);
} else if (Number.class.isAssignableFrom(fieldClass)) {
buildNumberValidation(builder, field, pathLiteral);
} else if (!isValueClass(fieldClass)) {
String method = validateObjectMethod(fieldClass, path(field, parentPath));
builder.indent(2).append("{}(bean.{}, errors, partial);\n", method, field.getName());
}
builder.indent(1).append("}\n");
}
builder.append('}');
this.builder.addMethod(builder.build());
return methodName;
}
use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.
the class QueryParamMapperBuilder method toBeanMethod.
private String toBeanMethod() {
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();
if (String.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = (String)params.get(\"{}\");\n", fieldName, name);
} else if (Integer.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toInt((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (Long.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toLong((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (Double.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toDouble((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (BigDecimal.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toBigDecimal((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (Boolean.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toBoolean((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (LocalDateTime.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toDateTime((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (LocalDate.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toDate((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (ZonedDateTime.class.equals(fieldClass)) {
builder.indent(1).append("bean.{} = {}.toZonedDateTime((String)params.get(\"{}\"));\n", fieldName, helper, name);
} else if (fieldClass.isEnum()) {
builder.indent(1).append("bean.{} = ({}){}.toEnum((String)params.get(\"{}\"), {});\n", fieldName, type(fieldClass), helper, name, variable(fieldClass));
}
}
builder.indent(1).append("return bean;\n");
builder.append("}");
return builder.build();
}
Aggregations