@Test public void processSubTypeRule() { TypeSpec.Builder builder = TypeSpec.classBuilder("Test"); TypeModel model = mock(TypeModel.class); Map<String, String> subTypes = new HashMap<>(); subTypes.put("A", "com.flipkart.lyrics.A"); subTypes.put("B", "com.flipkart.lyrics.B"); SubTypeModel subTypeModel = new SubTypeModel("one", subTypes); when(model.getSubTypes()).thenReturn(subTypeModel); new JacksonStyle().processSubTypeRule(builder, model); TypeSpec typeSpec = builder.build(); assertEquals("Test", typeSpec.name); assertEquals(2, typeSpec.annotations.size()); AnnotationSpec typeInfoAnnotation = typeSpec.annotations.get(0); assertEquals(3, typeInfoAnnotation.members.size()); AnnotationSpec subTypesAnnotation = typeSpec.annotations.get(1); assertEquals(1, subTypesAnnotation.members.size()); assertEquals(2, subTypesAnnotation.members.get("value").size()); }
@Override public void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel) { SubTypeModel subTypeModel = typeModel.getSubTypes(); AnnotationSpec.Builder typeInfoAnnotation = AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As." + (subTypeModel.isExistingProperty() ? "EXISTING_PROPERTY" : "PROPERTY")) .addMember("property", "$S", subTypeModel.getProperty()); if (subTypeModel.isVisible()) { typeInfoAnnotation.addMember("visible", "$L", subTypeModel.isVisible()); } AnnotationSpec.Builder subTypesBuilder = AnnotationSpec.builder(JsonSubTypes.class); for (Map.Entry<String, String> subType : subTypeModel.getSubTypeMapping().entrySet()) { subTypesBuilder.addMember("value", "$L", AnnotationSpec.builder(JsonSubTypes.Type.class) .addMember("value", "$T.class", getClassName(subType.getValue())) .addMember("name", "$S", subType.getKey()) .build()); } typeSpec.addAnnotation(typeInfoAnnotation.build()); typeSpec.addAnnotation(subTypesBuilder.build()); }
JacksonStyle extends AnnotatorStyle { @Override public void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel) { SubTypeModel subTypeModel = typeModel.getSubTypes(); AnnotationSpec.Builder typeInfoAnnotation = AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As." + (subTypeModel.isExistingProperty() ? "EXISTING_PROPERTY" : "PROPERTY")) .addMember("property", "$S", subTypeModel.getProperty()); if (subTypeModel.isVisible()) { typeInfoAnnotation.addMember("visible", "$L", subTypeModel.isVisible()); } AnnotationSpec.Builder subTypesBuilder = AnnotationSpec.builder(JsonSubTypes.class); for (Map.Entry<String, String> subType : subTypeModel.getSubTypeMapping().entrySet()) { subTypesBuilder.addMember("value", "$L", AnnotationSpec.builder(JsonSubTypes.Type.class) .addMember("value", "$T.class", getClassName(subType.getValue())) .addMember("name", "$S", subType.getKey()) .build()); } typeSpec.addAnnotation(typeInfoAnnotation.build()); typeSpec.addAnnotation(subTypesBuilder.build()); } }
JacksonStyle extends AnnotatorStyle { @Override public void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel) { SubTypeModel subTypeModel = typeModel.getSubTypes(); AnnotationSpec.Builder typeInfoAnnotation = AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As." + (subTypeModel.isExistingProperty() ? "EXISTING_PROPERTY" : "PROPERTY")) .addMember("property", "$S", subTypeModel.getProperty()); if (subTypeModel.isVisible()) { typeInfoAnnotation.addMember("visible", "$L", subTypeModel.isVisible()); } AnnotationSpec.Builder subTypesBuilder = AnnotationSpec.builder(JsonSubTypes.class); for (Map.Entry<String, String> subType : subTypeModel.getSubTypeMapping().entrySet()) { subTypesBuilder.addMember("value", "$L", AnnotationSpec.builder(JsonSubTypes.Type.class) .addMember("value", "$T.class", getClassName(subType.getValue())) .addMember("name", "$S", subType.getKey()) .build()); } typeSpec.addAnnotation(typeInfoAnnotation.build()); typeSpec.addAnnotation(subTypesBuilder.build()); }  }
JacksonStyle extends AnnotatorStyle { @Override public void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel) { SubTypeModel subTypeModel = typeModel.getSubTypes(); AnnotationSpec.Builder typeInfoAnnotation = AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As." + (subTypeModel.isExistingProperty() ? "EXISTING_PROPERTY" : "PROPERTY")) .addMember("property", "$S", subTypeModel.getProperty()); if (subTypeModel.isVisible()) { typeInfoAnnotation.addMember("visible", "$L", subTypeModel.isVisible()); } AnnotationSpec.Builder subTypesBuilder = AnnotationSpec.builder(JsonSubTypes.class); for (Map.Entry<String, String> subType : subTypeModel.getSubTypeMapping().entrySet()) { subTypesBuilder.addMember("value", "$L", AnnotationSpec.builder(JsonSubTypes.Type.class) .addMember("value", "$T.class", getClassName(subType.getValue())) .addMember("name", "$S", subType.getKey()) .build()); } typeSpec.addAnnotation(typeInfoAnnotation.build()); typeSpec.addAnnotation(subTypesBuilder.build()); }  @Override void processNamedAsRule(FieldSpec.Builder fieldSpec, FieldModel fieldModel); @Override void processInclusionRule(FieldSpec.Builder fieldSpec, FieldModel fieldModel); @Override void processGlobalInclusionRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processIgnoreUnknownsRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processPropertyOrderRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processConstructorOrderRule(MethodSpec.Builder methodSpec, TypeModel typeModel, List<String> constructorFields); }
JacksonStyle extends AnnotatorStyle { @Override public void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel) { SubTypeModel subTypeModel = typeModel.getSubTypes(); AnnotationSpec.Builder typeInfoAnnotation = AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As." + (subTypeModel.isExistingProperty() ? "EXISTING_PROPERTY" : "PROPERTY")) .addMember("property", "$S", subTypeModel.getProperty()); if (subTypeModel.isVisible()) { typeInfoAnnotation.addMember("visible", "$L", subTypeModel.isVisible()); } AnnotationSpec.Builder subTypesBuilder = AnnotationSpec.builder(JsonSubTypes.class); for (Map.Entry<String, String> subType : subTypeModel.getSubTypeMapping().entrySet()) { subTypesBuilder.addMember("value", "$L", AnnotationSpec.builder(JsonSubTypes.Type.class) .addMember("value", "$T.class", getClassName(subType.getValue())) .addMember("name", "$S", subType.getKey()) .build()); } typeSpec.addAnnotation(typeInfoAnnotation.build()); typeSpec.addAnnotation(subTypesBuilder.build()); }  @Override void processNamedAsRule(FieldSpec.Builder fieldSpec, FieldModel fieldModel); @Override void processInclusionRule(FieldSpec.Builder fieldSpec, FieldModel fieldModel); @Override void processGlobalInclusionRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processSubTypeRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processIgnoreUnknownsRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processPropertyOrderRule(TypeSpec.Builder typeSpec, TypeModel typeModel); @Override void processConstructorOrderRule(MethodSpec.Builder methodSpec, TypeModel typeModel, List<String> constructorFields);  }