@Test public void buildMethods() throws Exception { underTest.addProperty("foo", TypeName.INT) .addProperty("bar", TypeName.OBJECT) .addProperty("baz", TypeName.BOOLEAN); final List<MethodSpec> actual = underTest.buildMethods(); assertThat(actual) .hasSize(4) .doesNotContainNull(); assertThat(actual.get(0)) .isNotConstructor() .hasName("getFoo") .hasAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", "foo") .build()) .isPublic() .hasReturnType(TypeName.INT) .hasNoParameter() .hasNoException() .hasStatement("return foo"); assertThat(actual.get(1)) .isNotConstructor() .hasName("getBar") .hasAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", "bar") .build()) .isPublic() .hasReturnType(TypeName.OBJECT) .hasNoParameter() .hasNoException() .hasStatement("return bar"); assertThat(actual.get(2)) .isNotConstructor() .hasName("isBaz") .hasAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", "baz") .build()) .isPublic() .hasReturnType(TypeName.BOOLEAN) .hasNoParameter() .hasNoException() .hasStatement("return baz"); }
@Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(JsonCreator.class) .build()); final ImmutableList.Builder<MethodSpec> builder = ImmutableList.builder(); getProperties().entrySet().forEach(property -> { final String name = property.getKey(); final TypeName type = property.getValue(); final String fieldName = fieldNamePolicy.convert(name, type); final String methodName = methodNamePolicy.convert(name, type); builder.add(MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(type) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); }
JacksonClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(JsonCreator.class) .build()); final ImmutableList.Builder<MethodSpec> builder = ImmutableList.builder(); getProperties().entrySet().forEach(property -> { final String name = property.getKey(); final TypeName type = property.getValue(); final String fieldName = fieldNamePolicy.convert(name, type); final String methodName = methodNamePolicy.convert(name, type); builder.add(MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(type) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } }
JacksonClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(JsonCreator.class) .build()); final ImmutableList.Builder<MethodSpec> builder = ImmutableList.builder(); getProperties().entrySet().forEach(property -> { final String name = property.getKey(); final TypeName type = property.getValue(); final String fieldName = fieldNamePolicy.convert(name, type); final String methodName = methodNamePolicy.convert(name, type); builder.add(MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(type) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } JacksonClassBuilder(@Nonnull NamePolicy fieldNamePolicy,
                               @Nonnull NamePolicy methodNamePolicy,
                               @Nonnull NamePolicy parameterNamePolicy); }
JacksonClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(JsonCreator.class) .build()); final ImmutableList.Builder<MethodSpec> builder = ImmutableList.builder(); getProperties().entrySet().forEach(property -> { final String name = property.getKey(); final TypeName type = property.getValue(); final String fieldName = fieldNamePolicy.convert(name, type); final String methodName = methodNamePolicy.convert(name, type); builder.add(MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(type) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } JacksonClassBuilder(@Nonnull NamePolicy fieldNamePolicy,
                               @Nonnull NamePolicy methodNamePolicy,
                               @Nonnull NamePolicy parameterNamePolicy);  }
JacksonClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(JsonCreator.class) .build()); final ImmutableList.Builder<MethodSpec> builder = ImmutableList.builder(); getProperties().entrySet().forEach(property -> { final String name = property.getKey(); final TypeName type = property.getValue(); final String fieldName = fieldNamePolicy.convert(name, type); final String methodName = methodNamePolicy.convert(name, type); builder.add(MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(type) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .addAnnotation(AnnotationSpec.builder(JsonProperty.class) .addMember("value", "$S", name) .build()) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } JacksonClassBuilder(@Nonnull NamePolicy fieldNamePolicy,
                               @Nonnull NamePolicy methodNamePolicy,
                               @Nonnull NamePolicy parameterNamePolicy);   }