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.ParameterAnnotationEntry;
019    import org.apache.bcel.generic.Type;
020    import org.apache.commons.classscan.model.MetaAnnotation;
021    import org.apache.commons.classscan.model.MetaType;
022    import org.apache.commons.classscan.spi.model.SpiMetaAnnotation;
023    import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
024    import org.apache.commons.classscan.spi.model.SpiMetaParameter;
025    import org.apache.commons.classscan.util.NameSet;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    public class BcelParameter implements SpiMetaParameter {
030    
031        private static final Logger logger = LoggerFactory.getLogger(BcelParameter.class);
032    
033        private MetaType type;
034        private String typeName;
035        private final NameSet<? extends SpiMetaAnnotation> annotations;
036    
037        public BcelParameter(Type argType, ParameterAnnotationEntry annotationEntry) {
038            typeName = argType.getSignature();
039            annotations = AnnotationMap.createAnnotations(annotationEntry != null 
040                            ?annotationEntry.getAnnotationEntries() :null);
041        }
042    
043            @Override
044            public boolean resolve(SpiMetaClassLoader classLoader) {
045            type = classLoader.resolveTypeForDescriptor(typeName);
046            if(type==null) {
047                    logger.info("cannot resolve "+typeName+" as a parameter type");
048                    return false;
049            }
050            typeName= null;
051                    return annotations.resolve(classLoader);
052            }
053    
054        @Override
055        public Set<? extends MetaAnnotation> getAnnotations() {
056            return annotations;
057        }
058    
059        @Override
060        public MetaAnnotation getAnnotation(String annotationName) {
061            return annotations.getValue(annotationName);
062        }
063    
064        @Override
065        public MetaType getType() {
066            return type;
067        }
068    }