Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/SecureLookupSupplier.java
41154 views
/*1* Copyright (c) 2016, 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.dynalink;2627import java.lang.invoke.MethodHandles;28import java.lang.invoke.MethodHandles.Lookup;29import java.util.Objects;3031/**32* Provides security-checked access to a {@code MethodHandles.Lookup} object.33* See {@link #getLookup()} for details.34*/35public class SecureLookupSupplier {36/**37* The name of a runtime permission required to successfully invoke the38* {@link #getLookup()} method.39*/40public static final String GET_LOOKUP_PERMISSION_NAME = "dynalink.getLookup";4142private static final RuntimePermission GET_LOOKUP_PERMISSION = new RuntimePermission(SecureLookupSupplier.GET_LOOKUP_PERMISSION_NAME);4344private final MethodHandles.Lookup lookup;4546/**47* Creates a new secure lookup supplier, securing the passed lookup.48* @param lookup the lookup to secure. Can not be null.49* @throws NullPointerException if null is passed.50*/51public SecureLookupSupplier(final MethodHandles.Lookup lookup) {52this.lookup = Objects.requireNonNull(lookup, "lookup");53}5455/**56* Returns the lookup secured by this {@code SecureLookupSupplier}.57* @return the lookup secured by this {@code SecureLookupSupplier}.58* @throws SecurityException if the secured lookup isn't the59* {@link MethodHandles#publicLookup()}, and a security manager is present,60* and a check for {@code RuntimePermission("dynalink.getLookup")} fails.61*/62public final Lookup getLookup() {63@SuppressWarnings("removal")64final SecurityManager sm = System.getSecurityManager();65if (sm != null && lookup != MethodHandles.publicLookup()) {66sm.checkPermission(GET_LOOKUP_PERMISSION);67}68return lookup;69}7071/**72* Returns the value of {@link #getLookup()} without a security check. Can73* be used by subclasses to access the lookup quickly.74* @return same as returned value of {@link #getLookup()}.75*/76protected final Lookup getLookupPrivileged() {77return lookup;78}79}808182