src/share/classes/com/sun/tools/javac/util/Context.java

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 893
8f0dcb9499db
child 1326
30c36e23f154
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

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

mercurial