Path: blob/master/src/java.base/share/classes/jdk/internal/reflect/ClassDefiner.java
41159 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.reflect;2627import java.security.AccessController;28import java.security.PrivilegedAction;2930import jdk.internal.access.JavaLangAccess;31import jdk.internal.access.SharedSecrets;3233/** Utility class which assists in calling defineClass() by34creating a new class loader which delegates to the one needed in35order for proper resolution of the given bytecodes to occur. */3637class ClassDefiner {38static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();3940/** <P> We define generated code into a new class loader which41delegates to the defining loader of the target class. It is42necessary for the VM to be able to resolve references to the43target class from the generated bytecodes, which could not occur44if the generated code was loaded into the bootstrap class45loader. </P>4647<P> There are two primary reasons for creating a new loader48instead of defining these bytecodes directly into the defining49loader of the target class: first, it avoids any possible50security risk of having these bytecodes in the same loader.51Second, it allows the generated bytecodes to be unloaded earlier52than would otherwise be possible, decreasing run-time53footprint. </P>54*/55static Class<?> defineClass(String name, byte[] bytes, int off, int len,56final ClassLoader parentClassLoader)57{58@SuppressWarnings("removal")59ClassLoader newLoader = AccessController.doPrivileged(60new PrivilegedAction<ClassLoader>() {61public ClassLoader run() {62return new DelegatingClassLoader(parentClassLoader);63}64});65return JLA.defineClass(newLoader, name, bytes, null, "__ClassDefiner__");66}67}686970// NOTE: this class's name and presence are known to the virtual71// machine as of the fix for 4474172.72class DelegatingClassLoader extends ClassLoader {73DelegatingClassLoader(ClassLoader parent) {74super(parent);75}76}777879