1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.util;
20
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23
24 import org.apache.bcel.classfile.JavaClass;
25
26 /**
27 * Maintains a least-recently-used (LRU) cache of {@link JavaClass} with maximum size {@code cacheSize}.
28 *
29 * <p>
30 * This repository supports a class path consisting of too many JAR files to handle in {@link ClassPathRepository} or
31 * {@link MemorySensitiveClassPathRepository} without causing {@code OutOfMemoryError}.
32 * </p>
33 *
34 * @since 6.4.0
35 */
36 public class LruCacheClassPathRepository extends AbstractClassPathRepository {
37
38 private final LinkedHashMap<String, JavaClass> loadedClasses;
39
40 public LruCacheClassPathRepository(final ClassPath path, final int cacheSize) {
41 super(path);
42
43 if (cacheSize < 1) {
44 throw new IllegalArgumentException("cacheSize must be a positive number.");
45 }
46 final int initialCapacity = (int) (0.75 * cacheSize);
47 final boolean accessOrder = true; // Evicts least-recently-accessed
48 loadedClasses = new LinkedHashMap<String, JavaClass>(initialCapacity, cacheSize, accessOrder) {
49
50 private static final long serialVersionUID = 1L;
51
52 @Override
53 protected boolean removeEldestEntry(final Map.Entry<String, JavaClass> eldest) {
54 return size() > cacheSize;
55 }
56 };
57 }
58
59 @Override
60 public void clear() {
61 loadedClasses.clear();
62 }
63
64 @Override
65 public JavaClass findClass(final String className) {
66 return loadedClasses.get(className);
67 }
68
69 @Override
70 public void removeClass(final JavaClass javaClass) {
71 loadedClasses.remove(javaClass.getClassName());
72 }
73
74 @Override
75 public void storeClass(final JavaClass javaClass) {
76 // Not storing parent's _loadedClass
77 loadedClasses.put(javaClass.getClassName(), javaClass);
78 javaClass.setRepository(this);
79 }
80 }