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