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