jdk/src/share/classes/java/lang/ClassValue.java
author jrose
Tue, 17 May 2011 19:48:19 -0700
changeset 9731 d0f7a3e441c4
parent 8822 8145ab9f5f86
child 9752 88ab34b6da6d
permissions -rw-r--r--
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one Summary: point-fixes for 7038847, 7038860, 7042656, 7042829, 7041853, and several other reports Reviewed-by: never, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     1
/*
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     4
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    10
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    15
 * accompanied this code).
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    16
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    20
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    23
 * questions.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    24
 */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package java.lang;
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    27
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    28
import java.util.WeakHashMap;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicInteger;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    30
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    31
/**
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    32
 * Lazily associate a computed value with (potentially) every type.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    33
 * For example, if a dynamic language needs to construct a message dispatch
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    34
 * table for each class encountered at a message send call site,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    35
 * it can use a {@code ClassValue} to cache information needed to
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    36
 * perform the message send quickly, for each class encountered.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    37
 * @author John Rose, JSR 292 EG
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    38
 * @since 1.7
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    39
 */
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    40
public abstract class ClassValue<T> {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    41
    /**
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    42
     * Computes the given class's derived value for this {@code ClassValue}.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    43
     * <p>
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    44
     * This method will be invoked within the first thread that accesses
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    45
     * the value with the {@link #get get} method.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    46
     * <p>
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    47
     * Normally, this method is invoked at most once per class,
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    48
     * but it may be invoked again if there has been a call to
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    49
     * {@link #remove remove}.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    50
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    51
     * If this method throws an exception, the corresponding call to {@code get}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    52
     * will terminate abnormally with that exception, and no class value will be recorded.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    53
     *
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    54
     * @param type the type whose class value must be computed
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    55
     * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    56
     * @see #get
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    57
     * @see #remove
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    58
     */
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    59
    protected abstract T computeValue(Class<?> type);
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    60
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    61
    /**
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    62
     * Returns the value for the given class.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    63
     * If no value has yet been computed, it is obtained by
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    64
     * an invocation of the {@link #computeValue computeValue} method.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    65
     * <p>
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    66
     * The actual installation of the value on the class
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    67
     * is performed atomically.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    68
     * At that point, if several racing threads have
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    69
     * computed values, one is chosen, and returned to
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    70
     * all the racing threads.
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    71
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    72
     * The {@code type} parameter is typically a class, but it may be any type,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    73
     * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    74
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    75
     * In the absence of {@code remove} calls, a class value has a simple
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    76
     * state diagram:  uninitialized and initialized.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    77
     * When {@code remove} calls are made,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    78
     * the rules for value observation are more complex.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    79
     * See the documentation for {@link #remove remove} for more information.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    80
     *
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    81
     * @param type the type whose class value must be computed or retrieved
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    82
     * @return the current value associated with this {@code ClassValue}, for the given class or interface
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    83
     * @throws NullPointerException if the argument is null
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    84
     * @see #remove
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
    85
     * @see #computeValue
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    86
     */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    87
    public T get(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    88
        ClassValueMap map = getMap(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    89
        if (map != null) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    90
            Object x = map.get(this);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    91
            if (x != null) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    92
                return (T) map.unmaskNull(x);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    93
            }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    94
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    95
        return setComputedValue(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    96
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    97
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    98
    /**
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    99
     * Removes the associated value for the given class.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   100
     * If this value is subsequently {@linkplain #get read} for the same class,
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
   101
     * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   102
     * This may result in an additional invocation of the
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
   103
     * {@code computeValue computeValue} method for the given class.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
   104
     * <p>
8346
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   105
     * In order to explain the interaction between {@code get} and {@code remove} calls,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   106
     * we must model the state transitions of a class value to take into account
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   107
     * the alternation between uninitialized and initialized states.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   108
     * To do this, number these states sequentially from zero, and note that
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   109
     * uninitialized (or removed) states are numbered with even numbers,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   110
     * while initialized (or re-initialized) states have odd numbers.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   111
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   112
     * When a thread {@code T} removes a class value in state {@code 2N},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   113
     * nothing happens, since the class value is already uninitialized.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   114
     * Otherwise, the state is advanced atomically to {@code 2N+1}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   115
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   116
     * When a thread {@code T} queries a class value in state {@code 2N},
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   117
     * the thread first attempts to initialize the class value to state {@code 2N+1}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   118
     * by invoking {@code computeValue} and installing the resulting value.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   119
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   120
     * When {@code T} attempts to install the newly computed value,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   121
     * if the state is still at {@code 2N}, the class value will be initialized
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   122
     * with the computed value, advancing it to state {@code 2N+1}.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   123
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   124
     * Otherwise, whether the new state is even or odd,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   125
     * {@code T} will discard the newly computed value
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   126
     * and retry the {@code get} operation.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   127
     * <p>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   128
     * Discarding and retrying is an important proviso,
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   129
     * since otherwise {@code T} could potentially install
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   130
     * a disastrously stale value.  For example:
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   131
     * <ul>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   132
     * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   133
     * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   134
     * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   135
     * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   136
     * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   137
     * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   138
     * <li> the previous actions of {@code T2} are repeated several times
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   139
     * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   140
     * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   141
     * </ul>
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   142
     * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   143
     * observe the time-dependent states as it computes {@code V1}, etc.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   144
     * This does not remove the threat of a stale value, since there is a window of time
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   145
     * between the return of {@code computeValue} in {@code T} and the installation
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   146
     * of the the new value.  No user synchronization is possible during this time.
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   147
     *
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   148
     * @param type the type whose class value must be removed
3b891698c4ec 7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents: 7562
diff changeset
   149
     * @throws NullPointerException if the argument is null
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   150
     */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   151
    public void remove(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   152
        ClassValueMap map = getMap(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   153
        if (map != null) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   154
            synchronized (map) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   155
                map.remove(this);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   156
            }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   157
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   158
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   159
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   160
    /// Implementation...
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
   161
    // FIXME: Use a data structure here similar that of ThreadLocal (7030453).
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   162
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   163
    private static final AtomicInteger STORE_BARRIER = new AtomicInteger();
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   164
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   165
    /** Slow path for {@link #get}. */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   166
    private T setComputedValue(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   167
        ClassValueMap map = getMap(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   168
        if (map == null) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   169
            map = initializeMap(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   170
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   171
        T value = computeValue(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   172
        STORE_BARRIER.lazySet(0);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   173
        // All stores pending from computeValue are completed.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   174
        synchronized (map) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   175
            // Warm up the table with a null entry.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   176
            map.preInitializeEntry(this);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   177
        }
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
   178
        STORE_BARRIER.lazySet(0);
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   179
        // All stores pending from table expansion are completed.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   180
        synchronized (map) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   181
            value = (T) map.initializeEntry(this, value);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   182
            // One might fear a possible race condition here
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   183
            // if the code for map.put has flushed the write
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   184
            // to map.table[*] before the writes to the Map.Entry
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   185
            // are done.  This is not possible, since we have
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   186
            // warmed up the table with an empty entry.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   187
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   188
        return value;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   189
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   190
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   191
    // Replace this map by a per-class slot.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   192
    private static final WeakHashMap<Class<?>, ClassValueMap> ROOT
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   193
        = new WeakHashMap<Class<?>, ClassValueMap>();
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   194
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   195
    private static ClassValueMap getMap(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   196
        return ROOT.get(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   197
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   198
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   199
    private static ClassValueMap initializeMap(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   200
        synchronized (ClassValue.class) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   201
            ClassValueMap map = ROOT.get(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   202
            if (map == null)
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   203
                ROOT.put(type, map = new ClassValueMap());
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   204
            return map;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   205
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   206
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   207
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   208
    static class ClassValueMap extends WeakHashMap<ClassValue, Object> {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   209
        /** Make sure this table contains an Entry for the given key, even if it is empty. */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   210
        void preInitializeEntry(ClassValue key) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   211
            if (!this.containsKey(key))
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   212
                this.put(key, null);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   213
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   214
        /** Make sure this table contains a non-empty Entry for the given key. */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   215
        Object initializeEntry(ClassValue key, Object value) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   216
            Object prior = this.get(key);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   217
            if (prior != null) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   218
                return unmaskNull(prior);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   219
            }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   220
            this.put(key, maskNull(value));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   221
            return value;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   222
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   223
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   224
        Object maskNull(Object x) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   225
            return x == null ? this : x;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   226
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   227
        Object unmaskNull(Object x) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   228
            return x == this ? null : x;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   229
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   230
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   231
}