@Test public void testSingleField(@TuneProvider Tune tune) { RuleSet ruleSet = new DefaultRuleSet(tune, metaInfo); TypeSpec.Builder typeBuilder = TypeSpec.classBuilder("Sample"); Map<String, FieldModel> fieldModels = new HashMap<>(); FieldModel fieldModel = mock(FieldModel.class); when(fieldModel.getFieldType()).thenReturn(FieldType.STRING); when(fieldModel.getModifiers()).thenReturn(new Modifier[]{Modifier.PUBLIC}); fieldModels.put("one", fieldModel); TypeModel typeModel = mock(TypeModel.class); Mockito.when(typeModel.getFields()).thenReturn(fieldModels); FieldsHandler handler = new FieldsHandler(tune, metaInfo, ruleSet); handler.process(typeBuilder, typeModel); TypeSpec typeSpec = typeBuilder.build(); assertEquals(1, typeSpec.fieldSpecs.size(), "Only one field should be present"); assertEquals("one", typeSpec.fieldSpecs.get(0).name, "Field should have the expected name"); assertEquals(2, typeSpec.methodSpecs.size(), "Getters and setters should be present"); assertEquals(getGetterSetterName("one", false, false, false), typeSpec.methodSpecs.get(0).name, "Getter should be first"); assertEquals(getGetterSetterName("one", true, false, false), typeSpec.methodSpecs.get(1).name, "Setter should be second"); }
@Override public void process(TypeSpec.Builder typeBuilder, TypeModel typeModel) { for (String key : typeModel.getFields().keySet()) { FieldModel fieldModel = typeModel.getFields().get(key); FieldSpec.Builder originalFieldBuilder = getFieldTypeHandler(fieldModel.getFieldType(), tune.getFieldTypeHandlerSet()) .process(typeBuilder, key, fieldModel); FieldSpec.Builder fieldBuilder = tune.getFieldModificationHandlers().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null).findFirst() .map(e -> e.getValue().process(originalFieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()), fieldModel)) .orElse(originalFieldBuilder); handleFieldRules(fieldBuilder, fieldModel); List<Boolean> triggers = tune.getFieldsAdditionalPropertiesHandler().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null) .map(e -> e.getValue().process(fieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()))) .collect(Collectors.toList()); boolean hideField = triggers.stream().anyMatch(b -> !b); if (hideField) { continue; } FieldSpec fieldSpec = fieldBuilder.build(); typeBuilder.addField(fieldSpec); if (tune.areAccessorsNeeded() && !metaInfo.isOverrideAccessorGeneration()) { new GetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); new SetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); } } }
FieldsHandler extends Handler { @Override public void process(TypeSpec.Builder typeBuilder, TypeModel typeModel) { for (String key : typeModel.getFields().keySet()) { FieldModel fieldModel = typeModel.getFields().get(key); FieldSpec.Builder originalFieldBuilder = getFieldTypeHandler(fieldModel.getFieldType(), tune.getFieldTypeHandlerSet()) .process(typeBuilder, key, fieldModel); FieldSpec.Builder fieldBuilder = tune.getFieldModificationHandlers().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null).findFirst() .map(e -> e.getValue().process(originalFieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()), fieldModel)) .orElse(originalFieldBuilder); handleFieldRules(fieldBuilder, fieldModel); List<Boolean> triggers = tune.getFieldsAdditionalPropertiesHandler().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null) .map(e -> e.getValue().process(fieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()))) .collect(Collectors.toList()); boolean hideField = triggers.stream().anyMatch(b -> !b); if (hideField) { continue; } FieldSpec fieldSpec = fieldBuilder.build(); typeBuilder.addField(fieldSpec); if (tune.areAccessorsNeeded() && !metaInfo.isOverrideAccessorGeneration()) { new GetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); new SetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); } } } }
FieldsHandler extends Handler { @Override public void process(TypeSpec.Builder typeBuilder, TypeModel typeModel) { for (String key : typeModel.getFields().keySet()) { FieldModel fieldModel = typeModel.getFields().get(key); FieldSpec.Builder originalFieldBuilder = getFieldTypeHandler(fieldModel.getFieldType(), tune.getFieldTypeHandlerSet()) .process(typeBuilder, key, fieldModel); FieldSpec.Builder fieldBuilder = tune.getFieldModificationHandlers().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null).findFirst() .map(e -> e.getValue().process(originalFieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()), fieldModel)) .orElse(originalFieldBuilder); handleFieldRules(fieldBuilder, fieldModel); List<Boolean> triggers = tune.getFieldsAdditionalPropertiesHandler().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null) .map(e -> e.getValue().process(fieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()))) .collect(Collectors.toList()); boolean hideField = triggers.stream().anyMatch(b -> !b); if (hideField) { continue; } FieldSpec fieldSpec = fieldBuilder.build(); typeBuilder.addField(fieldSpec); if (tune.areAccessorsNeeded() && !metaInfo.isOverrideAccessorGeneration()) { new GetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); new SetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); } } } FieldsHandler(Tune tune, MetaInfo metaInfo, RuleSet ruleSet); }
FieldsHandler extends Handler { @Override public void process(TypeSpec.Builder typeBuilder, TypeModel typeModel) { for (String key : typeModel.getFields().keySet()) { FieldModel fieldModel = typeModel.getFields().get(key); FieldSpec.Builder originalFieldBuilder = getFieldTypeHandler(fieldModel.getFieldType(), tune.getFieldTypeHandlerSet()) .process(typeBuilder, key, fieldModel); FieldSpec.Builder fieldBuilder = tune.getFieldModificationHandlers().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null).findFirst() .map(e -> e.getValue().process(originalFieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()), fieldModel)) .orElse(originalFieldBuilder); handleFieldRules(fieldBuilder, fieldModel); List<Boolean> triggers = tune.getFieldsAdditionalPropertiesHandler().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null) .map(e -> e.getValue().process(fieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()))) .collect(Collectors.toList()); boolean hideField = triggers.stream().anyMatch(b -> !b); if (hideField) { continue; } FieldSpec fieldSpec = fieldBuilder.build(); typeBuilder.addField(fieldSpec); if (tune.areAccessorsNeeded() && !metaInfo.isOverrideAccessorGeneration()) { new GetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); new SetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); } } } FieldsHandler(Tune tune, MetaInfo metaInfo, RuleSet ruleSet); @Override void process(TypeSpec.Builder typeBuilder, TypeModel typeModel); }
FieldsHandler extends Handler { @Override public void process(TypeSpec.Builder typeBuilder, TypeModel typeModel) { for (String key : typeModel.getFields().keySet()) { FieldModel fieldModel = typeModel.getFields().get(key); FieldSpec.Builder originalFieldBuilder = getFieldTypeHandler(fieldModel.getFieldType(), tune.getFieldTypeHandlerSet()) .process(typeBuilder, key, fieldModel); FieldSpec.Builder fieldBuilder = tune.getFieldModificationHandlers().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null).findFirst() .map(e -> e.getValue().process(originalFieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()), fieldModel)) .orElse(originalFieldBuilder); handleFieldRules(fieldBuilder, fieldModel); List<Boolean> triggers = tune.getFieldsAdditionalPropertiesHandler().entrySet().stream() .filter(e -> fieldModel.getAdditionalFields().get(e.getKey()) != null) .map(e -> e.getValue().process(fieldBuilder, e.getKey(), fieldModel.getAdditionalFields().get(e.getKey()))) .collect(Collectors.toList()); boolean hideField = triggers.stream().anyMatch(b -> !b); if (hideField) { continue; } FieldSpec fieldSpec = fieldBuilder.build(); typeBuilder.addField(fieldSpec); if (tune.areAccessorsNeeded() && !metaInfo.isOverrideAccessorGeneration()) { new GetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); new SetterHandler(tune, metaInfo, ruleSet).process(typeBuilder, fieldSpec, fieldModel); } } } FieldsHandler(Tune tune, MetaInfo metaInfo, RuleSet ruleSet); @Override void process(TypeSpec.Builder typeBuilder, TypeModel typeModel);  }