use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.
the class GetResolverGenerator method createMapFromCursorMethodSpec.
@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName).addCode("\n");
for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
final JavaType javaType = columnMeta.javaType;
final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
final boolean isBoxed = javaType.isBoxedType();
if (isBoxed) {
// otherwise -> if primitive and value from cursor null -> fail early
builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
}
builder.addStatement("object.$L = cursor.$L", columnMeta.elementName, getFromCursor);
if (isBoxed) {
builder.endControlFlow();
}
}
return builder.addCode("\n").addStatement("return object").build();
}
use of com.squareup.javapoet.MethodSpec in project graylog2-server by Graylog2.
the class WithBeanGetterExtension method generateGetterMethod.
private MethodSpec generateGetterMethod(String name, ExecutableElement element) {
final TypeName returnType = ClassName.get(element.getReturnType());
final String prefix = isBoolean(returnType) ? "is" : "get";
final String getterName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
final MethodSpec.Builder builder = MethodSpec.methodBuilder(prefix + getterName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addAnnotation(AnnotationSpec.builder(JsonIgnore.class).build()).addStatement("return $N()", name).returns(returnType);
// Copy all annotations but @JsonProperty, @JsonIgnore, and @Override to the new method.
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
final TypeName annotationType = ClassName.get(annotationMirror.getAnnotationType());
if (SKIP_ANNOTATIONS.contains(annotationType)) {
continue;
}
builder.addAnnotation(AnnotationSpec.get(annotationMirror));
}
return builder.build();
}
use of com.squareup.javapoet.MethodSpec in project raml-for-jax-rs by mulesoft-labs.
the class Jsr303ResourceExtension method onMethod.
@Override
public MethodSpec.Builder onMethod(ResourceContext context, GMethod method, GRequest gRequest, MethodSpec.Builder methodSpec) {
MethodSpec spec = methodSpec.build();
MethodSpec.Builder builder = MethodSpec.methodBuilder(spec.name);
builder.addAnnotations(spec.annotations);
if (spec.code != null) {
builder.addCode(spec.code);
}
if (spec.defaultValue != null) {
builder.defaultValue(spec.defaultValue);
}
builder.addExceptions(spec.exceptions);
if (spec.javadoc != null) {
builder.addJavadoc("$L", spec.javadoc);
}
builder.addModifiers(spec.modifiers);
for (ParameterSpec parameter : spec.parameters) {
if (!(parameter.type.isPrimitive() || parameter.type.isBoxedPrimitive() || parameter.type.withoutAnnotations().toString().equals("java.lang.String"))) {
builder.addParameter(parameter.toBuilder().addAnnotation(Valid.class).build());
} else {
builder.addParameter(parameter);
}
}
if (spec.returnType != null) {
builder.returns(spec.returnType);
}
builder.addTypeVariables(spec.typeVariables);
builder.varargs(spec.varargs);
return builder;
}
use of com.squareup.javapoet.MethodSpec in project kripton by xcesco.
the class SqlModifyBuilder method generate.
/**
* @param elementUtils
* @param builder
* @param method
* @param updateMode
*/
public static void generate(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
ModifyType updateResultType = detectModifyType(method, method.jql.operationType);
// if true, field must be associate to ben attributes
TypeName returnType = method.getReturnClass();
if (updateResultType == null) {
throw (new InvalidMethodSignException(method));
}
// generate method code
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
ParameterSpec parameterSpec;
for (Pair<String, TypeName> item : method.getParameters()) {
parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
methodBuilder.addParameter(parameterSpec);
}
methodBuilder.returns(returnType);
// generate inner code
updateResultType.generate(classBuilder, methodBuilder, method, returnType);
MethodSpec methodSpec = methodBuilder.build();
classBuilder.addMethod(methodSpec);
if (method.contentProviderEntryPathEnabled) {
// delete-select, update-select can be used with content provider
// insert-select no
// if (method.jql.containsSelectOperation && updateResultType==) {
// AssertKripton.failWithInvalidMethodSignException(true, method, "
// SQL with inner SELECT can not be used in content provider");
// }
generateModifierForContentProvider(BaseProcessor.elementUtils, classBuilder, method, updateResultType);
}
}
use of com.squareup.javapoet.MethodSpec in project kripton by xcesco.
the class BindM2MBuilder method generateSelects.
private void generateSelects(M2MEntity entity, String packageName) {
String idPart = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.idName);
String methodName = "";
String workId = "";
methodName = "selectBy" + idPart;
if (!isMethodAlreadyDefined(entity, methodName)) {
// @formatter:off
MethodSpec methodSpec = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(BindSqlSelect.class).addMember("where", "$S", entity.idName + "=${" + entity.idName + "}").build()).addParameter(ParameterSpec.builder(Long.TYPE, entity.idName).addAnnotation(AnnotationSpec.builder(BindSqlParam.class).addMember("value", "$S", entity.idName).build()).build()).returns(TypeUtility.className(packageName, entity.name)).build();
// @formatter:on
classBuilder.addMethod(methodSpec);
}
methodName = entity.entity1Name.simpleName() + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.idName);
workId = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, methodName);
methodName = "selectBy" + methodName;
if (!isMethodAlreadyDefined(entity, methodName)) {
// @formatter:off
MethodSpec methodSpec = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(BindSqlSelect.class).addMember("where", "$S", workId + "=${" + workId + "}").build()).addParameter(ParameterSpec.builder(Long.TYPE, workId).addAnnotation(AnnotationSpec.builder(BindSqlParam.class).addMember("value", "$S", workId).build()).build()).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), TypeUtility.className(packageName, entity.name))).build();
// @formatter:on
classBuilder.addMethod(methodSpec);
}
methodName = entity.entity2Name.simpleName() + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.idName);
workId = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, methodName);
methodName = "selectBy" + methodName;
if (!isMethodAlreadyDefined(entity, methodName)) {
// @formatter:off
MethodSpec methodSpec = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addAnnotation(AnnotationSpec.builder(BindSqlSelect.class).addMember("where", "$S", workId + "=${" + workId + "}").build()).addParameter(ParameterSpec.builder(Long.TYPE, workId).addAnnotation(AnnotationSpec.builder(BindSqlParam.class).addMember("value", "$S", workId).build()).build()).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), TypeUtility.className(packageName, entity.name))).build();
// @formatter:on
classBuilder.addMethod(methodSpec);
}
}
Aggregations