Document public API in AnnotationAttributes

AnnotationAttributes has existed for several years, but none of the
"get" methods that make up its public API are documented. In many
cases, the behavior can be inferred from the name of the method, but
for some methods there are "hidden gems" and unexpected behavior
lurking behind the scenes.

This commit addresses this issue by documenting all public methods. In
addition, the hidden support for converting single elements into
single-element arrays has also been documented and tested.

Issue: SPR-13072
This commit is contained in:
Sam Brannen
2015-05-26 16:43:50 +02:00
parent 197f6594f4
commit 0ac0e2ce20
2 changed files with 150 additions and 21 deletions

View File

@@ -44,18 +44,18 @@ public class AnnotationAttributesTests {
@Test
public void typeSafeAttributeAccess() {
AnnotationAttributes nestedAttributes = new AnnotationAttributes();
nestedAttributes.put("value", 10);
nestedAttributes.put("name", "algernon");
attributes.put("name", "dave");
attributes.put("names", new String[] { "dave", "frank", "hal" });
attributes.put("bool1", true);
attributes.put("bool2", false);
attributes.put("color", Color.RED);
attributes.put("clazz", Integer.class);
attributes.put("class", Integer.class);
attributes.put("classes", new Class<?>[] { Number.class, Short.class, Integer.class });
attributes.put("number", 42);
attributes.put("numbers", new int[] { 42, 43 });
AnnotationAttributes nestedAttributes = new AnnotationAttributes();
nestedAttributes.put("value", 10);
nestedAttributes.put("name", "algernon");
attributes.put("anno", nestedAttributes);
attributes.put("annoArray", new AnnotationAttributes[] { nestedAttributes });
@@ -64,13 +64,33 @@ public class AnnotationAttributesTests {
assertThat(attributes.getBoolean("bool1"), equalTo(true));
assertThat(attributes.getBoolean("bool2"), equalTo(false));
assertThat(attributes.<Color>getEnum("color"), equalTo(Color.RED));
assertTrue(attributes.getClass("clazz").equals(Integer.class));
assertTrue(attributes.getClass("class").equals(Integer.class));
assertThat(attributes.getClassArray("classes"), equalTo(new Class[] { Number.class, Short.class, Integer.class }));
assertThat(attributes.<Integer>getNumber("number"), equalTo(42));
assertThat(attributes.getAnnotation("anno").<Integer>getNumber("value"), equalTo(10));
assertThat(attributes.getAnnotationArray("annoArray")[0].getString("name"), equalTo("algernon"));
}
@Test
public void singleElementToSingleElementArrayConversionSupport() {
AnnotationAttributes nestedAttributes = new AnnotationAttributes();
nestedAttributes.put("name", "Dilbert");
// Store single elements
attributes.put("names", "Dogbert");
attributes.put("classes", Number.class);
attributes.put("nestedAttributes", nestedAttributes);
// Get back arrays of single elements
assertThat(attributes.getStringArray("names"), equalTo(new String[] { "Dogbert" }));
assertThat(attributes.getClassArray("classes"), equalTo(new Class[] { Number.class }));
AnnotationAttributes[] array = attributes.getAnnotationArray("nestedAttributes");
assertNotNull(array);
assertTrue(array.getClass().isArray());
assertThat(array.length, is(1));
assertThat(array[0].getString("name"), equalTo("Dilbert"));
}
@Test
public void getEnumWithNullAttributeName() {
exception.expect(IllegalArgumentException.class);