1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 package org.apache.bcel.util; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.util.HashMap; 23 import java.util.Map; 24 25 import org.apache.bcel.classfile.ClassParser; 26 import org.apache.bcel.classfile.JavaClass; 27 28 /** 29 * The repository maintains information about which classes have 30 * been loaded. 31 * 32 * It loads its data from the ClassLoader implementation 33 * passed into its constructor. 34 * 35 * @see org.apache.bcel.Repository 36 * 37 * @version $Id$ 38 */ 39 public class ClassLoaderRepository implements Repository { 40 41 private final java.lang.ClassLoader loader; 42 private final Map<String, JavaClass> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS 43 44 ClassLoaderRepository(final java.lang.ClassLoader loader)45 public ClassLoaderRepository(final java.lang.ClassLoader loader) { 46 this.loader = loader; 47 } 48 49 50 /** 51 * Store a new JavaClass into this Repository. 52 */ 53 @Override storeClass( final JavaClass clazz )54 public void storeClass( final JavaClass clazz ) { 55 loadedClasses.put(clazz.getClassName(), clazz); 56 clazz.setRepository(this); 57 } 58 59 60 /** 61 * Remove class from repository 62 */ 63 @Override removeClass( final JavaClass clazz )64 public void removeClass( final JavaClass clazz ) { 65 loadedClasses.remove(clazz.getClassName()); 66 } 67 68 69 /** 70 * Find an already defined JavaClass. 71 */ 72 @Override findClass( final String className )73 public JavaClass findClass( final String className ) { 74 return loadedClasses.containsKey(className) ? loadedClasses.get(className) : null; 75 } 76 77 78 /** 79 * Lookup a JavaClass object from the Class Name provided. 80 */ 81 @Override loadClass(final String className)82 public JavaClass loadClass(final String className) throws ClassNotFoundException { 83 final String classFile = className.replace('.', '/'); 84 JavaClass RC = findClass(className); 85 if (RC != null) { 86 return RC; 87 } 88 try (InputStream is = loader.getResourceAsStream(classFile + ".class")) { 89 if (is == null) { 90 throw new ClassNotFoundException(className + " not found."); 91 } 92 final ClassParser parser = new ClassParser(is, className); 93 RC = parser.parse(); 94 storeClass(RC); 95 return RC; 96 } catch (final IOException e) { 97 throw new ClassNotFoundException(className + " not found: " + e, e); 98 } 99 } 100 101 102 @Override loadClass( final Class<?> clazz )103 public JavaClass loadClass( final Class<?> clazz ) throws ClassNotFoundException { 104 return loadClass(clazz.getName()); 105 } 106 107 108 /** Clear all entries from cache. 109 */ 110 @Override clear()111 public void clear() { 112 loadedClasses.clear(); 113 } 114 115 116 /* 117 * @return null 118 */ 119 @Override getClassPath()120 public ClassPath getClassPath() { 121 return null; 122 } 123 } 124