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    /**
043     * Constructs a ClassLoaderRepository.
044     *
045     * @param loader The class loader.
046     */
047    public ClassLoaderRepository(final java.lang.ClassLoader loader) {
048        this.loader = loader;
049    }
050
051    /**
052     * Clear all entries from cache.
053     */
054    @Override
055    public void clear() {
056        loadedClasses.clear();
057    }
058
059    /**
060     * Find an already defined JavaClass.
061     */
062    @Override
063    public JavaClass findClass(final String className) {
064        return loadedClasses.get(className);
065    }
066
067    /*
068     * @return null.
069     */
070    @Override
071    public ClassPath getClassPath() {
072        return null;
073    }
074
075    @Override
076    public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
077        return loadClass(clazz.getName());
078    }
079
080    /**
081     * Lookup a JavaClass object from the Class Name provided.
082     */
083    @Override
084    public JavaClass loadClass(final String className) throws ClassNotFoundException {
085        final String classFile = Utility.packageToPath(className);
086        JavaClass RC = findClass(className);
087        if (RC != null) {
088            return RC;
089        }
090        try (InputStream is = loader.getResourceAsStream(classFile + JavaClass.EXTENSION)) {
091            if (is == null) {
092                throw new ClassNotFoundException(className + " not found.");
093            }
094            final ClassParser parser = new ClassParser(is, className);
095            RC = parser.parse();
096            // The this_class name inside the parsed bytes is attacker-controlled: caching it under its own name
097            // would let a class file found under one name poison the repository entry for another (the JDK's
098            // defineClass() rejects the same mismatch). Refuse to cache or return it.
099            if (!RC.getClassName().equals(className)) {
100                throw new ClassNotFoundException("Class name mismatch: requested " + className + " but the class file declares " + RC.getClassName());
101            }
102            storeClass(RC);
103            return RC;
104        } catch (final IOException e) {
105            throw new ClassNotFoundException(className + " not found: " + e, e);
106        }
107    }
108
109    /**
110     * Remove class from repository
111     */
112    @Override
113    public void removeClass(final JavaClass clazz) {
114        loadedClasses.remove(clazz.getClassName());
115    }
116
117    /**
118     * Store a new JavaClass into this Repository.
119     */
120    @Override
121    public void storeClass(final JavaClass clazz) {
122        loadedClasses.put(clazz.getClassName(), clazz);
123        clazz.setRepository(this);
124    }
125}