0N/A/*
0N/A * Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage sun.reflect;
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport sun.misc.Unsafe;
0N/A
0N/A/** Utility class which assists in calling Unsafe.defineClass() by
0N/A creating a new class loader which delegates to the one needed in
0N/A order for proper resolution of the given bytecodes to occur. */
0N/A
0N/Aclass ClassDefiner {
0N/A static final Unsafe unsafe = Unsafe.getUnsafe();
0N/A
0N/A /** <P> We define generated code into a new class loader which
0N/A delegates to the defining loader of the target class. It is
0N/A necessary for the VM to be able to resolve references to the
0N/A target class from the generated bytecodes, which could not occur
0N/A if the generated code was loaded into the bootstrap class
0N/A loader. </P>
0N/A
0N/A <P> There are two primary reasons for creating a new loader
0N/A instead of defining these bytecodes directly into the defining
0N/A loader of the target class: first, it avoids any possible
0N/A security risk of having these bytecodes in the same loader.
0N/A Second, it allows the generated bytecodes to be unloaded earlier
0N/A than would otherwise be possible, decreasing run-time
0N/A footprint. </P>
0N/A */
0N/A static Class defineClass(String name, byte[] bytes, int off, int len,
0N/A final ClassLoader parentClassLoader)
0N/A {
0N/A ClassLoader newLoader = AccessController.doPrivileged(
0N/A new PrivilegedAction<ClassLoader>() {
0N/A public ClassLoader run() {
0N/A return new DelegatingClassLoader(parentClassLoader);
0N/A }
0N/A });
0N/A return unsafe.defineClass(name, bytes, off, len, newLoader, null);
0N/A }
0N/A}
0N/A
0N/A
0N/A// NOTE: this class's name and presence are known to the virtual
0N/A// machine as of the fix for 4474172.
0N/Aclass DelegatingClassLoader extends ClassLoader {
0N/A DelegatingClassLoader(ClassLoader parent) {
0N/A super(parent);
0N/A }
0N/A}
0N/A