Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
| 3 | import java.lang.annotation.*; |
| 4 | import java.util.*; |
| 5 | |
| 6 | import aQute.bnd.annotation.metatype.*; |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 7 | import aQute.bnd.osgi.Descriptors.TypeRef; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 8 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 9 | public class Annotation { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | TypeRef name; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 11 | Map<String,Object> elements; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | ElementType member; |
| 13 | RetentionPolicy policy; |
| 14 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | public Annotation(TypeRef name, Map<String,Object> elements, ElementType member, RetentionPolicy policy) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | this.name = name; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | if (elements == null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | this.elements = Collections.emptyMap(); |
| 19 | else |
| 20 | this.elements = elements; |
| 21 | this.member = member; |
| 22 | this.policy = policy; |
| 23 | } |
| 24 | |
| 25 | public TypeRef getName() { |
| 26 | return name; |
| 27 | } |
| 28 | |
| 29 | public ElementType getElementType() { |
| 30 | return member; |
| 31 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | public RetentionPolicy getRetentionPolicy() { |
| 34 | return policy; |
| 35 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | |
Stuart McCulloch | 55d4dfe | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 37 | @Override |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 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(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 59 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | return elements.keySet(); |
| 61 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 62 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | public <T extends java.lang.annotation.Annotation> T getAnnotation() throws Exception { |
| 64 | String cname = name.getFQN(); |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 65 | try { |
| 66 | Class<T> c = (Class<T>) getClass().getClassLoader().loadClass(cname); |
| 67 | return getAnnotation(c); |
| 68 | } |
| 69 | catch (ClassNotFoundException e) { |
| 70 | } |
| 71 | catch (NoClassDefFoundError e) { |
| 72 | } |
| 73 | return null; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 74 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 75 | |
| 76 | public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> c) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 77 | String cname = name.getFQN(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | if (!c.getName().equals(cname)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | return null; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | return Configurable.createConfigurable(c, elements); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | } |
| 82 | } |