--- /dev/null 2016-04-05 09:31:53.373418148 +0200 +++ new/src/java.base/share/classes/java/lang/reflect/ClassLoaderValue.java 2016-04-08 10:43:28.146663184 +0200 @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package java.lang.reflect; + +import java.util.Objects; +import java.util.function.BiFunction; + +/** + * root-ClassLoaderValue. Each instance defines a separate namespace for + * associated values. + *
+ * ClassLoaderValue allows associating a + * {@link #computeIfAbsent(ClassLoader, BiFunction) computed} non-null value with + * a {@code (ClassLoader, keys...)} tuple. The associated value, as well as the + * keys are strongly reachable from the associated ClassLoader so care should be + * taken to use such keys and values that only reference types resolvable from + * the associated ClassLoader. Failing that, ClassLoader leaks are inevitable. + *
+ * Example usage: + *
{@code + * // create a root instance which represents a namespace and declares the type of + * // associated values (Class instances in this example) + * static final ClassLoaderValue+ *> proxyClasses = new ClassLoaderValue<>(); + * + * // create a compound key composed of a Module and a list of interfaces + * Module module = ...; + * List > interfaces = ...; + * ClassLoaderValue >.Sub .Sub >> key = + * proxyClasses.sub(module).sub(interfaces); + * + * // use the compound key together with ClassLoader to lazily associate + * // the value with tuple (loader, module, interfaces) and return it + * ClassLoader loader = ...; + * Class> proxyClass = key.computeIfAbsent(loader, (ld, ky) -> { + * List
> intfcs = ky.key(); + * Module m = ky.parent().key(); + * Class> clazz = defineProxyClass(ld, m, intfcs); + * return clazz; + * }); + * }
+ * {@code classLoaderValue.