@Test public void testWithConfiguration() { final Settings settings = TestUtils.settings(); settings.jsonLibrary = JsonLibrary.gson; PropertyPolymorphismExtension extension = new PropertyPolymorphismExtension(); Map<String, String> config = new HashMap<>(); config.put(PropertyPolymorphismExtension.MARKER_ANNOTATION, Marker.class.getName()); config.put(PropertyPolymorphismExtension.NAME_ANNOTATION, PropertyName.class.getName()); config.put(PropertyPolymorphismExtension.NAME_ELEMENT, "name"); extension.setConfiguration(config); settings.extensions.add(extension); final String output = new TypeScriptGenerator(settings) .generateTypeScript(Input.from(TestA.class, TestBSub1.class, TestBSub2.class)); assertTrue(output, output.contains("b: TestBRef;")); assertTrue(output, output.contains("interface TestBRef {")); assertTrue(output, output.contains("foo: TestBSub2;")); assertTrue(output, output.contains("testBSub1: TestBSub1;")); }
@Override public void setConfiguration(Map<String, String> configuration) throws RuntimeException { { String markerAnnotationName = configuration.get(MARKER_ANNOTATION); if (markerAnnotationName == null) { throw new RuntimeException("Property '" + MARKER_ANNOTATION + "' has to be configured"); } isPolymorphicBase = cls -> { for (Annotation annotation : cls.getAnnotations()) { if (markerAnnotationName.equals(annotation.annotationType().getName())) { return true; } } return false; }; } { String nameAnnotationName = configuration.get(NAME_ANNOTATION); if (nameAnnotationName == null) { getPropertyName = this::defaultPropertyName; } else { String nameElementName = configuration.get(NAME_ELEMENT); if (nameElementName == null) { nameElementName = "value"; } String nameFieldNameFinal = nameElementName; getPropertyName = subType -> { try { for (Annotation annotation : subType.getAnnotations()) { if (nameAnnotationName.equals(annotation.annotationType().getName())) { Method method = annotation.annotationType().getDeclaredMethod(nameFieldNameFinal); return (String) method.invoke(annotation); } } } catch (Throwable t) { throw new RuntimeException(t); } return defaultPropertyName(subType); }; } } }
PropertyPolymorphismExtension extends Extension { @Override public void setConfiguration(Map<String, String> configuration) throws RuntimeException { { String markerAnnotationName = configuration.get(MARKER_ANNOTATION); if (markerAnnotationName == null) { throw new RuntimeException("Property '" + MARKER_ANNOTATION + "' has to be configured"); } isPolymorphicBase = cls -> { for (Annotation annotation : cls.getAnnotations()) { if (markerAnnotationName.equals(annotation.annotationType().getName())) { return true; } } return false; }; } { String nameAnnotationName = configuration.get(NAME_ANNOTATION); if (nameAnnotationName == null) { getPropertyName = this::defaultPropertyName; } else { String nameElementName = configuration.get(NAME_ELEMENT); if (nameElementName == null) { nameElementName = "value"; } String nameFieldNameFinal = nameElementName; getPropertyName = subType -> { try { for (Annotation annotation : subType.getAnnotations()) { if (nameAnnotationName.equals(annotation.annotationType().getName())) { Method method = annotation.annotationType().getDeclaredMethod(nameFieldNameFinal); return (String) method.invoke(annotation); } } } catch (Throwable t) { throw new RuntimeException(t); } return defaultPropertyName(subType); }; } } } }
PropertyPolymorphismExtension extends Extension { @Override public void setConfiguration(Map<String, String> configuration) throws RuntimeException { { String markerAnnotationName = configuration.get(MARKER_ANNOTATION); if (markerAnnotationName == null) { throw new RuntimeException("Property '" + MARKER_ANNOTATION + "' has to be configured"); } isPolymorphicBase = cls -> { for (Annotation annotation : cls.getAnnotations()) { if (markerAnnotationName.equals(annotation.annotationType().getName())) { return true; } } return false; }; } { String nameAnnotationName = configuration.get(NAME_ANNOTATION); if (nameAnnotationName == null) { getPropertyName = this::defaultPropertyName; } else { String nameElementName = configuration.get(NAME_ELEMENT); if (nameElementName == null) { nameElementName = "value"; } String nameFieldNameFinal = nameElementName; getPropertyName = subType -> { try { for (Annotation annotation : subType.getAnnotations()) { if (nameAnnotationName.equals(annotation.annotationType().getName())) { Method method = annotation.annotationType().getDeclaredMethod(nameFieldNameFinal); return (String) method.invoke(annotation); } } } catch (Throwable t) { throw new RuntimeException(t); } return defaultPropertyName(subType); }; } } } PropertyPolymorphismExtension();  PropertyPolymorphismExtension(Predicate<Class<?>> isPolymorphicBase,
            Function<Class<?>, String> getPropertyName); }
