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    public ClassPathRepository(final ClassPath classPath) {
038        super(classPath);
039    }
040
041    /**
042     * Clears all entries from cache.
043     */
044    @Override
045    public void clear() {
046        loadedClasses.clear();
047    }
048
049    /**
050     * Finds an already defined (cached) JavaClass object by name.
051     */
052    @Override
053    public JavaClass findClass(final String className) {
054        return loadedClasses.get(className);
055    }
056
057    /**
058     * Removes class from repository.
059     */
060    @Override
061    public void removeClass(final JavaClass javaClass) {
062        loadedClasses.remove(javaClass.getClassName());
063    }
064
065    /**
066     * Stores a new JavaClass instance into this Repository.
067     */
068    @Override
069    public void storeClass(final JavaClass javaClass) {
070        loadedClasses.put(javaClass.getClassName(), javaClass);
071        javaClass.setRepository(this);
072    }
073}