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