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.AnnotationEntry;
019    import org.apache.bcel.classfile.ElementValuePair;
020    import org.apache.commons.classscan.builtin.ClassNameHelper;
021    import org.apache.commons.classscan.spi.model.SpiMetaAnnotation;
022    import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
023    import org.apache.commons.classscan.util.NameSet;
024    
025    public class BcelAnnotation implements SpiMetaAnnotation {
026    
027        private final String name;
028        private final NameSet<SpiProperty> properties;
029    
030        public BcelAnnotation(AnnotationEntry annotation) {
031            name = ClassNameHelper.internalToCanonicalName(annotation.getAnnotationType());
032    
033            ElementValuePair[] elementValuePairs = annotation.getElementValuePairs();
034            if (elementValuePairs.length == 0) {
035                properties = NameSet.emptyNameSet();
036            }
037            else {
038                properties = new NameSet<SpiProperty>(fillMapAndArray(elementValuePairs));
039            }
040        }
041    
042            @Override
043            public boolean resolve(SpiMetaClassLoader classLoader) {
044                    return properties.resolve(classLoader);
045            }
046    
047        private SpiProperty[] fillMapAndArray(ElementValuePair[] evPairs) {
048            SpiProperty[] values = new SpiProperty[evPairs.length];
049            for (int i = 0; i < evPairs.length; ++i) {
050                values[i] = new BcelProperty(evPairs[i]);
051            }
052            return values;
053        }
054    
055        @Override
056        public String getName() {
057            return name;
058        }
059    
060        @Override
061        public Set<? extends Property> getProperties() {
062            return properties;
063        }
064    
065        @Override
066        public Property getProperty(String propertyName) {
067            return properties.getValue(propertyName);
068        }
069    }