blob: cafe0e3c4eb40736176f41b93c924f8ca6367747 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.osgi;
2
3import java.lang.annotation.*;
4import java.util.*;
5
6import aQute.bnd.annotation.metatype.*;
7import aQute.lib.osgi.Descriptors.TypeRef;
8
Stuart McCulloch4482c702012-06-15 13:27:53 +00009@SuppressWarnings("unchecked")
10public class Annotation {
Stuart McCullochf3173222012-06-07 21:57:32 +000011 TypeRef name;
Stuart McCulloch4482c702012-06-15 13:27:53 +000012 Map<String,Object> elements;
Stuart McCullochf3173222012-06-07 21:57:32 +000013 ElementType member;
14 RetentionPolicy policy;
15
Stuart McCulloch4482c702012-06-15 13:27:53 +000016 public Annotation(TypeRef name, Map<String,Object> elements, ElementType member, RetentionPolicy policy) {
Stuart McCullochf3173222012-06-07 21:57:32 +000017 this.name = name;
Stuart McCulloch4482c702012-06-15 13:27:53 +000018 if (elements == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000019 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 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000033
Stuart McCullochf3173222012-06-07 21:57:32 +000034 public RetentionPolicy getRetentionPolicy() {
35 return policy;
36 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000037
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();
65 Class<T> c = (Class<T>) getClass().getClassLoader().loadClass(cname);
66 return getAnnotation(c);
67 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000068
69 public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> c) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000070 String cname = name.getFQN();
Stuart McCulloch4482c702012-06-15 13:27:53 +000071 if (!c.getName().equals(cname))
Stuart McCullochf3173222012-06-07 21:57:32 +000072 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000073 return Configurable.createConfigurable(c, elements);
Stuart McCullochf3173222012-06-07 21:57:32 +000074 }
75}