    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.util;
    019
    020import java.io.IOException;
    021import java.io.InputStream;
    022import java.util.HashMap;
    023import java.util.Map;
    024
    025import org.apache.bcel.classfile.ClassParser;
    026import org.apache.bcel.classfile.JavaClass;
    027
    028/**
    029 * The repository maintains information about which classes have been loaded.
    030 *
    031 * It loads its data from the ClassLoader implementation passed into its constructor.
    032 *
    033 * @see org.apache.bcel.Repository
    034 *
    035 */
    036public class ClassLoaderRepository implements Repository {
    037
    038    private final java.lang.ClassLoader loader;
    039    private final Map<String, JavaClass> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
    040
    041    public ClassLoaderRepository(final java.lang.ClassLoader loader) {
    042        this.loader = loader;
    043    }
    044
    045    /**
    046     * Clear all entries from cache.
    047     */
    048    @Override
    049    public void clear() {
    050        loadedClasses.clear();
    051    }
    052
    053    /**
    054     * Find an already defined JavaClass.
    055     */
    056    @Override
    057    public JavaClass findClass(final String className) {
    058        return loadedClasses.get(className);
    059    }
    060
    061    /*
    062     * @return null
    063     */
    064    @Override
    065    public ClassPath getClassPath() {
    066        return null;
    067    }
    068
    069    @Override
    070    public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
    071        return loadClass(clazz.getName());
    072    }
    073
    074    /**
    075     * Lookup a JavaClass object from the Class Name provided.
    076     */
    077    @Override
    078    public JavaClass loadClass(final String className) throws ClassNotFoundException {
    079        final String classFile = className.replace('.', '/');
    080        JavaClass RC = findClass(className);
    081        if (RC != null) {
    082            return RC;
    083        }
    084        try (InputStream is = loader.getResourceAsStream(classFile + ".class")) {
    085            if (is == null) {
    086                throw new ClassNotFoundException(className + " not found.");
    087            }
    088            final ClassParser parser = new ClassParser(is, className);
    089            RC = parser.parse();
    090            storeClass(RC);
    091            return RC;
    092        } catch (final IOException e) {
    093            throw new ClassNotFoundException(className + " not found: " + e, e);
    094        }
    095    }
    096
    097    /**
    098     * Remove class from repository
    099     */
    100    @Override
    101    public void removeClass(final JavaClass clazz) {
    102        loadedClasses.remove(clazz.getClassName());
    103    }
    104
    105    /**
    106     * Store a new JavaClass into this Repository.
    107     */
    108    @Override
    109    public void storeClass(final JavaClass clazz) {
    110        loadedClasses.put(clazz.getClassName(), clazz);
    111        clazz.setRepository(this);
    112    }
    113}