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.HashMap;
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  
23  import org.objectweb.asm.Type;
24  
25  /**
26   * @author Eugene Kuleshov
27   */
28  public class AnnMethod {
29  
30      private final AnnClass owner;
31      private final int access;
32      private final String name;
33      private final String desc;
34      private Map<String, Ann> anns = new LinkedHashMap<String, Ann>();
35      private Map<Integer, Map<String, Ann>> paramAnns = new HashMap<Integer, Map<String, Ann>>();
36  
37      public AnnMethod(AnnClass owner, int access, String name, String desc) {
38          this.owner = owner;
39          this.access = access;
40          this.name = name;
41          this.desc = desc;
42      }
43  
44      public int getAccess() {
45          return access;
46      }
47  
48      public String getName() {
49          return name;
50      }
51  
52      public String getDesc() {
53          return desc;
54      }
55  
56      public Map<String, Ann> getAnns() {
57          return anns;
58      }
59  
60      public Map<Integer, Map<String, Ann>> getParamAnns() {
61          return paramAnns;
62      }
63  
64      public void addAnn(Ann ann) {
65          anns.put(ann.getDesc(), ann);
66      }
67  
68      public void addParamAnn(int parameter, Ann ann) {
69          Map<String, Ann> anns = paramAnns.get(parameter);
70          if (anns == null) {
71              anns = new LinkedHashMap<String, Ann>();
72              paramAnns.put(parameter, anns);
73          }
74          anns.put(ann.getDesc(), ann);
75      }
76  
77      public <T> T getAnnotation(Class<T> c) {
78          Ann ann = anns.get(Type.getDescriptor(c));
79          return ann == null ? null : ann.getAnnotation(c, owner.getClassLoader());
80      }
81  
82      public <T> T getParameterAnnotation(int parameter, Class<T> c) {
83          Map<String, Ann> anns = paramAnns.get(parameter);
84          if (anns == null) {
85              return null;
86          }
87          Ann ann = anns.get(Type.getDescriptor(c));
88          return ann == null ? null : ann.getAnnotation(c, owner.getClassLoader());
89      }
90  }