View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.apache.commons.classscan.bcel;
15  
16  import java.util.Set;
17  
18  import org.apache.bcel.classfile.AnnotationEntry;
19  import org.apache.bcel.classfile.ElementValuePair;
20  import org.apache.commons.classscan.builtin.ClassNameHelper;
21  import org.apache.commons.classscan.spi.model.SpiMetaAnnotation;
22  import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
23  import org.apache.commons.classscan.util.NameSet;
24  
25  public class BcelAnnotation implements SpiMetaAnnotation {
26  
27      private final String name;
28      private final NameSet<SpiProperty> properties;
29  
30      public BcelAnnotation(AnnotationEntry annotation) {
31          name = ClassNameHelper.internalToCanonicalName(annotation.getAnnotationType());
32  
33          ElementValuePair[] elementValuePairs = annotation.getElementValuePairs();
34          if (elementValuePairs.length == 0) {
35              properties = NameSet.emptyNameSet();
36          }
37          else {
38              properties = new NameSet<SpiProperty>(fillMapAndArray(elementValuePairs));
39          }
40      }
41  
42  	@Override
43  	public boolean resolve(SpiMetaClassLoader classLoader) {
44  		return properties.resolve(classLoader);
45  	}
46  
47      private SpiProperty[] fillMapAndArray(ElementValuePair[] evPairs) {
48          SpiProperty[] values = new SpiProperty[evPairs.length];
49          for (int i = 0; i < evPairs.length; ++i) {
50              values[i] = new BcelProperty(evPairs[i]);
51          }
52          return values;
53      }
54  
55      @Override
56      public String getName() {
57          return name;
58      }
59  
60      @Override
61      public Set<? extends Property> getProperties() {
62          return properties;
63      }
64  
65      @Override
66      public Property getProperty(String propertyName) {
67          return properties.getValue(propertyName);
68      }
69  }