Implementation of ClassValue for bck2brwsr default tip
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985cd1cc103a03c
parent 1984 87bb69774b64
Implementation of ClassValue for bck2brwsr
rt/emul/compact/src/main/java/java/lang/ClassValue.java
rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionClassValueTest.java
rt/emul/mini/src/main/java/java/lang/ClassValue.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/ClassValue.java	Tue Jan 17 07:04:06 2017 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +import java.lang.Class;
    1.32 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.33 +
    1.34 +/**
    1.35 + * Lazily associate a computed value with (potentially) every type.
    1.36 + * For example, if a dynamic language needs to construct a message dispatch
    1.37 + * table for each class encountered at a message send call site,
    1.38 + * it can use a {@code ClassValue} to cache information needed to
    1.39 + * perform the message send quickly, for each class encountered.
    1.40 + * @author John Rose, JSR 292 EG
    1.41 + * @since 1.7
    1.42 + */
    1.43 +public abstract class ClassValue<T> {
    1.44 +    /**
    1.45 +     * Sole constructor.  (For invocation by subclass constructors, typically
    1.46 +     * implicit.)
    1.47 +     */
    1.48 +    protected ClassValue() {
    1.49 +    }
    1.50 +
    1.51 +    /**
    1.52 +     * Computes the given class's derived value for this {@code ClassValue}.
    1.53 +     * <p>
    1.54 +     * This method will be invoked within the first thread that accesses
    1.55 +     * the value with the {@link #get get} method.
    1.56 +     * <p>
    1.57 +     * Normally, this method is invoked at most once per class,
    1.58 +     * but it may be invoked again if there has been a call to
    1.59 +     * {@link #remove remove}.
    1.60 +     * <p>
    1.61 +     * If this method throws an exception, the corresponding call to {@code get}
    1.62 +     * will terminate abnormally with that exception, and no class value will be recorded.
    1.63 +     *
    1.64 +     * @param type the type whose class value must be computed
    1.65 +     * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface
    1.66 +     * @see #get
    1.67 +     * @see #remove
    1.68 +     */
    1.69 +    protected abstract T computeValue(Class<?> type);
    1.70 +
    1.71 +    /**
    1.72 +     * Returns the value for the given class.
    1.73 +     * If no value has yet been computed, it is obtained by
    1.74 +     * an invocation of the {@link #computeValue computeValue} method.
    1.75 +     * <p>
    1.76 +     * The actual installation of the value on the class
    1.77 +     * is performed atomically.
    1.78 +     * At that point, if several racing threads have
    1.79 +     * computed values, one is chosen, and returned to
    1.80 +     * all the racing threads.
    1.81 +     * <p>
    1.82 +     * The {@code type} parameter is typically a class, but it may be any type,
    1.83 +     * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
    1.84 +     * <p>
    1.85 +     * In the absence of {@code remove} calls, a class value has a simple
    1.86 +     * state diagram:  uninitialized and initialized.
    1.87 +     * When {@code remove} calls are made,
    1.88 +     * the rules for value observation are more complex.
    1.89 +     * See the documentation for {@link #remove remove} for more information.
    1.90 +     *
    1.91 +     * @param type the type whose class value must be computed or retrieved
    1.92 +     * @return the current value associated with this {@code ClassValue}, for the given class or interface
    1.93 +     * @throws NullPointerException if the argument is null
    1.94 +     * @see #remove
    1.95 +     * @see #computeValue
    1.96 +     */
    1.97 +    public T get(Class<?> type) {
    1.98 +        T value = access(type, id, false, null);
    1.99 +        if (value == undefined()) {
   1.100 +            value = access(type, id, true, computeValue(type));
   1.101 +        }
   1.102 +        return value;
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Removes the associated value for the given class.
   1.107 +     * If this value is subsequently {@linkplain #get read} for the same class,
   1.108 +     * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
   1.109 +     * This may result in an additional invocation of the
   1.110 +     * {@code computeValue} method for the given class.
   1.111 +     * <p>
   1.112 +     * In order to explain the interaction between {@code get} and {@code remove} calls,
   1.113 +     * we must model the state transitions of a class value to take into account
   1.114 +     * the alternation between uninitialized and initialized states.
   1.115 +     * To do this, number these states sequentially from zero, and note that
   1.116 +     * uninitialized (or removed) states are numbered with even numbers,
   1.117 +     * while initialized (or re-initialized) states have odd numbers.
   1.118 +     * <p>
   1.119 +     * When a thread {@code T} removes a class value in state {@code 2N},
   1.120 +     * nothing happens, since the class value is already uninitialized.
   1.121 +     * Otherwise, the state is advanced atomically to {@code 2N+1}.
   1.122 +     * <p>
   1.123 +     * When a thread {@code T} queries a class value in state {@code 2N},
   1.124 +     * the thread first attempts to initialize the class value to state {@code 2N+1}
   1.125 +     * by invoking {@code computeValue} and installing the resulting value.
   1.126 +     * <p>
   1.127 +     * When {@code T} attempts to install the newly computed value,
   1.128 +     * if the state is still at {@code 2N}, the class value will be initialized
   1.129 +     * with the computed value, advancing it to state {@code 2N+1}.
   1.130 +     * <p>
   1.131 +     * Otherwise, whether the new state is even or odd,
   1.132 +     * {@code T} will discard the newly computed value
   1.133 +     * and retry the {@code get} operation.
   1.134 +     * <p>
   1.135 +     * Discarding and retrying is an important proviso,
   1.136 +     * since otherwise {@code T} could potentially install
   1.137 +     * a disastrously stale value.  For example:
   1.138 +     * <ul>
   1.139 +     * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
   1.140 +     * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
   1.141 +     * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
   1.142 +     * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
   1.143 +     * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
   1.144 +     * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
   1.145 +     * <li> the previous actions of {@code T2} are repeated several times
   1.146 +     * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
   1.147 +     * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
   1.148 +     * </ul>
   1.149 +     * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
   1.150 +     * observe the time-dependent states as it computes {@code V1}, etc.
   1.151 +     * This does not remove the threat of a stale value, since there is a window of time
   1.152 +     * between the return of {@code computeValue} in {@code T} and the installation
   1.153 +     * of the the new value.  No user synchronization is possible during this time.
   1.154 +     *
   1.155 +     * @param type the type whose class value must be removed
   1.156 +     * @throws NullPointerException if the argument is null
   1.157 +     */
   1.158 +    public void remove(Class<?> type) {
   1.159 +        access(type, id, true, undefined());
   1.160 +    }
   1.161 +
   1.162 +    /// Implementation...
   1.163 +
   1.164 +    private static int COUNTER = 0;
   1.165 +    private final int id = COUNTER++;
   1.166 +
   1.167 +    @JavaScriptBody(args = {}, body = "return undefined;")
   1.168 +    private static native Object undefined();
   1.169 +
   1.170 +    @JavaScriptBody(args = { "where", "index", "set", "newValue" }, body =
   1.171 +        "var data = where['values'];\n" +
   1.172 +        "if (!data) {\n" +
   1.173 +        "  data = where['values'] = [];\n" +
   1.174 +        "}\n" +
   1.175 +        "if (set) {\n" +
   1.176 +        "  data[index] = newValue;\n" +
   1.177 +        "}\n" +
   1.178 +        "return data[index];\n" +
   1.179 +        ""
   1.180 +    )
   1.181 +    private static native <T> T access(Class<?> where, int index, boolean set, T newValue);
   1.182 +}
     2.1 --- a/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionClassValueTest.java	Tue Jan 17 06:16:06 2017 +0100
     2.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionClassValueTest.java	Tue Jan 17 07:04:06 2017 +0100
     2.3 @@ -17,6 +17,8 @@
     2.4   */
     2.5  package org.apidesign.bck2brwsr.tck;
     2.6  
     2.7 +import java.io.Serializable;
     2.8 +import java.lang.ClassValue;
     2.9  import org.apidesign.bck2brwsr.vmtest.Compare;
    2.10  import org.apidesign.bck2brwsr.vmtest.VMTest;
    2.11  import org.testng.annotations.Factory;
    2.12 @@ -56,13 +58,6 @@
    2.13          return one == two;
    2.14      }
    2.15  
    2.16 -    @Compare public boolean valueCanBeCleared() {
    2.17 -        String one = LOWER.get(Runnable.class);
    2.18 -        LOWER.remove(Runnable.class);
    2.19 -        String two = LOWER.get(Runnable.class);
    2.20 -        return one != two;
    2.21 -    }
    2.22 -
    2.23      @Compare public String upperObject() {
    2.24          return UPPER.get(Object.class);
    2.25      }
    2.26 @@ -79,6 +74,47 @@
    2.27          return LOWER.get(CharSequence.class) + UPPER.get(CharSequence.class);
    2.28      }
    2.29  
    2.30 +    private static final class CountingNull extends ClassValue<Object> {
    2.31 +        int cnt;
    2.32 +
    2.33 +        @Override
    2.34 +        protected Object computeValue(Class<?> type) {
    2.35 +            cnt++;
    2.36 +            return null;
    2.37 +        }
    2.38 +    }
    2.39 +
    2.40 +    @Compare public int getNullThreeTimes() {
    2.41 +        CountingNull counter = new CountingNull();
    2.42 +        Object o1 = counter.get(Serializable.class);
    2.43 +        Object o2 = counter.get(Serializable.class);
    2.44 +        Object o3 = counter.get(Serializable.class);
    2.45 +        assert o1 == null;
    2.46 +        assert o2 == null;
    2.47 +        assert o3 == null;
    2.48 +        return counter.cnt;
    2.49 +    }
    2.50 +
    2.51 +    private static final class NewObj extends ClassValue<Integer> {
    2.52 +        int cnt;
    2.53 +
    2.54 +        @Override
    2.55 +        protected Integer computeValue(Class<?> type) {
    2.56 +            cnt++;
    2.57 +            return new Integer(cnt);
    2.58 +        }
    2.59 +    }
    2.60 +
    2.61 +    @Compare public boolean valueCanBeCleared() {
    2.62 +        NewObj cache = new NewObj();
    2.63 +        Integer one = cache.get(Runnable.class);
    2.64 +        Integer two = cache.get(Runnable.class);
    2.65 +        assert one == two;
    2.66 +        cache.remove(Runnable.class);
    2.67 +        Integer three = cache.get(Runnable.class);
    2.68 +        return one != three;
    2.69 +    }
    2.70 +
    2.71      @Factory
    2.72      public static Object[] create() {
    2.73          return VMTest.create(ReflectionClassValueTest.class);
     3.1 --- a/rt/emul/mini/src/main/java/java/lang/ClassValue.java	Tue Jan 17 06:16:06 2017 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,239 +0,0 @@
     3.4 -/*
     3.5 - * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
     3.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 - *
     3.8 - * This code is free software; you can redistribute it and/or modify it
     3.9 - * under the terms of the GNU General Public License version 2 only, as
    3.10 - * published by the Free Software Foundation.  Oracle designates this
    3.11 - * particular file as subject to the "Classpath" exception as provided
    3.12 - * by Oracle in the LICENSE file that accompanied this code.
    3.13 - *
    3.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    3.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.17 - * version 2 for more details (a copy is included in the LICENSE file that
    3.18 - * accompanied this code).
    3.19 - *
    3.20 - * You should have received a copy of the GNU General Public License version
    3.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    3.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.23 - *
    3.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.25 - * or visit www.oracle.com if you need additional information or have any
    3.26 - * questions.
    3.27 - */
    3.28 -
    3.29 -package java.lang;
    3.30 -
    3.31 -import java.util.WeakHashMap;
    3.32 -import java.util.concurrent.atomic.AtomicInteger;
    3.33 -
    3.34 -/**
    3.35 - * Lazily associate a computed value with (potentially) every type.
    3.36 - * For example, if a dynamic language needs to construct a message dispatch
    3.37 - * table for each class encountered at a message send call site,
    3.38 - * it can use a {@code ClassValue} to cache information needed to
    3.39 - * perform the message send quickly, for each class encountered.
    3.40 - * @author John Rose, JSR 292 EG
    3.41 - * @since 1.7
    3.42 - */
    3.43 -public abstract class ClassValue<T> {
    3.44 -    /**
    3.45 -     * Sole constructor.  (For invocation by subclass constructors, typically
    3.46 -     * implicit.)
    3.47 -     */
    3.48 -    protected ClassValue() {
    3.49 -    }
    3.50 -
    3.51 -    /**
    3.52 -     * Computes the given class's derived value for this {@code ClassValue}.
    3.53 -     * <p>
    3.54 -     * This method will be invoked within the first thread that accesses
    3.55 -     * the value with the {@link #get get} method.
    3.56 -     * <p>
    3.57 -     * Normally, this method is invoked at most once per class,
    3.58 -     * but it may be invoked again if there has been a call to
    3.59 -     * {@link #remove remove}.
    3.60 -     * <p>
    3.61 -     * If this method throws an exception, the corresponding call to {@code get}
    3.62 -     * will terminate abnormally with that exception, and no class value will be recorded.
    3.63 -     *
    3.64 -     * @param type the type whose class value must be computed
    3.65 -     * @return the newly computed value associated with this {@code ClassValue}, for the given class or interface
    3.66 -     * @see #get
    3.67 -     * @see #remove
    3.68 -     */
    3.69 -    protected abstract T computeValue(Class<?> type);
    3.70 -
    3.71 -    /**
    3.72 -     * Returns the value for the given class.
    3.73 -     * If no value has yet been computed, it is obtained by
    3.74 -     * an invocation of the {@link #computeValue computeValue} method.
    3.75 -     * <p>
    3.76 -     * The actual installation of the value on the class
    3.77 -     * is performed atomically.
    3.78 -     * At that point, if several racing threads have
    3.79 -     * computed values, one is chosen, and returned to
    3.80 -     * all the racing threads.
    3.81 -     * <p>
    3.82 -     * The {@code type} parameter is typically a class, but it may be any type,
    3.83 -     * such as an interface, a primitive type (like {@code int.class}), or {@code void.class}.
    3.84 -     * <p>
    3.85 -     * In the absence of {@code remove} calls, a class value has a simple
    3.86 -     * state diagram:  uninitialized and initialized.
    3.87 -     * When {@code remove} calls are made,
    3.88 -     * the rules for value observation are more complex.
    3.89 -     * See the documentation for {@link #remove remove} for more information.
    3.90 -     *
    3.91 -     * @param type the type whose class value must be computed or retrieved
    3.92 -     * @return the current value associated with this {@code ClassValue}, for the given class or interface
    3.93 -     * @throws NullPointerException if the argument is null
    3.94 -     * @see #remove
    3.95 -     * @see #computeValue
    3.96 -     */
    3.97 -    public T get(Class<?> type) {
    3.98 -        ClassValueMap map = getMap(type);
    3.99 -        if (map != null) {
   3.100 -            Object x = map.get(this);
   3.101 -            if (x != null) {
   3.102 -                return (T) map.unmaskNull(x);
   3.103 -            }
   3.104 -        }
   3.105 -        return setComputedValue(type);
   3.106 -    }
   3.107 -
   3.108 -    /**
   3.109 -     * Removes the associated value for the given class.
   3.110 -     * If this value is subsequently {@linkplain #get read} for the same class,
   3.111 -     * its value will be reinitialized by invoking its {@link #computeValue computeValue} method.
   3.112 -     * This may result in an additional invocation of the
   3.113 -     * {@code computeValue} method for the given class.
   3.114 -     * <p>
   3.115 -     * In order to explain the interaction between {@code get} and {@code remove} calls,
   3.116 -     * we must model the state transitions of a class value to take into account
   3.117 -     * the alternation between uninitialized and initialized states.
   3.118 -     * To do this, number these states sequentially from zero, and note that
   3.119 -     * uninitialized (or removed) states are numbered with even numbers,
   3.120 -     * while initialized (or re-initialized) states have odd numbers.
   3.121 -     * <p>
   3.122 -     * When a thread {@code T} removes a class value in state {@code 2N},
   3.123 -     * nothing happens, since the class value is already uninitialized.
   3.124 -     * Otherwise, the state is advanced atomically to {@code 2N+1}.
   3.125 -     * <p>
   3.126 -     * When a thread {@code T} queries a class value in state {@code 2N},
   3.127 -     * the thread first attempts to initialize the class value to state {@code 2N+1}
   3.128 -     * by invoking {@code computeValue} and installing the resulting value.
   3.129 -     * <p>
   3.130 -     * When {@code T} attempts to install the newly computed value,
   3.131 -     * if the state is still at {@code 2N}, the class value will be initialized
   3.132 -     * with the computed value, advancing it to state {@code 2N+1}.
   3.133 -     * <p>
   3.134 -     * Otherwise, whether the new state is even or odd,
   3.135 -     * {@code T} will discard the newly computed value
   3.136 -     * and retry the {@code get} operation.
   3.137 -     * <p>
   3.138 -     * Discarding and retrying is an important proviso,
   3.139 -     * since otherwise {@code T} could potentially install
   3.140 -     * a disastrously stale value.  For example:
   3.141 -     * <ul>
   3.142 -     * <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N}
   3.143 -     * <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it
   3.144 -     * <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time
   3.145 -     * <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N}
   3.146 -     * <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)}
   3.147 -     * <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work
   3.148 -     * <li> the previous actions of {@code T2} are repeated several times
   3.149 -     * <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ...
   3.150 -     * <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em>
   3.151 -     * </ul>
   3.152 -     * We can assume in the above scenario that {@code CV.computeValue} uses locks to properly
   3.153 -     * observe the time-dependent states as it computes {@code V1}, etc.
   3.154 -     * This does not remove the threat of a stale value, since there is a window of time
   3.155 -     * between the return of {@code computeValue} in {@code T} and the installation
   3.156 -     * of the the new value.  No user synchronization is possible during this time.
   3.157 -     *
   3.158 -     * @param type the type whose class value must be removed
   3.159 -     * @throws NullPointerException if the argument is null
   3.160 -     */
   3.161 -    public void remove(Class<?> type) {
   3.162 -        ClassValueMap map = getMap(type);
   3.163 -        if (map != null) {
   3.164 -            synchronized (map) {
   3.165 -                map.remove(this);
   3.166 -            }
   3.167 -        }
   3.168 -    }
   3.169 -
   3.170 -    /// Implementation...
   3.171 -    // FIXME: Use a data structure here similar that of ThreadLocal (7030453).
   3.172 -
   3.173 -    private static final AtomicInteger STORE_BARRIER = new AtomicInteger();
   3.174 -
   3.175 -    /** Slow path for {@link #get}. */
   3.176 -    private T setComputedValue(Class<?> type) {
   3.177 -        ClassValueMap map = getMap(type);
   3.178 -        if (map == null) {
   3.179 -            map = initializeMap(type);
   3.180 -        }
   3.181 -        T value = computeValue(type);
   3.182 -        STORE_BARRIER.lazySet(0);
   3.183 -        // All stores pending from computeValue are completed.
   3.184 -        synchronized (map) {
   3.185 -            // Warm up the table with a null entry.
   3.186 -            map.preInitializeEntry(this);
   3.187 -        }
   3.188 -        STORE_BARRIER.lazySet(0);
   3.189 -        // All stores pending from table expansion are completed.
   3.190 -        synchronized (map) {
   3.191 -            value = (T) map.initializeEntry(this, value);
   3.192 -            // One might fear a possible race condition here
   3.193 -            // if the code for map.put has flushed the write
   3.194 -            // to map.table[*] before the writes to the Map.Entry
   3.195 -            // are done.  This is not possible, since we have
   3.196 -            // warmed up the table with an empty entry.
   3.197 -        }
   3.198 -        return value;
   3.199 -    }
   3.200 -
   3.201 -    // Replace this map by a per-class slot.
   3.202 -    private static final WeakHashMap<Class<?>, ClassValueMap> ROOT
   3.203 -        = new WeakHashMap<Class<?>, ClassValueMap>();
   3.204 -
   3.205 -    private static ClassValueMap getMap(Class<?> type) {
   3.206 -        type.getClass();  // test for null
   3.207 -        return ROOT.get(type);
   3.208 -    }
   3.209 -
   3.210 -    private static ClassValueMap initializeMap(Class<?> type) {
   3.211 -        synchronized (ClassValue.class) {
   3.212 -            ClassValueMap map = ROOT.get(type);
   3.213 -            if (map == null)
   3.214 -                ROOT.put(type, map = new ClassValueMap());
   3.215 -            return map;
   3.216 -        }
   3.217 -    }
   3.218 -
   3.219 -    static class ClassValueMap extends WeakHashMap<ClassValue, Object> {
   3.220 -        /** Make sure this table contains an Entry for the given key, even if it is empty. */
   3.221 -        void preInitializeEntry(ClassValue key) {
   3.222 -            if (!this.containsKey(key))
   3.223 -                this.put(key, null);
   3.224 -        }
   3.225 -        /** Make sure this table contains a non-empty Entry for the given key. */
   3.226 -        Object initializeEntry(ClassValue key, Object value) {
   3.227 -            Object prior = this.get(key);
   3.228 -            if (prior != null) {
   3.229 -                return unmaskNull(prior);
   3.230 -            }
   3.231 -            this.put(key, maskNull(value));
   3.232 -            return value;
   3.233 -        }
   3.234 -
   3.235 -        Object maskNull(Object x) {
   3.236 -            return x == null ? this : x;
   3.237 -        }
   3.238 -        Object unmaskNull(Object x) {
   3.239 -            return x == this ? null : x;
   3.240 -        }
   3.241 -    }
   3.242 -}