View Javadoc
1   /*
2    * Copyright (C) 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.plexus.metadata.ann;
18  
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import org.objectweb.asm.Type;
24  
25  /**
26   * @author Eugene Kuleshov
27   */
28  public class AnnClass {
29    
30    private int access;
31    private String name;
32    private String superName;
33    private String[] interfaces;
34    
35    private Map<String, Ann> anns = new LinkedHashMap<String, Ann>();
36    private Map<String, AnnField> fields = new LinkedHashMap<String, AnnField>();
37    private Map<String, AnnMethod> methods = new LinkedHashMap<String, AnnMethod>();
38    private ClassLoader cl;
39  
40    // setters
41    
42    public AnnClass(ClassLoader cl) {
43      this.cl = cl;
44    }
45  
46    public void setName(String name) {
47      this.name = name;
48    }
49  
50    public void setAccess(int access) {
51      this.access = access;
52    }
53  
54    public void setSuperName(String superName) {
55      this.superName = superName;
56    }
57  
58    public void setInterfaces(String[] interfaces) {
59      this.interfaces = interfaces;
60    }
61    
62    public void addAnn(Ann ann) {
63      anns.put(ann.getDesc(), ann);
64    }
65    
66    public void addField(AnnField field) {
67      fields.put(field.getName(), field);
68    }
69    
70    public void addMethod(AnnMethod method) {
71      methods.put(method.getName() + method.getDesc(), method);
72    }
73    
74    // getters
75    
76    public ClassLoader getClassLoader() {
77      return cl;
78    }
79    
80    public int getAccess() {
81      return access;
82    }
83  
84    public String getName() {
85      return name;
86    }
87    
88    public String getSuperName() {
89      return superName;
90    }
91  
92    public String[] getInterfaces() {
93      return interfaces;
94    }
95  
96    public Map<String, Ann> getAnns() {
97      return anns;
98    }
99  
100   public Map<String, AnnField> getFields() {
101     return fields;
102   }
103 
104   public Map<String, AnnMethod> getMethods() {
105     return methods;
106   }
107 
108   public Set<String> getFieldNames() {
109     return fields.keySet();
110   }
111   
112   public Set<String> getMethodKeys() {
113     return methods.keySet();
114   }
115   
116   // conversion to java.lang.Annotation
117   
118   public <T> T getAnnotation(Class<T> c) {
119     Ann ann = anns.get(Type.getDescriptor(c));
120     return ann == null ? null : ann.getAnnotation(c, cl);
121   }
122 
123   public <T> T getFieldAnnotation(String fieldName, Class<T> c) {
124     AnnField field = fields.get(fieldName);
125     return field==null ? null : field.getAnnotation(c);
126   }
127   
128   public <T> T getMethodAnnotation(String methodKey, Class<T> c) {
129     AnnMethod method = methods.get(methodKey);
130     return method==null ? null : method.getAnnotation(c);
131   }
132 
133 }
134