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    public MemorySensitiveClassPathRepository(final ClassPath path) {
040        super(path);
041    }
042
043    /**
044     * Clear all entries from cache.
045     */
046    @Override
047    public void clear() {
048        loadedClasses.clear();
049    }
050
051    /**
052     * Find an already defined (cached) JavaClass object by name.
053     */
054    @Override
055    public JavaClass findClass(final String className) {
056        final SoftReference<JavaClass> ref = loadedClasses.get(className);
057        return ref == null ? null : ref.get();
058    }
059
060    /**
061     * Remove class from repository
062     */
063    @Override
064    public void removeClass(final JavaClass clazz) {
065        loadedClasses.remove(clazz.getClassName());
066    }
067
068    /**
069     * Store a new JavaClass instance into this Repository.
070     */
071    @Override
072    public void storeClass(final JavaClass clazz) {
073        // Not calling super.storeClass because this subclass maintains the mapping.
074        loadedClasses.put(clazz.getClassName(), new SoftReference<>(clazz));
075        clazz.setRepository(this);
076    }
077}