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.lang.ref.SoftReference;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.bcel.classfile.JavaClass;
026
027/**
028 * This repository is used in situations where a Class is created outside the realm of a ClassLoader. Classes are loaded
029 * from the file systems using the paths specified in the given class path. By default, this is the value returned by
030 * ClassPath.getClassPath(). This repository holds onto classes with SoftReferences, and will reload as needed, in cases
031 * where memory sizes are important.
032 *
033 * @see org.apache.bcel.Repository
034 */
035public class MemorySensitiveClassPathRepository extends AbstractClassPathRepository {
036
037    private final Map<String, SoftReference<JavaClass>> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
038
039    /**
040     * Constructs a new MemorySensitiveClassPathRepository.
041     *
042     * @param path The class path.
043     */
044    public MemorySensitiveClassPathRepository(final ClassPath path) {
045        super(path);
046    }
047
048    /**
049     * Clear all entries from cache.
050     */
051    @Override
052    public void clear() {
053        loadedClasses.clear();
054    }
055
056    /**
057     * Find an already defined (cached) JavaClass object by name.
058     */
059    @Override
060    public JavaClass findClass(final String className) {
061        final SoftReference<JavaClass> ref = loadedClasses.get(className);
062        return ref == null ? null : ref.get();
063    }
064
065    /**
066     * Remove class from repository
067     */
068    @Override
069    public void removeClass(final JavaClass clazz) {
070        loadedClasses.remove(clazz.getClassName());
071    }
072
073    /**
074     * Store a new JavaClass instance into this Repository.
075     */
076    @Override
077    public void storeClass(final JavaClass clazz) {
078        // Not calling super.storeClass because this subclass maintains the mapping.
079        loadedClasses.put(clazz.getClassName(), new SoftReference<>(clazz));
080        clazz.setRepository(this);
081    }
082}