001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.util;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.util.Hashtable;
024
025import org.apache.bcel.Const;
026import org.apache.bcel.classfile.ClassParser;
027import org.apache.bcel.classfile.ConstantClass;
028import org.apache.bcel.classfile.ConstantPool;
029import org.apache.bcel.classfile.ConstantUtf8;
030import org.apache.bcel.classfile.JavaClass;
031import org.apache.bcel.classfile.Utility;
032
033/**
034 * <p>
035 * Drop in replacement for the standard class loader of the JVM. You can use it in conjunction with the JavaWrapper to
036 * dynamically modify/create classes as they're requested.
037 * </p>
038 *
039 * <p>
040 * This class loader recognizes special requests in a distinct format, that is, when the name of the requested class
041 * contains with "$$BCEL$$" it calls the createClass() method with that name (everything bevor the $$BCEL$$ is
042 * considered to be the package name. You can subclass the class loader and override that method. "Normal" classes class
043 * can be modified by overriding the modifyClass() method which is called just before defineClass().
044 * </p>
045 *
046 * <p>
047 * There may be a number of packages where you have to use the default class loader (which may also be faster). You can
048 * define the set of packages where to use the system class loader in the constructor. The default value contains
049 * "java.", "sun.", "javax."
050 * </p>
051 *
052 * @see JavaWrapper
053 * @see ClassPath
054 * @deprecated 6.0 Do not use - does not work
055 */
056@Deprecated
057public class ClassLoader extends java.lang.ClassLoader {
058
059    private static final String BCEL_TOKEN = "$$BCEL$$";
060
061    /**
062     * Default packages that are ignored by the class loader.
063     */
064    public static final String[] DEFAULT_IGNORED_PACKAGES = {"java.", "javax.", "sun."};
065
066    private final Hashtable<String, Class<?>> classes = new Hashtable<>();
067    // Hashtable is synchronized thus thread-safe
068    private final String[] ignoredPackages;
069    private Repository repository = SyntheticRepository.getInstance();
070
071    /**
072     * Constructs a ClassLoader with default ignored packages.
073     * Ignored packages are by default ( "java.", "sun.", "javax."), for example loaded by system class loader.
074     */
075    public ClassLoader() {
076        this(DEFAULT_IGNORED_PACKAGES);
077    }
078
079    /**
080     * Constructs a ClassLoader with a delegate class loader.
081     *
082     * @param deferTo delegate class loader to use for ignored packages.
083     */
084    public ClassLoader(final java.lang.ClassLoader deferTo) {
085        super(deferTo);
086        this.ignoredPackages = DEFAULT_IGNORED_PACKAGES;
087        this.repository = new ClassLoaderRepository(deferTo);
088    }
089
090    /**
091     * Constructs a ClassLoader with a delegate class loader and ignored packages.
092     *
093     * @param deferTo delegate class loader to use for ignored packages.
094     * @param ignoredPackages classes contained in these packages will be loaded with the system class loader.
095     */
096    public ClassLoader(final java.lang.ClassLoader deferTo, final String[] ignoredPackages) {
097        this(ignoredPackages);
098        this.repository = new ClassLoaderRepository(deferTo);
099    }
100
101    /**
102     * Constructs a ClassLoader with specific ignored packages.
103     *
104     * @param ignoredPackages classes contained in these packages will be loaded with the system class loader.
105     */
106    public ClassLoader(final String[] ignoredPackages) {
107        this.ignoredPackages = ignoredPackages;
108    }
109
110    /**
111     * Override this method to create you own classes on the fly. The name contains the special token $$BCEL$$. Everything
112     * before that token is considered to be a package name. You can encode your own arguments into the subsequent string.
113     * You must ensure however not to use any "illegal" characters, that is, characters that may not appear in a Java class
114     * name too.
115     * <p>
116     * The default implementation interprets the string as a encoded compressed Java class, unpacks and decodes it with the
117     * Utility.decode() method, and parses the resulting byte array and returns the resulting JavaClass object.
118     * </p>
119     *
120     * @param className compressed byte code with "$$BCEL$$" in it.
121     * @return the created JavaClass.
122     */
123    protected JavaClass createClass(final String className) {
124        final int index = className.indexOf(BCEL_TOKEN);
125        final String realName = className.substring(index + BCEL_TOKEN.length());
126        JavaClass clazz = null;
127        try {
128            final byte[] bytes = Utility.decode(realName, true);
129            final ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");
130            clazz = parser.parse();
131        } catch (final IOException e) {
132            e.printStackTrace();
133            return null;
134        }
135        // Adapt the class name to the passed value
136        final ConstantPool cp = clazz.getConstantPool();
137        final ConstantClass cl = cp.getConstant(clazz.getClassNameIndex(), Const.CONSTANT_Class, ConstantClass.class);
138        final ConstantUtf8 name = cp.getConstantUtf8(cl.getNameIndex());
139        name.setBytes(Utility.packageToPath(className));
140        return clazz;
141    }
142
143    @Override
144    protected Class<?> loadClass(final String className, final boolean resolve) throws ClassNotFoundException {
145        Class<?> cl = null;
146        /*
147         * First try: lookup hash table.
148         */
149        if ((cl = classes.get(className)) == null) {
150            /*
151             * Second try: Load system class using system class loader. You better don't mess around with them.
152             */
153            for (final String ignoredPackage : ignoredPackages) {
154                if (className.startsWith(ignoredPackage)) {
155                    cl = getParent().loadClass(className);
156                    break;
157                }
158            }
159            if (cl == null) {
160                JavaClass clazz = null;
161                /*
162                 * Third try: Special request?
163                 */
164                if (className.contains(BCEL_TOKEN)) {
165                    clazz = createClass(className);
166                } else { // Fourth try: Load classes via repository
167                    if ((clazz = repository.loadClass(className)) == null) {
168                        throw new ClassNotFoundException(className);
169                    }
170                    clazz = modifyClass(clazz);
171                }
172                if (clazz != null) {
173                    final byte[] bytes = clazz.getBytes();
174                    cl = defineClass(className, bytes, 0, bytes.length);
175                } else {
176                    cl = Class.forName(className);
177                }
178            }
179            if (resolve) {
180                resolveClass(cl);
181            }
182        }
183        classes.put(className, cl);
184        return cl;
185    }
186
187    /**
188     * Override this method if you want to alter a class before it gets actually loaded. Does nothing by default.
189     *
190     * @param clazz the class to modify.
191     * @return the modified class.
192     */
193    protected JavaClass modifyClass(final JavaClass clazz) {
194        return clazz;
195    }
196}