001 /*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014 package org.apache.commons.classscan.bcel;
015
016 import java.util.Set;
017
018 import org.apache.bcel.classfile.Field;
019 import org.apache.commons.classscan.model.MetaAnnotation;
020 import org.apache.commons.classscan.model.MetaType;
021 import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
022 import org.apache.commons.classscan.spi.model.SpiMetaField;
023
024 public class BcelField implements SpiMetaField {
025
026 private final String fieldName;
027 private final boolean isStatic;
028 private String fieldSignature;
029 private MetaType metaType;
030 private final AnnotationMap annotations;
031
032 public BcelField(Field field) {
033 fieldName = field.getName();
034 isStatic = field.isStatic();
035 fieldSignature = field.getSignature();
036 annotations = AnnotationMap.createAnnotations(field.getAnnotationEntries());
037 }
038
039 @Override
040 public boolean resolve(SpiMetaClassLoader classLoader) {
041 metaType = classLoader.resolveTypeForDescriptor(fieldSignature);
042 if(metaType == null) {
043 return false;
044 }
045 fieldSignature = null;
046
047 return annotations.resolve(classLoader);
048 }
049
050 @Override
051 public String getName() {
052 return fieldName;
053 }
054
055 @Override
056 public Set<? extends MetaAnnotation> getAnnotations() {
057 return annotations;
058 }
059
060 @Override
061 public MetaAnnotation getAnnotation(String annotationName) {
062 return annotations.getValue(annotationName);
063 }
064
065 @Override
066 public boolean isStatic() {
067 return isStatic;
068 }
069
070 @Override
071 public MetaType getType() {
072 return metaType;
073 }
074 }