    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 */
    017package org.apache.bcel.util;
    018
    019import java.io.IOException;
    020import java.io.InputStream;
    021import java.util.HashMap;
    022import java.util.Map;
    023
    024import org.apache.bcel.classfile.ClassParser;
    025import org.apache.bcel.classfile.JavaClass;
    026
    027/**
    028 * The repository maintains information about which classes have been loaded.
    029 *
    030 * It loads its data from the ClassLoader implementation passed into its constructor.
    031 *
    032 * @see org.apache.bcel.Repository
    033 */
    034public class ClassLoaderRepository implements Repository {
    035
    036    private final java.lang.ClassLoader loader;
    037    private final Map<String, JavaClass> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
    038
    039    public ClassLoaderRepository(final java.lang.ClassLoader loader) {
    040        this.loader = loader;
    041    }
    042
    043    /**
    044     * Clear all entries from cache.
    045     */
    046    @Override
    047    public void clear() {
    048        loadedClasses.clear();
    049    }
    050
    051    /**
    052     * Find an already defined JavaClass.
    053     */
    054    @Override
    055    public JavaClass findClass(final String className) {
    056        return loadedClasses.get(className);
    057    }
    058
    059    /*
    060     * @return null
    061     */
    062    @Override
    063    public ClassPath getClassPath() {
    064        return null;
    065    }
    066
    067    @Override
    068    public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
    069        return loadClass(clazz.getName());
    070    }
    071
    072    /**
    073     * Lookup a JavaClass object from the Class Name provided.
    074     */
    075    @Override
    076    public JavaClass loadClass(final String className) throws ClassNotFoundException {
    077        final String classFile = className.replace('.', '/');
    078        JavaClass RC = findClass(className);
    079        if (RC != null) {
    080            return RC;
    081        }
    082        try (InputStream is = loader.getResourceAsStream(classFile + ".class")) {
    083            if (is == null) {
    084                throw new ClassNotFoundException(className + " not found.");
    085            }
    086            final ClassParser parser = new ClassParser(is, className);
    087            RC = parser.parse();
    088            storeClass(RC);
    089            return RC;
    090        } catch (final IOException e) {
    091            throw new ClassNotFoundException(className + " not found: " + e, e);
    092        }
    093    }
    094
    095    /**
    096     * Remove class from repository
    097     */
    098    @Override
    099    public void removeClass(final JavaClass clazz) {
    100        loadedClasses.remove(clazz.getClassName());
    101    }
    102
    103    /**
    104     * Store a new JavaClass into this Repository.
    105     */
    106    @Override
    107    public void storeClass(final JavaClass clazz) {
    108        loadedClasses.put(clazz.getClassName(), clazz);
    109        clazz.setRepository(this);
    110    }
    111}