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  
22  import org.apache.bcel.classfile.ClassParser;
23  import org.apache.bcel.classfile.JavaClass;
24  import org.apache.bcel.classfile.Utility;
25  
26  /**
27   * This abstract class provides a logic of a loading {@link JavaClass} objects class names via {@link ClassPath}.
28   *
29   * <p>
30   * Subclasses can choose caching strategy of the objects by implementing the abstract methods (e.g.,
31   * {@link #storeClass(JavaClass)} and {@link #findClass(String)}).
32   * </p>
33   *
34   * @since 6.4.0
35   */
36  abstract class AbstractClassPathRepository implements Repository {
37  
38      private final ClassPath classPath;
39  
40      AbstractClassPathRepository(final ClassPath classPath) {
41          this.classPath = classPath;
42      }
43  
44      @Override
45      public abstract void clear();
46  
47      @Override
48      public abstract JavaClass findClass(final String className);
49  
50      @Override
51      public ClassPath getClassPath() {
52          return classPath;
53      }
54  
55      /**
56       * Finds the JavaClass object for a runtime Class object. If a class with the same name is already in this Repository,
57       * the Repository version is returned. Otherwise, getResourceAsStream() is called on the Class object to find the
58       * class's representation. If the representation is found, it is added to the Repository.
59       *
60       * @see Class
61       * @param clazz the runtime Class object
62       * @return JavaClass object for given runtime class
63       * @throws ClassNotFoundException if the class is not in the Repository, and its representation could not be found
64       */
65      @Override
66      public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
67          final String className = clazz.getName();
68          final JavaClass repositoryClass = findClass(className);
69          if (repositoryClass != null) {
70              return repositoryClass;
71          }
72          String name = className;
73          final int i = name.lastIndexOf('.');
74          if (i > 0) {
75              name = name.substring(i + 1);
76          }
77  
78          try (InputStream clsStream = clazz.getResourceAsStream(name + JavaClass.EXTENSION)) {
79              return loadClass(clsStream, className);
80          } catch (final IOException e) {
81              return null;
82          }
83      }
84  
85      private JavaClass loadClass(final InputStream inputStream, final String className) throws ClassNotFoundException {
86          try {
87              if (inputStream != null) {
88                  final ClassParser parser = new ClassParser(inputStream, className);
89                  final JavaClass clazz = parser.parse();
90                  storeClass(clazz);
91                  return clazz;
92              }
93          } catch (final IOException e) {
94              throw new ClassNotFoundException("Exception while looking for class " + className + ": " + e, e);
95          }
96          throw new ClassNotFoundException("ClassRepository could not load " + className);
97      }
98  
99      /**
100      * Finds a JavaClass object by name. If it is already in this Repository, the Repository version is returned. Otherwise,
101      * the Repository's classpath is searched for the class (and it is added to the Repository if found).
102      *
103      * @param className the name of the class
104      * @return the JavaClass object
105      * @throws ClassNotFoundException if the class is not in the Repository, and could not be found on the classpath
106      */
107     @Override
108     public JavaClass loadClass(String className) throws ClassNotFoundException {
109         if (className == null || className.isEmpty()) {
110             throw new IllegalArgumentException("Invalid class name " + className);
111         }
112         className = Utility.pathToPackage(className); // Just in case, canonical form
113         final JavaClass clazz = findClass(className);
114         if (clazz != null) {
115             return clazz;
116         }
117         try (InputStream inputStream = classPath.getInputStream(className)) {
118             return loadClass(inputStream, className);
119         } catch (final IOException e) {
120             throw new ClassNotFoundException("Exception while looking for class " + className + ": " + e, e);
121         }
122     }
123 
124     @Override
125     public abstract void removeClass(final JavaClass javaClass);
126 
127     @Override
128     public abstract void storeClass(final JavaClass javaClass);
129 }