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.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.util.Hashtable;
22  
23  import org.apache.bcel.Const;
24  import org.apache.bcel.classfile.ClassParser;
25  import org.apache.bcel.classfile.ConstantClass;
26  import org.apache.bcel.classfile.ConstantPool;
27  import org.apache.bcel.classfile.ConstantUtf8;
28  import org.apache.bcel.classfile.JavaClass;
29  import org.apache.bcel.classfile.Utility;
30  
31  /**
32   * <p>
33   * Drop in replacement for the standard class loader of the JVM. You can use it in conjunction with the JavaWrapper to
34   * dynamically modify/create classes as they're requested.
35   * </p>
36   *
37   * <p>
38   * This class loader recognizes special requests in a distinct format, i.e., when the name of the requested class
39   * contains with "$$BCEL$$" it calls the createClass() method with that name (everything bevor the $$BCEL$$ is
40   * considered to be the package name. You can subclass the class loader and override that method. "Normal" classes class
41   * can be modified by overriding the modifyClass() method which is called just before defineClass().
42   * </p>
43   *
44   * <p>
45   * There may be a number of packages where you have to use the default class loader (which may also be faster). You can
46   * define the set of packages where to use the system class loader in the constructor. The default value contains
47   * "java.", "sun.", "javax."
48   * </p>
49   *
50   * @see JavaWrapper
51   * @see ClassPath
52   * @deprecated 6.0 Do not use - does not work
53   */
54  @Deprecated
55  public class ClassLoader extends java.lang.ClassLoader {
56  
57      private static final String BCEL_TOKEN = "$$BCEL$$";
58  
59      public static final String[] DEFAULT_IGNORED_PACKAGES = {"java.", "javax.", "sun."};
60  
61      private final Hashtable<String, Class<?>> classes = new Hashtable<>();
62      // Hashtable is synchronized thus thread-safe
63      private final String[] ignoredPackages;
64      private Repository repository = SyntheticRepository.getInstance();
65  
66      /**
67       * Ignored packages are by default ( "java.", "sun.", "javax."), i.e. loaded by system class loader
68       */
69      public ClassLoader() {
70          this(DEFAULT_IGNORED_PACKAGES);
71      }
72  
73      /**
74       * @param deferTo delegate class loader to use for ignored packages
75       */
76      public ClassLoader(final java.lang.ClassLoader deferTo) {
77          super(deferTo);
78          this.ignoredPackages = DEFAULT_IGNORED_PACKAGES;
79          this.repository = new ClassLoaderRepository(deferTo);
80      }
81  
82      /**
83       * @param ignoredPackages classes contained in these packages will be loaded with the system class loader
84       * @param deferTo delegate class loader to use for ignored packages
85       */
86      public ClassLoader(final java.lang.ClassLoader deferTo, final String[] ignoredPackages) {
87          this(ignoredPackages);
88          this.repository = new ClassLoaderRepository(deferTo);
89      }
90  
91      /**
92       * @param ignoredPackages classes contained in these packages will be loaded with the system class loader
93       */
94      public ClassLoader(final String[] ignoredPackages) {
95          this.ignoredPackages = ignoredPackages;
96      }
97  
98      /**
99       * Override this method to create you own classes on the fly. The name contains the special token $$BCEL$$. Everything
100      * before that token is considered to be a package name. You can encode your own arguments into the subsequent string.
101      * You must ensure however not to use any "illegal" characters, i.e., characters that may not appear in a Java class
102      * name too
103      * <p>
104      * The default implementation interprets the string as a encoded compressed Java class, unpacks and decodes it with the
105      * Utility.decode() method, and parses the resulting byte array and returns the resulting JavaClass object.
106      * </p>
107      *
108      * @param className compressed byte code with "$$BCEL$$" in it
109      */
110     protected JavaClass createClass(final String className) {
111         final int index = className.indexOf(BCEL_TOKEN);
112         final String realName = className.substring(index + BCEL_TOKEN.length());
113         JavaClass clazz = null;
114         try {
115             final byte[] bytes = Utility.decode(realName, true);
116             final ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");
117             clazz = parser.parse();
118         } catch (final IOException e) {
119             e.printStackTrace();
120             return null;
121         }
122         // Adapt the class name to the passed value
123         final ConstantPool cp = clazz.getConstantPool();
124         final ConstantClass cl = cp.getConstant(clazz.getClassNameIndex(), Const.CONSTANT_Class, ConstantClass.class);
125         final ConstantUtf8 name = cp.getConstantUtf8(cl.getNameIndex());
126         name.setBytes(Utility.packageToPath(className));
127         return clazz;
128     }
129 
130     @Override
131     protected Class<?> loadClass(final String className, final boolean resolve) throws ClassNotFoundException {
132         Class<?> cl = null;
133         /*
134          * First try: lookup hash table.
135          */
136         if ((cl = classes.get(className)) == null) {
137             /*
138              * Second try: Load system class using system class loader. You better don't mess around with them.
139              */
140             for (final String ignoredPackage : ignoredPackages) {
141                 if (className.startsWith(ignoredPackage)) {
142                     cl = getParent().loadClass(className);
143                     break;
144                 }
145             }
146             if (cl == null) {
147                 JavaClass clazz = null;
148                 /*
149                  * Third try: Special request?
150                  */
151                 if (className.contains(BCEL_TOKEN)) {
152                     clazz = createClass(className);
153                 } else { // Fourth try: Load classes via repository
154                     if ((clazz = repository.loadClass(className)) == null) {
155                         throw new ClassNotFoundException(className);
156                     }
157                     clazz = modifyClass(clazz);
158                 }
159                 if (clazz != null) {
160                     final byte[] bytes = clazz.getBytes();
161                     cl = defineClass(className, bytes, 0, bytes.length);
162                 } else {
163                     cl = Class.forName(className);
164                 }
165             }
166             if (resolve) {
167                 resolveClass(cl);
168             }
169         }
170         classes.put(className, cl);
171         return cl;
172     }
173 
174     /**
175      * Override this method if you want to alter a class before it gets actually loaded. Does nothing by default.
176      */
177     protected JavaClass modifyClass(final JavaClass clazz) {
178         return clazz;
179     }
180 }