View Javadoc
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.io.IOException;
22  import java.io.InputStream;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.bcel.classfile.ClassParser;
27  import org.apache.bcel.classfile.JavaClass;
28  import org.apache.bcel.classfile.Utility;
29  
30  /**
31   * The repository maintains information about which classes have been loaded.
32   *
33   * It loads its data from the ClassLoader implementation passed into its constructor.
34   *
35   * @see org.apache.bcel.Repository
36   */
37  public class ClassLoaderRepository implements Repository {
38  
39      private final java.lang.ClassLoader loader;
40      private final Map<String, JavaClass> loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
41  
42      public ClassLoaderRepository(final java.lang.ClassLoader loader) {
43          this.loader = loader;
44      }
45  
46      /**
47       * Clear all entries from cache.
48       */
49      @Override
50      public void clear() {
51          loadedClasses.clear();
52      }
53  
54      /**
55       * Find an already defined JavaClass.
56       */
57      @Override
58      public JavaClass findClass(final String className) {
59          return loadedClasses.get(className);
60      }
61  
62      /*
63       * @return null
64       */
65      @Override
66      public ClassPath getClassPath() {
67          return null;
68      }
69  
70      @Override
71      public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
72          return loadClass(clazz.getName());
73      }
74  
75      /**
76       * Lookup a JavaClass object from the Class Name provided.
77       */
78      @Override
79      public JavaClass loadClass(final String className) throws ClassNotFoundException {
80          final String classFile = Utility.packageToPath(className);
81          JavaClass RC = findClass(className);
82          if (RC != null) {
83              return RC;
84          }
85          try (InputStream is = loader.getResourceAsStream(classFile + JavaClass.EXTENSION)) {
86              if (is == null) {
87                  throw new ClassNotFoundException(className + " not found.");
88              }
89              final ClassParser parser = new ClassParser(is, className);
90              RC = parser.parse();
91              storeClass(RC);
92              return RC;
93          } catch (final IOException e) {
94              throw new ClassNotFoundException(className + " not found: " + e, e);
95          }
96      }
97  
98      /**
99       * Remove class from repository
100      */
101     @Override
102     public void removeClass(final JavaClass clazz) {
103         loadedClasses.remove(clazz.getClassName());
104     }
105 
106     /**
107      * Store a new JavaClass into this Repository.
108      */
109     @Override
110     public void storeClass(final JavaClass clazz) {
111         loadedClasses.put(clazz.getClassName(), clazz);
112         clazz.setRepository(this);
113     }
114 }