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