/** * This method is used to extract the annotations associated with * the type. Annotations extracted include the <code>Root</code> * annotation and the <code>Namespace</code> annotation as well as * other annotations that are used to describe the type. * * @param type this is the type to extract the annotations from */ private void extract(Class type) { for(Annotation label : labels) { if(label instanceof Namespace) { namespace(label); } if(label instanceof NamespaceList) { scope(label); } if(label instanceof Root) { root(label); } if(label instanceof Order) { order(label); } if(label instanceof Default) { access(label); } } }
/** * This is used to set the optional <code>Root</code> annotation for * the class. The root can only be set once, so if a super type also * has a root annotation define it must be ignored. * * @param label this is the label used to define the root */ private void root(Annotation label) { if(label != null) { Root value = (Root)label; String real = type.getSimpleName(); String text = real; if(value != null) { text = value.name(); if(isEmpty(text)) { text = Reflector.getName(real); } strict = value.strict(); root = value; name = text; } } }
public void testMixedAnnotations() throws Exception { Map<String, Contact> map = getContacts(MixedAnnotations.class); assertEquals(map.size(), 3); assertFalse(map.get("name").isReadOnly()); assertFalse(map.get("value").isReadOnly()); assertFalse(map.get("list").isReadOnly()); assertEquals(String.class, map.get("name").getType()); assertEquals(int.class, map.get("value").getType()); assertEquals(List.class, map.get("list").getType()); assertEquals(Element.class, map.get("name").getAnnotation().annotationType()); assertEquals(Attribute.class, map.get("value").getAnnotation().annotationType()); assertEquals(ElementList.class, map.get("list").getAnnotation().annotationType()); assertEquals(Element.class, map.get("name").getAnnotation(Element.class).annotationType()); assertEquals(Attribute.class, map.get("value").getAnnotation(Attribute.class).annotationType()); assertEquals(ElementList.class, map.get("list").getAnnotation(ElementList.class).annotationType()); assertNull(map.get("name").getAnnotation(Root.class)); assertNull(map.get("value").getAnnotation(Root.class)); assertNull(map.get("list").getAnnotation(Root.class)); }
public void testMixedAnnotationsWithNoDefaults() throws Exception { Map<String, Contact> map = getContacts(MixedAnnotations.class); assertEquals(map.size(), 4); assertFalse(map.get("name").isReadOnly()); assertFalse(map.get("value").isReadOnly()); assertEquals(int.class, map.get("value").getType()); assertEquals(String.class, map.get("name").getType()); assertEquals(Attribute.class, map.get("name").getAnnotation().annotationType()); assertEquals(Element.class, map.get("value").getAnnotation().annotationType()); assertEquals(Attribute.class, map.get("name").getAnnotation(Attribute.class).annotationType()); assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType()); assertNull(map.get("name").getAnnotation(Root.class)); assertNull(map.get("value").getAnnotation(Root.class)); }
/** * This method is used to scan the provided <code>Type</code> for * an annotation. If the <code>Type</code> represents a field or * method then the annotation can be taken directly from that * field or method. If however the type represents a class then * the class itself must contain the annotation. * * @param real the type that represents the field or method * * @return this returns the annotation on the field or method */ private Convert getConvert(Class real) throws Exception { Convert convert = getAnnotation(real, Convert.class); if(convert != null) { Root root = getAnnotation(real, Root.class); if(root == null) { throw new ConvertException("Root annotation required for %s", real); } } return convert; }
/** * This will acquire the name of the <code>Root</code> annotation * for the specified class. This will traverse the inheritance * hierarchy looking for the root annotation, when it is found it * is used to acquire a name for the XML element it represents. * * @param real the actual type of the object being searched * @param type this is the type to acquire the root name with * * @return the root name for the specified type if it exists */ private String getRoot(Class<?> real, Class<?> type) { String name = type.getSimpleName(); Root root = type.getAnnotation(Root.class); if(root != null) { String text = root.name(); if(!isEmpty(text)) { return text; } return Reflector.getName(name); } return null; }
public void testExtendedAnnotations() throws Exception { Map<String, Contact> map = getContacts(ExtendedAnnotations.class); assertFalse(map.get("array").isReadOnly()); assertFalse(map.get("map").isReadOnly()); assertFalse(map.get("name").isReadOnly()); assertFalse(map.get("value").isReadOnly()); assertEquals(String[].class, map.get("array").getType()); assertEquals(Map.class, map.get("map").getType()); assertEquals(int.class, map.get("value").getType()); assertEquals(String.class, map.get("name").getType()); assertEquals(Attribute.class, map.get("name").getAnnotation().annotationType()); assertEquals(Element.class, map.get("value").getAnnotation().annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation().annotationType()); assertEquals(Element.class, map.get("array").getAnnotation().annotationType()); assertEquals(Attribute.class, map.get("name").getAnnotation(Attribute.class).annotationType()); assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation(ElementMap.class).annotationType()); assertEquals(Element.class, map.get("array").getAnnotation(Element.class).annotationType()); assertNull(map.get("name").getAnnotation(Root.class)); assertNull(map.get("value").getAnnotation(Root.class)); assertNull(map.get("map").getAnnotation(Root.class)); assertNull(map.get("array").getAnnotation(Root.class)); }
public void testMixedAnnotations() throws Exception { Map<String, Contact> map = getContacts(MixedAnnotations.class); assertFalse(map.get("array").isReadOnly()); assertFalse(map.get("map").isReadOnly()); assertFalse(map.get("name").isReadOnly()); assertFalse(map.get("value").isReadOnly()); assertEquals(String[].class, map.get("array").getType()); assertEquals(Map.class, map.get("map").getType()); assertEquals(int.class, map.get("value").getType()); assertEquals(String.class, map.get("name").getType()); assertEquals(Attribute.class, map.get("name").getAnnotation().annotationType()); assertEquals(Element.class, map.get("value").getAnnotation().annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation().annotationType()); assertEquals(ElementArray.class, map.get("array").getAnnotation().annotationType()); assertEquals(Attribute.class, map.get("name").getAnnotation(Attribute.class).annotationType()); assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation(ElementMap.class).annotationType()); assertEquals(ElementArray.class, map.get("array").getAnnotation(ElementArray.class).annotationType()); assertNull(map.get("name").getAnnotation(Root.class)); assertNull(map.get("value").getAnnotation(Root.class)); assertNull(map.get("map").getAnnotation(Root.class)); assertNull(map.get("array").getAnnotation(Root.class)); }
public void testNoAnnotations() throws Exception { Map<String, Contact> map = getContacts(NoAnnotations.class); assertFalse(map.get("name").isReadOnly()); assertFalse(map.get("value").isReadOnly()); assertFalse(map.get("date").isReadOnly()); assertFalse(map.get("locale").isReadOnly()); assertFalse(map.get("array").isReadOnly()); assertFalse(map.get("list").isReadOnly()); assertFalse(map.get("map").isReadOnly()); assertEquals(String.class, map.get("name").getType()); assertEquals(int.class, map.get("value").getType()); assertEquals(Date.class, map.get("date").getType()); assertEquals(Locale.class, map.get("locale").getType()); assertEquals(int[].class, map.get("array").getType()); assertEquals(List.class, map.get("list").getType()); assertEquals(Map.class, map.get("map").getType()); assertEquals(Element.class, map.get("name").getAnnotation().annotationType()); assertEquals(Element.class, map.get("value").getAnnotation().annotationType()); assertEquals(Element.class, map.get("date").getAnnotation().annotationType()); assertEquals(Element.class, map.get("locale").getAnnotation().annotationType()); assertEquals(Element.class, map.get("array").getAnnotation().annotationType()); assertEquals(ElementArray.class, map.get("strings").getAnnotation().annotationType()); assertEquals(ElementList.class, map.get("list").getAnnotation().annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation().annotationType()); assertEquals(Element.class, map.get("name").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("value").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("date").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("locale").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("array").getAnnotation(Element.class).annotationType()); assertEquals(ElementArray.class, map.get("strings").getAnnotation(ElementArray.class).annotationType()); assertEquals(ElementList.class, map.get("list").getAnnotation(ElementList.class).annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation(ElementMap.class).annotationType()); assertNull(map.get("name").getAnnotation(Root.class)); assertNull(map.get("value").getAnnotation(Root.class)); assertNull(map.get("date").getAnnotation(Root.class)); assertNull(map.get("locale").getAnnotation(Root.class)); assertNull(map.get("array").getAnnotation(Root.class)); assertNull(map.get("strings").getAnnotation(Root.class)); assertNull(map.get("list").getAnnotation(Root.class)); assertNull(map.get("map").getAnnotation(Root.class)); }
public void testNoAnnotations() throws Exception { Map<String, Contact> map = getContacts(NoAnnotations.class); assertFalse(map.get("date").isReadOnly()); assertFalse(map.get("customer").isReadOnly()); assertFalse(map.get("name").isReadOnly()); assertFalse(map.get("price").isReadOnly()); assertFalse(map.get("list").isReadOnly()); assertFalse(map.get("map").isReadOnly()); assertFalse(map.get("array").isReadOnly()); assertEquals(Date.class, map.get("date").getType()); assertEquals(String.class, map.get("customer").getType()); assertEquals(String.class, map.get("name").getType()); assertEquals(int.class, map.get("price").getType()); assertEquals(List.class, map.get("list").getType()); assertEquals(Map.class, map.get("map").getType()); assertEquals(String[].class, map.get("array").getType()); assertEquals(Element.class, map.get("date").getAnnotation().annotationType()); assertEquals(Element.class, map.get("customer").getAnnotation().annotationType()); assertEquals(Element.class, map.get("name").getAnnotation().annotationType()); assertEquals(Element.class, map.get("price").getAnnotation().annotationType()); assertEquals(ElementList.class, map.get("list").getAnnotation().annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation().annotationType()); assertEquals(ElementArray.class, map.get("array").getAnnotation().annotationType()); assertEquals(Element.class, map.get("date").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("customer").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("name").getAnnotation(Element.class).annotationType()); assertEquals(Element.class, map.get("price").getAnnotation(Element.class).annotationType()); assertEquals(ElementList.class, map.get("list").getAnnotation(ElementList.class).annotationType()); assertEquals(ElementMap.class, map.get("map").getAnnotation(ElementMap.class).annotationType()); assertEquals(ElementArray.class, map.get("array").getAnnotation(ElementArray.class).annotationType()); assertNull(map.get("date").getAnnotation(Root.class)); assertNull(map.get("customer").getAnnotation(Root.class)); assertNull(map.get("name").getAnnotation(Root.class)); assertNull(map.get("price").getAnnotation(Root.class)); assertNull(map.get("list").getAnnotation(Root.class)); assertNull(map.get("map").getAnnotation(Root.class)); assertNull(map.get("array").getAnnotation(Root.class)); }
@Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return clazz.isAnnotationPresent(Root.class) && canWrite(mediaType); }
/** * This returns the <code>Root</code> annotation for the class. * The root determines the type of deserialization that is to * be performed and also contains the name of the root element. * * @return this returns the name of the object being scanned */ public Root getRoot() { return detail.getRoot(); }
/** * This returns the <code>Root</code> annotation for the class. * The root determines the type of deserialization that is to * be performed and also contains the name of the root element. * * @return this returns the name of the object being scanned */ public Root getRoot() { return root; }
/** * This returns the <code>Root</code> annotation for the class. * The root determines the type of deserialization that is to * be performed and also contains the name of the root element. * * @return this returns the name of the object being scanned */ Root getRoot();
/** * This returns the root of the class processed by this scanner. * The root determines the type of deserialization that is to * be performed and also contains the name of the root element. * * @return this returns the name of the object being scanned */ public Root getRoot() { return root; }