001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.util;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.util.Hashtable;
022
023import org.apache.bcel.Const;
024import org.apache.bcel.classfile.ClassParser;
025import org.apache.bcel.classfile.ConstantClass;
026import org.apache.bcel.classfile.ConstantPool;
027import org.apache.bcel.classfile.ConstantUtf8;
028import org.apache.bcel.classfile.JavaClass;
029import org.apache.bcel.classfile.Utility;
030
031/**
032 * <p>
033 * Drop in replacement for the standard class loader of the JVM. You can use it in conjunction with the JavaWrapper to
034 * dynamically modify/create classes as they're requested.
035 * </p>
036 *
037 * <p>
038 * This class loader recognizes special requests in a distinct format, i.e., when the name of the requested class
039 * contains with "$$BCEL$$" it calls the createClass() method with that name (everything bevor the $$BCEL$$ is
040 * considered to be the package name. You can subclass the class loader and override that method. "Normal" classes class
041 * can be modified by overriding the modifyClass() method which is called just before defineClass().
042 * </p>
043 *
044 * <p>
045 * There may be a number of packages where you have to use the default class loader (which may also be faster). You can
046 * define the set of packages where to use the system class loader in the constructor. The default value contains
047 * "java.", "sun.", "javax."
048 * </p>
049 *
050 * @see JavaWrapper
051 * @see ClassPath
052 * @deprecated 6.0 Do not use - does not work
053 */
054@Deprecated
055public class ClassLoader extends java.lang.ClassLoader {
056
057    private static final String BCEL_TOKEN = "$$BCEL$$";
058
059    public static final String[] DEFAULT_IGNORED_PACKAGES = {"java.", "javax.", "sun."};
060
061    private final Hashtable<String, Class<?>> classes = new Hashtable<>();
062    // Hashtable is synchronized thus thread-safe
063    private final String[] ignoredPackages;
064    private Repository repository = SyntheticRepository.getInstance();
065
066    /**
067     * Ignored packages are by default ( "java.", "sun.", "javax."), i.e. loaded by system class loader
068     */
069    public ClassLoader() {
070        this(DEFAULT_IGNORED_PACKAGES);
071    }
072
073    /**
074     * @param deferTo delegate class loader to use for ignored packages
075     */
076    public ClassLoader(final java.lang.ClassLoader deferTo) {
077        super(deferTo);
078        this.ignoredPackages = DEFAULT_IGNORED_PACKAGES;
079        this.repository = new ClassLoaderRepository(deferTo);
080    }
081
082    /**
083     * @param ignoredPackages classes contained in these packages will be loaded with the system class loader
084     * @param deferTo delegate class loader to use for ignored packages
085     */
086    public ClassLoader(final java.lang.ClassLoader deferTo, final String[] ignoredPackages) {
087        this(ignoredPackages);
088        this.repository = new ClassLoaderRepository(deferTo);
089    }
090
091    /**
092     * @param ignoredPackages classes contained in these packages will be loaded with the system class loader
093     */
094    public ClassLoader(final String[] ignoredPackages) {
095        this.ignoredPackages = ignoredPackages;
096    }
097
098    /**
099     * 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}