Path: blob/master/src/java.base/share/classes/jdk/internal/loader/ClassLoaderValue.java
41159 views
/*1* Copyright (c) 2016, 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.loader;2627import java.util.Objects;28import java.util.function.BiFunction;2930/**31* root-ClassLoaderValue. Each instance defines a separate namespace for32* associated values.33* <p>34* ClassLoaderValue allows associating a35* {@link #computeIfAbsent(ClassLoader, BiFunction) computed} non-null value with36* a {@code (ClassLoader, keys...)} tuple. The associated value, as well as the37* keys are strongly reachable from the associated ClassLoader so care should be38* taken to use such keys and values that only reference types resolvable from39* the associated ClassLoader. Failing that, ClassLoader leaks are inevitable.40* <p>41* Example usage:42* <pre>{@code43* // create a root instance which represents a namespace and declares the type of44* // associated values (Class instances in this example)45* static final ClassLoaderValue<Class<?>> proxyClasses = new ClassLoaderValue<>();46*47* // create a compound key composed of a Module and a list of interfaces48* Module module = ...;49* List<Class<?>> interfaces = ...;50* ClassLoaderValue<Class<?>>.Sub<Module>.Sub<List<Class<?>>> key =51* proxyClasses.sub(module).sub(interfaces);52*53* // use the compound key together with ClassLoader to lazily associate54* // the value with tuple (loader, module, interfaces) and return it55* ClassLoader loader = ...;56* Class<?> proxyClass = key.computeIfAbsent(loader, (ld, ky) -> {57* List<Class<?>> intfcs = ky.key();58* Module m = ky.parent().key();59* Class<?> clazz = defineProxyClass(ld, m, intfcs);60* return clazz;61* });62* }</pre>63* <p>64* {@code classLoaderValue.<operation>(classLoader, ...)} represents an operation65* to {@link #get}, {@link #putIfAbsent}, {@link #computeIfAbsent} or {@link #remove}66* a value associated with a (classLoader, classLoaderValue) tuple. ClassLoader67* instances and root-{@link ClassLoaderValue} instances are compared using68* identity equality while {@link Sub sub}-ClassLoaderValue instances define69* {@link #equals(Object) equality} in terms of equality of its70* {@link Sub#parent() parent} ClassLoaderValue and its71* {@link #key() key} component.72*73* @param <V> the type of value(s) associated with the root-ClassLoaderValue and74* all its {@link #sub(Object) descendants}.75* @author Peter Levart76* @since 977*/78public final class ClassLoaderValue<V>79extends AbstractClassLoaderValue<ClassLoaderValue<V>, V> {8081/**82* Constructs new root-ClassLoaderValue representing its own namespace.83*/84public ClassLoaderValue() {}8586/**87* @return the key component of this root-ClassLoaderValue (itself).88*/89@Override90public ClassLoaderValue<V> key() {91return this;92}9394/**95* root-ClassLoaderValue can only be equal to itself and has no predecessors.96*/97@Override98public boolean isEqualOrDescendantOf(AbstractClassLoaderValue<?, V> clv) {99return equals(Objects.requireNonNull(clv));100}101}102103104