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.Field;
19  import org.apache.commons.classscan.model.MetaAnnotation;
20  import org.apache.commons.classscan.model.MetaType;
21  import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
22  import org.apache.commons.classscan.spi.model.SpiMetaField;
23  
24  public class BcelField implements SpiMetaField {
25  
26      private final String fieldName;
27      private final boolean isStatic;
28      private String fieldSignature;
29      private MetaType metaType;
30      private final AnnotationMap annotations;
31  
32      public BcelField(Field field) {
33          fieldName = field.getName();
34          isStatic = field.isStatic();
35          fieldSignature = field.getSignature();
36          annotations = AnnotationMap.createAnnotations(field.getAnnotationEntries());
37      }
38  
39  	@Override
40  	public boolean resolve(SpiMetaClassLoader classLoader) {
41  		metaType = classLoader.resolveTypeForDescriptor(fieldSignature);
42  		if(metaType == null) {
43  			return false;
44  		}
45  		fieldSignature = null;
46  		
47  		return annotations.resolve(classLoader);
48  	}
49  
50      @Override
51      public String getName() {
52          return fieldName;
53      }
54  
55      @Override
56      public Set<? extends MetaAnnotation> getAnnotations() {
57          return annotations;
58      }
59  
60      @Override
61      public MetaAnnotation getAnnotation(String annotationName) {
62          return annotations.getValue(annotationName);
63      }
64  
65      @Override
66      public boolean isStatic() {
67          return isStatic;
68      }
69  
70      @Override
71      public MetaType getType() {
72          return metaType;
73      }
74  }