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