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