Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.osgi; |
| 2 | |
| 3 | import java.lang.annotation.*; |
| 4 | import java.util.*; |
| 5 | |
| 6 | import aQute.bnd.annotation.metatype.*; |
| 7 | import aQute.lib.osgi.Descriptors.TypeRef; |
| 8 | |
| 9 | @SuppressWarnings("unchecked") public class Annotation { |
| 10 | TypeRef name; |
| 11 | Map<String, Object> elements; |
| 12 | ElementType member; |
| 13 | RetentionPolicy policy; |
| 14 | |
| 15 | public Annotation(TypeRef name, Map<String, Object> elements, ElementType member, |
| 16 | RetentionPolicy policy) { |
| 17 | this.name = name; |
| 18 | if ( elements == null) |
| 19 | this.elements = Collections.emptyMap(); |
| 20 | else |
| 21 | this.elements = elements; |
| 22 | this.member = member; |
| 23 | this.policy = policy; |
| 24 | } |
| 25 | |
| 26 | public TypeRef getName() { |
| 27 | return name; |
| 28 | } |
| 29 | |
| 30 | public ElementType getElementType() { |
| 31 | return member; |
| 32 | } |
| 33 | |
| 34 | public RetentionPolicy getRetentionPolicy() { |
| 35 | return policy; |
| 36 | } |
| 37 | |
| 38 | public String toString() { |
| 39 | return name + ":" + member + ":" + policy + ":" + elements; |
| 40 | } |
| 41 | |
| 42 | public <T> T get(String string) { |
| 43 | if (elements == null) |
| 44 | return null; |
| 45 | |
| 46 | return (T) elements.get(string); |
| 47 | } |
| 48 | |
| 49 | public <T> void put(String string, Object v) { |
| 50 | if (elements == null) |
| 51 | return; |
| 52 | |
| 53 | elements.put(string, v); |
| 54 | } |
| 55 | |
| 56 | public Set<String> keySet() { |
| 57 | if (elements == null) |
| 58 | return Collections.emptySet(); |
| 59 | |
| 60 | return elements.keySet(); |
| 61 | } |
| 62 | public <T extends java.lang.annotation.Annotation> T getAnnotation() throws Exception { |
| 63 | String cname = name.getFQN(); |
| 64 | Class<T> c = (Class<T>) getClass().getClassLoader().loadClass(cname); |
| 65 | return getAnnotation(c); |
| 66 | } |
| 67 | public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> c) |
| 68 | throws Exception { |
| 69 | String cname = name.getFQN(); |
| 70 | if ( ! c.getName().equals(cname)) |
| 71 | return null; |
| 72 | return Configurable.createConfigurable(c, elements ); |
| 73 | } |
| 74 | } |