langtools/src/share/classes/com/sun/tools/javac/util/Context.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 1264 076a3cde30d5
child 5520 86e4b9a9da40
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 1206
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * Support for an abstract context, modelled loosely after ThreadLocal
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 * but using a user-provided context instead of the current thread.
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * <p>Within the compiler, a single Context is used for each
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * invocation of the compiler.  The context is then used to ensure a
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * single copy of each compiler phase exists per compiler invocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * <p>The context can be used to assist in extending the compiler by
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * extending its components.  To do that, the extended component must
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * be registered before the base component.  We break initialization
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * cycles by (1) registering a factory for the component rather than
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * the component itself, and (2) a convention for a pattern of usage
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * in which each base component registers itself by calling an
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * instance method that is overridden in extended components.  A base
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * phase supporting extension would look something like this:
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * <p><pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * public class Phase {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *     protected static final Context.Key<Phase> phaseKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *         new Context.Key<Phase>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *     public static Phase instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *         Phase instance = context.get(phaseKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *         if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *             // the phase has not been overridden
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *             instance = new Phase(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *         return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *     }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *     protected Phase(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *         context.put(phaseKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *         // other intitialization follows...
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *     }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 * <p>In the compiler, we simply use Phase.instance(context) to get
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 * the reference to the phase.  But in extensions of the compiler, we
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 * must register extensions of the phases to replace the base phase,
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 * and this must be done before any reference to the phase is accessed
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 * using Phase.instance().  An extended phase might be declared thus:
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * <p><pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 * public class NewPhase extends Phase {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 *     protected NewPhase(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 *         super(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 *     }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 *     public static void preRegister(final Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 *         context.put(phaseKey, new Context.Factory<Phase>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 *             public Phase make() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 *                 return new NewPhase(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 *             }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 *         });
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 *     }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 * }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
 * <p>And is registered early in the extended compiler like this
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 * <p><pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 *     NewPhase.preRegister(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
public class Context {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /** The client creates an instance of this class for each key.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    public static class Key<T> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        // note: we inherit identity equality from Object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * The client can register a factory for lazy creation of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * instance.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public static interface Factory<T> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        T make();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     * The underlying map storing the data.
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * We maintain the invariant that this table contains only
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * mappings of the form
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * Key<T> -> T or Key<T> -> Factory<T> */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   119
    private Map<Key<?>,Object> ht = new HashMap<Key<?>,Object>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /** Set the factory for the key in this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public <T> void put(Key<T> key, Factory<T> fac) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        checkState(ht);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        Object old = ht.put(key, fac);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        if (old != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            throw new AssertionError("duplicate context value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /** Set the value for the key in this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public <T> void put(Key<T> key, T data) {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   131
        if (data instanceof Factory<?>)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            throw new AssertionError("T extends Context.Factory");
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        checkState(ht);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        Object old = ht.put(key, data);
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   135
        if (old != null && !(old instanceof Factory<?>) && old != data && data != null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            throw new AssertionError("duplicate context value");
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    /** Get the value for the key in this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    public <T> T get(Key<T> key) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        checkState(ht);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        Object o = ht.get(key);
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   143
        if (o instanceof Factory<?>) {
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   144
            Factory<?> fac = (Factory<?>)o;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            o = fac.make();
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
   146
            if (o instanceof Factory<?>)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                throw new AssertionError("T extends Context.Factory");
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            assert ht.get(key) == o;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        /* The following cast can't fail unless there was
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
         * cheating elsewhere, because of the invariant on ht.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
         * Since we found a key of type Key<T>, the value must
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
         * be of type T.
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        return Context.<T>uncheckedCast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public Context() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    private Map<Class<?>, Key<?>> kt = new HashMap<Class<?>, Key<?>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    private <T> Key<T> key(Class<T> clss) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        checkState(kt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        Key<T> k = uncheckedCast(kt.get(clss));
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        if (k == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            k = new Key<T>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            kt.put(clss, k);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        return k;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    public <T> T get(Class<T> clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        return get(key(clazz));
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    public <T> void put(Class<T> clazz, T data) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        put(key(clazz), data);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    public <T> void put(Class<T> clazz, Factory<T> fac) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        put(key(clazz), fac);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     * TODO: This method should be removed and Context should be made type safe.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     * This can be accomplished by using class literals as type tokens.
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    @SuppressWarnings("unchecked")
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    private static <T> T uncheckedCast(Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        return (T)o;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    public void dump() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        for (Object value : ht.values())
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            System.err.println(value == null ? null : value.getClass());
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    public void clear() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        ht = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        kt = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    private static void checkState(Map<?,?> t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        if (t == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            throw new IllegalStateException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
}