001/*
002 *  Copyright 2012 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.scanner.core.helper;
017
018import java.io.BufferedInputStream;
019import java.io.DataInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.WeakHashMap;
023
024import javassist.bytecode.ClassFile;
025
026public final class JavassistClassFileHelper {
027
028        private static final WeakHashMap<String, ClassFile> CLASSFILES_BY_NAME = new WeakHashMap<String, ClassFile>(1024, 0.6f);
029        private static final WeakHashMap<String, ClassFile> CLASSFILES_BY_FILE = new WeakHashMap<String, ClassFile>(1024, 0.6f);
030        
031        private static final int MAXIMUM_SIZE = 2048;
032        
033        private JavassistClassFileHelper() {
034        }
035        
036    public static ClassFile constructClassFile(String className, InputStream bits) throws IOException {
037
038                ClassFile cachedClassFile = CLASSFILES_BY_NAME.get(className);
039                if (cachedClassFile != null) {
040                        return cachedClassFile;
041                }
042        
043        DataInputStream dstream = new DataInputStream(new BufferedInputStream(bits));
044        ClassFile cf = null;
045        try {
046            cf = new ClassFile(dstream);
047        } finally {
048            dstream.close();
049            bits.close();
050        }
051        
052        if (cf != null) {
053                CLASSFILES_BY_NAME.put(className, cf);
054        }
055        return cf;
056    }
057    
058    public static ClassFile constructClassFileForPath(String path, InputStream bits) throws IOException {
059
060                ClassFile cachedClassFile = CLASSFILES_BY_FILE.get(path);
061                if (cachedClassFile != null) {
062                        return cachedClassFile;
063                }
064        
065        DataInputStream dstream = new DataInputStream(new BufferedInputStream(bits));
066        ClassFile cf = null;
067        try {
068            cf = new ClassFile(dstream);
069        } finally {
070            dstream.close();
071            bits.close();
072        }
073        
074        if (cf != null) {
075                if (CLASSFILES_BY_NAME.size() < MAXIMUM_SIZE) {
076                        CLASSFILES_BY_NAME.put(cf.getName(), cf);
077                }
078                if (CLASSFILES_BY_FILE.size() < MAXIMUM_SIZE) {
079                        CLASSFILES_BY_FILE.put(path, cf);
080                }
081        }
082        return cf;
083    }}