@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") .hasNoAnnotation() .isPublic() .hasReturnType(TypeName.INT) .hasNoParameter() .hasNoException() .hasStatement("return foo"); assertThat(actual.get(1)) .isNotConstructor() .hasName("getBar") .hasNoAnnotation() .isPublic() .hasReturnType(TypeName.OBJECT) .hasNoParameter() .hasNoException() .hasStatement("return bar"); assertThat(actual.get(2)) .isNotConstructor() .hasName("isBaz") .hasNoAnnotation() .isPublic() .hasReturnType(TypeName.BOOLEAN) .hasNoParameter() .hasNoException() .hasStatement("return baz"); assertThat(actual.get(3)) .isConstructor() .isPublic() .hasNoAnnotation() .hasNoException() .hasParameter(TypeName.INT, "foo") .hasParameter(TypeName.OBJECT, "bar") .hasParameter(TypeName.BOOLEAN, "baz") .hasCode(CodeBlock.builder() .addStatement("this.$1L = $1L", "foo") .addStatement("this.$1L = $1L", "bar") .addStatement("this.$1L = $1L", "baz") .build()); }
@Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC); 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) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); }
MoshiClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC); 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) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } }
MoshiClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC); 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) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } MoshiClassBuilder(@Nonnull NamePolicy fieldNamePolicy,
                             @Nonnull NamePolicy methodNamePolicy,
                             @Nonnull NamePolicy parameterNamePolicy); }
MoshiClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC); 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) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } MoshiClassBuilder(@Nonnull NamePolicy fieldNamePolicy,
                             @Nonnull NamePolicy methodNamePolicy,
                             @Nonnull NamePolicy parameterNamePolicy);  }
MoshiClassBuilder extends ClassBuilder { @Nonnull @Override protected List<MethodSpec> buildMethods() { final MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC); 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) .addStatement("return $L", fieldName) .build()); final String propertyName = parameterNamePolicy.convert(name, type); constructorBuilder.addParameter(ParameterSpec.builder(type, propertyName) .build()) .addStatement("this.$L = $L", fieldName, propertyName); }); builder.add(constructorBuilder.build()); return builder.build(); } MoshiClassBuilder(@Nonnull NamePolicy fieldNamePolicy,
                             @Nonnull NamePolicy methodNamePolicy,
                             @Nonnull NamePolicy parameterNamePolicy);   }