blob: 37c4cadf078b03720b5f0e828800e84a8b0cc6e7 [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
3import java.lang.annotation.*;
4import java.util.*;
5
6import aQute.bnd.annotation.metatype.*;
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00007import aQute.bnd.osgi.Descriptors.TypeRef;
Stuart McCullochbb014372012-06-07 21:57:32 +00008
Stuart McCulloch2286f232012-06-15 13:27:53 +00009public class Annotation {
Stuart McCullochbb014372012-06-07 21:57:32 +000010 TypeRef name;
Stuart McCulloch2286f232012-06-15 13:27:53 +000011 Map<String,Object> elements;
Stuart McCullochbb014372012-06-07 21:57:32 +000012 ElementType member;
13 RetentionPolicy policy;
14
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 public Annotation(TypeRef name, Map<String,Object> elements, ElementType member, RetentionPolicy policy) {
Stuart McCullochbb014372012-06-07 21:57:32 +000016 this.name = name;
Stuart McCulloch2286f232012-06-15 13:27:53 +000017 if (elements == null)
Stuart McCullochbb014372012-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 McCulloch2286f232012-06-15 13:27:53 +000032
Stuart McCullochbb014372012-06-07 21:57:32 +000033 public RetentionPolicy getRetentionPolicy() {
34 return policy;
35 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000036
Stuart McCullochbb014372012-06-07 21:57:32 +000037 public String toString() {
38 return name + ":" + member + ":" + policy + ":" + elements;
39 }
40
41 public <T> T get(String string) {
42 if (elements == null)
43 return null;
44
45 return (T) elements.get(string);
46 }
47
48 public <T> void put(String string, Object v) {
49 if (elements == null)
50 return;
51
52 elements.put(string, v);
53 }
54
55 public Set<String> keySet() {
56 if (elements == null)
57 return Collections.emptySet();
Stuart McCulloch2286f232012-06-15 13:27:53 +000058
Stuart McCullochbb014372012-06-07 21:57:32 +000059 return elements.keySet();
60 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000061
Stuart McCullochbb014372012-06-07 21:57:32 +000062 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 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000067
68 public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> c) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +000069 String cname = name.getFQN();
Stuart McCulloch2286f232012-06-15 13:27:53 +000070 if (!c.getName().equals(cname))
Stuart McCullochbb014372012-06-07 21:57:32 +000071 return null;
Stuart McCulloch2286f232012-06-15 13:27:53 +000072 return Configurable.createConfigurable(c, elements);
Stuart McCullochbb014372012-06-07 21:57:32 +000073 }
74}