@Test public void testSetter(@TuneProvider Tune tune) { FieldSpec.Builder builder = FieldSpec.builder(TypeName.INT, "test"); FieldSpec fieldSpec = builder.build(); FieldModel model = mock(FieldModel.class); when(model.getFieldType()).thenReturn(FieldType.INTEGER); TypeSpec.Builder classBuilder = TypeSpec.classBuilder("Klazz"); new SetterHandler(tune, null, new DefaultRuleSet(tune, null)).process(classBuilder, fieldSpec, model); TypeSpec spec = classBuilder.build(); assertEquals(1, spec.methodSpecs.size()); MethodSpec methodSpec = spec.methodSpecs.get(0); assertEquals(1, methodSpec.parameters.size()); assertEquals(TypeName.INT, methodSpec.parameters.get(0).type); assertEquals("test", methodSpec.parameters.get(0).name); assertTrue(methodSpec.hasModifier((Modifier.PUBLIC))); assertEquals(TypeName.VOID, methodSpec.returnType); assertEquals("setTest", methodSpec.name); }
public void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel) { if (fieldModel.isImmutable()) { return; } String methodName = getGetterSetterName(fieldSpec.name, true, fieldModel.getFieldType() == FieldType.BOOLEAN, fieldModel.isPrimitive()); MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addStatement("this.$L = $L", fieldSpec.name, fieldSpec.name); ParameterSpec.Builder param = ParameterSpec.builder(fieldSpec.type, fieldSpec.name); ruleSet.getSetterRequiredRule().process(builder, fieldModel, param); ruleSet.getSetterNotRequiredRule().process(builder, fieldModel, param); builder.addParameter(param.build()); typeBuilder.addMethod(builder.build()); }
SetterHandler { public void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel) { if (fieldModel.isImmutable()) { return; } String methodName = getGetterSetterName(fieldSpec.name, true, fieldModel.getFieldType() == FieldType.BOOLEAN, fieldModel.isPrimitive()); MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addStatement("this.$L = $L", fieldSpec.name, fieldSpec.name); ParameterSpec.Builder param = ParameterSpec.builder(fieldSpec.type, fieldSpec.name); ruleSet.getSetterRequiredRule().process(builder, fieldModel, param); ruleSet.getSetterNotRequiredRule().process(builder, fieldModel, param); builder.addParameter(param.build()); typeBuilder.addMethod(builder.build()); } }
SetterHandler { public void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel) { if (fieldModel.isImmutable()) { return; } String methodName = getGetterSetterName(fieldSpec.name, true, fieldModel.getFieldType() == FieldType.BOOLEAN, fieldModel.isPrimitive()); MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addStatement("this.$L = $L", fieldSpec.name, fieldSpec.name); ParameterSpec.Builder param = ParameterSpec.builder(fieldSpec.type, fieldSpec.name); ruleSet.getSetterRequiredRule().process(builder, fieldModel, param); ruleSet.getSetterNotRequiredRule().process(builder, fieldModel, param); builder.addParameter(param.build()); typeBuilder.addMethod(builder.build()); } SetterHandler(Tune tune, MetaInfo metaInfo, RuleSet ruleSet); }
SetterHandler { public void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel) { if (fieldModel.isImmutable()) { return; } String methodName = getGetterSetterName(fieldSpec.name, true, fieldModel.getFieldType() == FieldType.BOOLEAN, fieldModel.isPrimitive()); MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addStatement("this.$L = $L", fieldSpec.name, fieldSpec.name); ParameterSpec.Builder param = ParameterSpec.builder(fieldSpec.type, fieldSpec.name); ruleSet.getSetterRequiredRule().process(builder, fieldModel, param); ruleSet.getSetterNotRequiredRule().process(builder, fieldModel, param); builder.addParameter(param.build()); typeBuilder.addMethod(builder.build()); } SetterHandler(Tune tune, MetaInfo metaInfo, RuleSet ruleSet); void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel); }
SetterHandler { public void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel) { if (fieldModel.isImmutable()) { return; } String methodName = getGetterSetterName(fieldSpec.name, true, fieldModel.getFieldType() == FieldType.BOOLEAN, fieldModel.isPrimitive()); MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.PUBLIC) .returns(void.class) .addStatement("this.$L = $L", fieldSpec.name, fieldSpec.name); ParameterSpec.Builder param = ParameterSpec.builder(fieldSpec.type, fieldSpec.name); ruleSet.getSetterRequiredRule().process(builder, fieldModel, param); ruleSet.getSetterNotRequiredRule().process(builder, fieldModel, param); builder.addParameter(param.build()); typeBuilder.addMethod(builder.build()); } SetterHandler(Tune tune, MetaInfo metaInfo, RuleSet ruleSet); void process(TypeSpec.Builder typeBuilder, FieldSpec fieldSpec, FieldModel fieldModel);  }