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 from the file systems using the paths
027 * specified in the given class path. By default, this is the value returned by 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     * Stores a new JavaClass instance into this Repository.
041     */
042    @Override
043    public void storeClass(final JavaClass javaClass) {
044        _loadedClasses.put(javaClass.getClassName(), javaClass);
045        javaClass.setRepository(this);
046    }
047
048    /**
049     * Removes class from repository.
050     */
051    @Override
052    public void removeClass(final JavaClass javaClass) {
053        _loadedClasses.remove(javaClass.getClassName());
054    }
055
056    /**
057     * Finds an already defined (cached) JavaClass object by name.
058     */
059    @Override
060    public JavaClass findClass(final String className) {
061        return _loadedClasses.get(className);
062    }
063
064    /**
065     * Clears all entries from cache.
066     */
067    @Override
068    public void clear() {
069        _loadedClasses.clear();
070    }
071}