PropertyPolymorphismExtension extends Extension { @Override public void setConfiguration(Map<String, String> configuration) throws RuntimeException { { String markerAnnotationName = configuration.get(MARKER_ANNOTATION); if (markerAnnotationName == null) { throw new RuntimeException("Property '" + MARKER_ANNOTATION + "' has to be configured"); } isPolymorphicBase = cls -> { for (Annotation annotation : cls.getAnnotations()) { if (markerAnnotationName.equals(annotation.annotationType().getName())) { return true; } } return false; }; } { String nameAnnotationName = configuration.get(NAME_ANNOTATION); if (nameAnnotationName == null) { getPropertyName = this::defaultPropertyName; } else { String nameElementName = configuration.get(NAME_ELEMENT); if (nameElementName == null) { nameElementName = "value"; } String nameFieldNameFinal = nameElementName; getPropertyName = subType -> { try { for (Annotation annotation : subType.getAnnotations()) { if (nameAnnotationName.equals(annotation.annotationType().getName())) { Method method = annotation.annotationType().getDeclaredMethod(nameFieldNameFinal); return (String) method.invoke(annotation); } } } catch (Throwable t) { throw new RuntimeException(t); } return defaultPropertyName(subType); }; } } } PropertyPolymorphismExtension();  PropertyPolymorphismExtension(Predicate<Class<?>> isPolymorphicBase,
            Function<Class<?>, String> getPropertyName); @Override EmitterExtensionFeatures getFeatures(); @Override void setConfiguration(Map<String, String> configuration); @Override List<TransformerDefinition> getTransformers(); }
PropertyPolymorphismExtension extends Extension { @Override public void setConfiguration(Map<String, String> configuration) throws RuntimeException { { String markerAnnotationName = configuration.get(MARKER_ANNOTATION); if (markerAnnotationName == null) { throw new RuntimeException("Property '" + MARKER_ANNOTATION + "' has to be configured"); } isPolymorphicBase = cls -> { for (Annotation annotation : cls.getAnnotations()) { if (markerAnnotationName.equals(annotation.annotationType().getName())) { return true; } } return false; }; } { String nameAnnotationName = configuration.get(NAME_ANNOTATION); if (nameAnnotationName == null) { getPropertyName = this::defaultPropertyName; } else { String nameElementName = configuration.get(NAME_ELEMENT); if (nameElementName == null) { nameElementName = "value"; } String nameFieldNameFinal = nameElementName; getPropertyName = subType -> { try { for (Annotation annotation : subType.getAnnotations()) { if (nameAnnotationName.equals(annotation.annotationType().getName())) { Method method = annotation.annotationType().getDeclaredMethod(nameFieldNameFinal); return (String) method.invoke(annotation); } } } catch (Throwable t) { throw new RuntimeException(t); } return defaultPropertyName(subType); }; } } } PropertyPolymorphismExtension();  PropertyPolymorphismExtension(Predicate<Class<?>> isPolymorphicBase,
            Function<Class<?>, String> getPropertyName); @Override EmitterExtensionFeatures getFeatures(); @Override void setConfiguration(Map<String, String> configuration); @Override List<TransformerDefinition> getTransformers(); static final String MARKER_ANNOTATION; static final String NAME_ANNOTATION; static final String NAME_ELEMENT; public Predicate<Class<?>> isPolymorphicBase; public Function<Class<?>, String> getPropertyName; }