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.io.IOException;
017    import java.io.InputStream;
018    
019    import org.apache.bcel.classfile.ClassParser;
020    import org.apache.commons.classscan.MetaClassPathElement;
021    import org.apache.commons.classscan.MetaRegistry;
022    import org.apache.commons.classscan.spi.ClassDigesterFactory;
023    import org.apache.commons.classscan.spi.model.SpiClassDigester;
024    import org.apache.commons.classscan.spi.model.SpiMetaClass;
025    
026    /**
027     * A ClassDigesterFactory that uses BCEL to digest the class bytes
028     */
029    public class BcelClassDigesterFactory implements ClassDigesterFactory {
030    
031        // making digester an instance field ensures that BCEL library is loaded.
032        // If load is successful, this factory will produce a usable digester
033        private SpiClassDigester spiClassDigester = new SpiClassDigester() {
034                    @Override
035                    public SpiMetaClass createMetaClass(MetaClassPathElement location, String className, InputStream byteStream) throws IOException {
036                            return new BcelClass(location, new ClassParser(byteStream, className));
037                    }
038            };
039    
040            @Override
041            public SpiClassDigester createDigester(MetaRegistry metaRegistry) {
042                    return spiClassDigester;
043            }
044    }