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