ClassInfoCollector { public ClassInfo collect(AcceptingVisitor c) { return collected.computeIfAbsent( c.getClass(), (key) -> { final ClassInfo ci = new ClassInfo(); ci.clazz = c.getClass(); ci.name = aliasMapper.toName(c); ci.type = getQualifiedName(c.getClass()); c.accept(new AttrInfoCollector(ci.attributes, aliasMapper)); return ci; }); } ClassInfoCollector(ClassNameMapper aliasMapper); ClassInfo collect(AcceptingVisitor c); static String getQualifiedName(Class<?> clazz);  }
@Test public void testBooleanAttr() { @SuppressWarnings("unused") class Clazz extends AttrComposite { AttrBoolean attrTrue = attributes.register("attrTrue", AttrBoolean.builder().defaultValue(true)); AttrBoolean attrFalse = attributes.register("attrFalse", AttrBoolean.builder().defaultValue(false)); AttrBoolean attrNull = attributes.register("attrNull", AttrBoolean.builder().defaultValue(null)); } ClassInfoCollector collector = new ClassInfoCollector(JvmNameMapper.INSTANCE); ClassInfo classInfo = collector.collect(new Clazz()); Assertions.assertThat(classInfo.attributes) .hasEntrySatisfying("attrTrue", info -> Assertions.assertThat(info.value).isEqualTo(true)) .hasEntrySatisfying("attrFalse", info -> Assertions.assertThat(info.value).isEqualTo(false)) .hasEntrySatisfying("attrNull", info -> Assertions.assertThat(info.value).isNull()); }
@Test public void testNumericAttr() { @SuppressWarnings("unused") class Clazz extends AttrComposite { AttrInteger attrInteger = attributes.register("attrInteger", AttrInteger.builder().defaultValue(42)); AttrDouble attrDouble = attributes.register("attrDouble", AttrDouble.builder().defaultValue(42.5)); } ClassInfoCollector collector = new ClassInfoCollector(JvmNameMapper.INSTANCE); ClassInfo classInfo = collector.collect(new Clazz()); Assertions.assertThat(classInfo.attributes) .hasEntrySatisfying("attrInteger", info -> Assertions.assertThat(info.value).isEqualTo(42)) .hasEntrySatisfying( "attrDouble", info -> Assertions.assertThat(info.value).isEqualTo(42.5)); }
@Test public void testEnumAttr() { @SuppressWarnings("unused") class Clazz extends AttrComposite { AttrEnum<DayOfWeek> attrEnum = attributes.register( "attrEnum", AttrEnum.builder(DayOfWeek.class).defaultValue(DayOfWeek.FRIDAY)); AttrEnum<DayOfWeek> attrEnumNull = attributes.register("attrEnumNull", AttrEnum.builder(DayOfWeek.class).defaultValue(null)); } ClassInfoCollector collector = new ClassInfoCollector(JvmNameMapper.INSTANCE); ClassInfo classInfo = collector.collect(new Clazz()); Assertions.assertThat(classInfo.attributes) .hasEntrySatisfying( "attrEnum", info -> Assertions.assertThat(info.value).isEqualTo(DayOfWeek.FRIDAY.toString())) .hasEntrySatisfying("attrEnumNull", info -> Assertions.assertThat(info.value).isNull()); }
@Test public void testStringAttr() { @SuppressWarnings("unused") class Clazz extends AttrComposite { AttrString attr = attributes.register("attr", AttrString.builder().defaultValue("foo")); AttrString attrNull = attributes.register("attrNull", AttrString.builder().defaultValue(null)); } ClassInfoCollector collector = new ClassInfoCollector(JvmNameMapper.INSTANCE); ClassInfo classInfo = collector.collect(new Clazz()); Assertions.assertThat(classInfo.attributes) .hasEntrySatisfying("attr", info -> Assertions.assertThat(info.value).isEqualTo("foo")) .hasEntrySatisfying("attrNull", info -> Assertions.assertThat(info.value).isNull()); }
@Test public void testObjectAttr() { class Foo extends AttrComposite {} @SuppressWarnings("unused") class Clazz extends AttrComposite { AttrObject<Foo> attr = attributes.register("attr", AttrObject.builder(Foo.class).defaultValue(Foo::new)); AttrObject<Foo> attrNull = attributes.register("attrNull", AttrObject.builder(Foo.class).defaultValue(() -> null)); } ClassInfoCollector collector = new ClassInfoCollector(JvmNameMapper.INSTANCE); ClassInfo classInfo = collector.collect(new Clazz()); Assertions.assertThat(classInfo.attributes) .hasEntrySatisfying( "attr", info -> Assertions.assertThat(info.value) .isEqualTo(JvmNameMapper.INSTANCE.toName(new Foo()))) .hasEntrySatisfying("attrNull", info -> Assertions.assertThat(info.value).isNull()); }