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.util.HashMap;
020import java.util.Map;
021
022import org.apache.bcel.classfile.JavaClass;
023
024/**
025 * This repository is used in situations where a Class is created outside the realm of a ClassLoader. Classes are loaded
026 * from the file systems using the paths specified in the given class path. By default, this is the value returned by
027 * ClassPath.getClassPath().
028 *
029 * @see org.apache.bcel.Repository
030 */
031public class ClassPathRepository extends AbstractClassPathRepository {
032
033    private final Map<String, JavaClass> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
034
035    public ClassPathRepository(final ClassPath classPath) {
036        super(classPath);
037    }
038
039    /**
040     * Clears all entries from cache.
041     */
042    @Override
043    public void clear() {
044        loadedClasses.clear();
045    }
046
047    /**
048     * Finds an already defined (cached) JavaClass object by name.
049     */
050    @Override
051    public JavaClass findClass(final String className) {
052        return loadedClasses.get(className);
053    }
054
055    /**
056     * Removes class from repository.
057     */
058    @Override
059    public void removeClass(final JavaClass javaClass) {
060        loadedClasses.remove(javaClass.getClassName());
061    }
062
063    /**
064     * Stores a new JavaClass instance into this Repository.
065     */
066    @Override
067    public void storeClass(final JavaClass javaClass) {
068        loadedClasses.put(javaClass.getClassName(), javaClass);
069        javaClass.setRepository(this);
070    }
071}