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  * This repository is used in situations where a Class is created outside the realm of a ClassLoader. Classes are loaded from the file systems using the paths
30  * specified in the given class path. By default, this is the value returned by ClassPath.getClassPath(). <br>
31  *
32  * @see org.apache.bcel.Repository
33  */
34 public class ClassPathRepository implements Repository {
35 
36     private ClassPath _path = null;
37     private final Map<String, JavaClass> _loadedClasses = new HashMap<>(); // CLASSNAME X JAVACLASS
38 
ClassPathRepository(final ClassPath path)39     public ClassPathRepository(final ClassPath path) {
40         _path = path;
41     }
42 
43     /**
44      * Store a new JavaClass instance into this Repository.
45      */
46     @Override
storeClass(final JavaClass clazz)47     public void storeClass(final JavaClass clazz) {
48         _loadedClasses.put(clazz.getClassName(), clazz);
49         clazz.setRepository(this);
50     }
51 
52     /**
53      * Remove class from repository
54      */
55     @Override
removeClass(final JavaClass clazz)56     public void removeClass(final JavaClass clazz) {
57         _loadedClasses.remove(clazz.getClassName());
58     }
59 
60     /**
61      * Find an already defined (cached) JavaClass object by name.
62      */
63     @Override
findClass(final String className)64     public JavaClass findClass(final String className) {
65         return _loadedClasses.get(className);
66     }
67 
68     /**
69      * Find a JavaClass object by name. If it is already in this Repository, the Repository version is returned. Otherwise, the Repository's classpath is
70      * searched for the class (and it is added to the Repository if found).
71      *
72      * @param className
73      *            the name of the class
74      * @return the JavaClass object
75      * @throws ClassNotFoundException
76      *             if the class is not in the Repository, and could not be found on the classpath
77      */
78     @Override
loadClass(String className)79     public JavaClass loadClass(String className) throws ClassNotFoundException {
80         if ((className == null) || className.isEmpty()) {
81             throw new IllegalArgumentException("Invalid class name " + className);
82         }
83         className = className.replace('/', '.'); // Just in case, canonical form
84         final JavaClass clazz = findClass(className);
85         if (clazz != null) {
86             return clazz;
87         }
88         try {
89             return loadClass(_path.getInputStream(className), className);
90         } catch (final IOException e) {
91             throw new ClassNotFoundException("Exception while looking for class " + className + ": " + e, e);
92         }
93     }
94 
95     /**
96      * Find the JavaClass object for a runtime Class object. If a class with the same name is already in this Repository, the Repository version is returned.
97      * Otherwise, getResourceAsStream() is called on the Class object to find the class's representation. If the representation is found, it is added to the
98      * Repository.
99      *
100      * @see Class
101      * @param clazz
102      *            the runtime Class object
103      * @return JavaClass object for given runtime class
104      * @throws ClassNotFoundException
105      *             if the class is not in the Repository, and its representation could not be found
106      */
107     @Override
loadClass(final Class<?> clazz)108     public JavaClass loadClass(final Class<?> clazz) throws ClassNotFoundException {
109         final String className = clazz.getName();
110         final JavaClass repositoryClass = findClass(className);
111         if (repositoryClass != null) {
112             return repositoryClass;
113         }
114         String name = className;
115         final int i = name.lastIndexOf('.');
116         if (i > 0) {
117             name = name.substring(i + 1);
118         }
119         JavaClass cls = null;
120         try (InputStream clsStream = clazz.getResourceAsStream(name + ".class")) {
121             return cls = loadClass(clsStream, className);
122         } catch (final IOException e) {
123             return cls;
124         }
125     }
126 
loadClass(final InputStream is, final String className)127     private JavaClass loadClass(final InputStream is, final String className) throws ClassNotFoundException {
128         try {
129             if (is != null) {
130                 final ClassParser parser = new ClassParser(is, className);
131                 final JavaClass clazz = parser.parse();
132                 storeClass(clazz);
133                 return clazz;
134             }
135         } catch (final IOException e) {
136             throw new ClassNotFoundException("Exception while looking for class " + className + ": " + e, e);
137         } finally {
138             if (is != null) {
139                 try {
140                     is.close();
141                 } catch (final IOException e) {
142                     // ignored
143                 }
144             }
145         }
146         throw new ClassNotFoundException("SyntheticRepository could not load " + className);
147     }
148 
149     /**
150      * ClassPath associated with the Repository.
151      */
152     @Override
getClassPath()153     public ClassPath getClassPath() {
154         return _path;
155     }
156 
157     /**
158      * Clear all entries from cache.
159      */
160     @Override
clear()161     public void clear() {
162         _loadedClasses.clear();
163     }
164 }
165