blob: aca0a11b2d77fc3bb53f7019921cf51bea455d55 [file] [log] [blame]
Stuart McCulloch42151ee2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochf3173222012-06-07 21:57:32 +00002
3import java.lang.annotation.*;
4import java.util.*;
5
6import aQute.bnd.annotation.metatype.*;
Stuart McCulloch42151ee2012-07-16 13:43:38 +00007import aQute.bnd.osgi.Descriptors.TypeRef;
Stuart McCullochf3173222012-06-07 21:57:32 +00008
Stuart McCulloch4482c702012-06-15 13:27:53 +00009public class Annotation {
Stuart McCullochf3173222012-06-07 21:57:32 +000010 TypeRef name;
Stuart McCulloch4482c702012-06-15 13:27:53 +000011 Map<String,Object> elements;
Stuart McCullochf3173222012-06-07 21:57:32 +000012 ElementType member;
13 RetentionPolicy policy;
14
Stuart McCulloch4482c702012-06-15 13:27:53 +000015 public Annotation(TypeRef name, Map<String,Object> elements, ElementType member, RetentionPolicy policy) {
Stuart McCullochf3173222012-06-07 21:57:32 +000016 this.name = name;
Stuart McCulloch4482c702012-06-15 13:27:53 +000017 if (elements == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000018 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 McCulloch4482c702012-06-15 13:27:53 +000032
Stuart McCullochf3173222012-06-07 21:57:32 +000033 public RetentionPolicy getRetentionPolicy() {
34 return policy;
35 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000036
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000037 @Override
Stuart McCullochf3173222012-06-07 21:57:32 +000038 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 McCulloch4482c702012-06-15 13:27:53 +000059
Stuart McCullochf3173222012-06-07 21:57:32 +000060 return elements.keySet();
61 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000062
Stuart McCullochf3173222012-06-07 21:57:32 +000063 public <T extends java.lang.annotation.Annotation> T getAnnotation() throws Exception {
64 String cname = name.getFQN();
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000065 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 McCullochf3173222012-06-07 21:57:32 +000074 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000075
76 public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> c) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000077 String cname = name.getFQN();
Stuart McCulloch4482c702012-06-15 13:27:53 +000078 if (!c.getName().equals(cname))
Stuart McCullochf3173222012-06-07 21:57:32 +000079 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000080 return Configurable.createConfigurable(c, elements);
Stuart McCullochf3173222012-06-07 21:57:32 +000081 }
82}