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 */
018 package org.apache.bcel.util;
019
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.util.HashMap;
023 import java.util.Map;
024 import org.apache.bcel.classfile.ClassParser;
025 import org.apache.bcel.classfile.JavaClass;
026
027 /**
028 * The repository maintains information about which classes have
029 * been loaded.
030 *
031 * It loads its data from the ClassLoader implementation
032 * passed into its constructor.
033 *
034 * @see org.apache.bcel.Repository
035 *
036 * @version $Id: ClassLoaderRepository.java 1149459 2011-07-22 04:34:27Z dbrosius $
037 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
038 * @author David Dixon-Peugh
039 */
040 public class ClassLoaderRepository implements Repository {
041
042 private static final long serialVersionUID = -1052781833503868187L;
043 private java.lang.ClassLoader loader;
044 private Map<String, JavaClass> loadedClasses = new HashMap<String, JavaClass>(); // CLASSNAME X JAVACLASS
045
046
047 public ClassLoaderRepository(java.lang.ClassLoader loader) {
048 this.loader = loader;
049 }
050
051
052 /**
053 * Store a new JavaClass into this Repository.
054 */
055 public void storeClass( JavaClass clazz ) {
056 loadedClasses.put(clazz.getClassName(), clazz);
057 clazz.setRepository(this);
058 }
059
060
061 /**
062 * Remove class from repository
063 */
064 public void removeClass( JavaClass clazz ) {
065 loadedClasses.remove(clazz.getClassName());
066 }
067
068
069 /**
070 * Find an already defined JavaClass.
071 */
072 public JavaClass findClass( String className ) {
073 if (loadedClasses.containsKey(className)) {
074 return loadedClasses.get(className);
075 } else {
076 return null;
077 }
078 }
079
080
081 /**
082 * Lookup a JavaClass object from the Class Name provided.
083 */
084 public JavaClass loadClass( String className ) throws ClassNotFoundException {
085 String classFile = className.replace('.', '/');
086 JavaClass RC = findClass(className);
087 if (RC != null) {
088 return RC;
089 }
090 try {
091 InputStream is = loader.getResourceAsStream(classFile + ".class");
092 if (is == null) {
093 throw new ClassNotFoundException(className + " not found.");
094 }
095 ClassParser parser = new ClassParser(is, className);
096 RC = parser.parse();
097 storeClass(RC);
098 return RC;
099 } catch (IOException e) {
100 throw new ClassNotFoundException(className + " not found: " + e.toString(), e);
101 }
102 }
103
104
105 public JavaClass loadClass( Class<?> clazz ) throws ClassNotFoundException {
106 return loadClass(clazz.getName());
107 }
108
109
110 /** Clear all entries from cache.
111 */
112 public void clear() {
113 loadedClasses.clear();
114 }
115
116
117 /*
118 * @return null
119 */
120 public ClassPath getClassPath() {
121 return null;
122 }
123 }