View Javadoc
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   */
38  public class ClassLoaderRepository implements Repository {
39  
40      private final java.lang.ClassLoader loader;
41      private final Map<String, JavaClass> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
42  
43  
44      public ClassLoaderRepository(final java.lang.ClassLoader loader) {
45          this.loader = loader;
46      }
47  
48  
49      /**
50       * Store a new JavaClass into this Repository.
51       */
52      @Override
53      public void storeClass( final JavaClass clazz ) {
54          loadedClasses.put(clazz.getClassName(), clazz);
55          clazz.setRepository(this);
56      }
57  
58  
59      /**
60       * Remove class from repository
61       */
62      @Override
63      public void removeClass( final JavaClass clazz ) {
64          loadedClasses.remove(clazz.getClassName());
65      }
66  
67  
68      /**
69       * Find an already defined JavaClass.
70       */
71      @Override
72      public JavaClass findClass( final String className ) {
73          return loadedClasses.containsKey(className) ? loadedClasses.get(className) : null;
74      }
75  
76  
77      /**
78       * Lookup a JavaClass object from the Class Name provided.
79       */
80      @Override
81      public JavaClass loadClass(final String className) throws ClassNotFoundException {
82          final String classFile = className.replace('.', '/');
83          JavaClass RC = findClass(className);
84          if (RC != null) {
85              return RC;
86          }
87          try (InputStream is = loader.getResourceAsStream(classFile + ".class")) {
88              if (is == null) {
89                  throw new ClassNotFoundException(className + " not found.");
90              }
91              final ClassParserrser.html#ClassParser">ClassParser parser = new ClassParser(is, className);
92              RC = parser.parse();
93              storeClass(RC);
94              return RC;
95          } catch (final IOException e) {
96              throw new ClassNotFoundException(className + " not found: " + e, e);
97          }
98      }
99  
100 
101     @Override
102     public JavaClass loadClass( final Class<?> clazz ) throws ClassNotFoundException {
103         return loadClass(clazz.getName());
104     }
105 
106 
107     /** Clear all entries from cache.
108      */
109     @Override
110     public void clear() {
111         loadedClasses.clear();
112     }
113 
114 
115     /*
116      * @return null
117      */
118     @Override
119     public ClassPath getClassPath() {
120         return null;
121     }
122 }