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