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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1358
fc123bdeddb8
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1326 2 * Copyright (c) 2001, 2012, 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 *
jjg@1358 47 * <p><pre>{@code
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 * }
jjg@1358 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 *
jjg@1358 73 * <p><pre>{@code
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 * }
jjg@1358 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
jjg@1326 118 * {@literal Key<T> -> T }
jjg@1326 119 * or
jjg@1326 120 * {@literal Key<T> -> Factory<T> }
jjg@1326 121 */
mcimadamore@184 122 private Map<Key<?>,Object> ht = new HashMap<Key<?>,Object>();
duke@1 123
duke@1 124 /** Set the factory for the key in this context. */
duke@1 125 public <T> void put(Key<T> key, Factory<T> fac) {
duke@1 126 checkState(ht);
duke@1 127 Object old = ht.put(key, fac);
duke@1 128 if (old != null)
duke@1 129 throw new AssertionError("duplicate context value");
jjg@893 130 checkState(ft);
jjg@893 131 ft.put(key, fac); // cannot be duplicate if unique in ht
duke@1 132 }
duke@1 133
duke@1 134 /** Set the value for the key in this context. */
duke@1 135 public <T> void put(Key<T> key, T data) {
mcimadamore@184 136 if (data instanceof Factory<?>)
duke@1 137 throw new AssertionError("T extends Context.Factory");
duke@1 138 checkState(ht);
duke@1 139 Object old = ht.put(key, data);
mcimadamore@184 140 if (old != null && !(old instanceof Factory<?>) && old != data && data != null)
duke@1 141 throw new AssertionError("duplicate context value");
duke@1 142 }
duke@1 143
duke@1 144 /** Get the value for the key in this context. */
duke@1 145 public <T> T get(Key<T> key) {
duke@1 146 checkState(ht);
duke@1 147 Object o = ht.get(key);
mcimadamore@184 148 if (o instanceof Factory<?>) {
mcimadamore@184 149 Factory<?> fac = (Factory<?>)o;
jjg@893 150 o = fac.make(this);
mcimadamore@184 151 if (o instanceof Factory<?>)
duke@1 152 throw new AssertionError("T extends Context.Factory");
jjg@816 153 Assert.check(ht.get(key) == o);
duke@1 154 }
duke@1 155
duke@1 156 /* The following cast can't fail unless there was
duke@1 157 * cheating elsewhere, because of the invariant on ht.
duke@1 158 * Since we found a key of type Key<T>, the value must
duke@1 159 * be of type T.
duke@1 160 */
duke@1 161 return Context.<T>uncheckedCast(o);
duke@1 162 }
duke@1 163
duke@1 164 public Context() {}
duke@1 165
jjg@893 166 /**
jjg@893 167 * The table of preregistered factories.
jjg@893 168 */
jjg@893 169 private Map<Key<?>,Factory<?>> ft = new HashMap<Key<?>,Factory<?>>();
jjg@893 170
jjg@893 171 public Context(Context prev) {
jjg@893 172 kt.putAll(prev.kt); // retain all implicit keys
jjg@893 173 ft.putAll(prev.ft); // retain all factory objects
jjg@893 174 ht.putAll(prev.ft); // init main table with factories
jjg@893 175 }
jjg@893 176
jjg@893 177 /*
jjg@893 178 * The key table, providing a unique Key<T> for each Class<T>.
jjg@893 179 */
duke@1 180 private Map<Class<?>, Key<?>> kt = new HashMap<Class<?>, Key<?>>();
duke@1 181
duke@1 182 private <T> Key<T> key(Class<T> clss) {
duke@1 183 checkState(kt);
duke@1 184 Key<T> k = uncheckedCast(kt.get(clss));
duke@1 185 if (k == null) {
duke@1 186 k = new Key<T>();
duke@1 187 kt.put(clss, k);
duke@1 188 }
duke@1 189 return k;
duke@1 190 }
duke@1 191
duke@1 192 public <T> T get(Class<T> clazz) {
duke@1 193 return get(key(clazz));
duke@1 194 }
duke@1 195
duke@1 196 public <T> void put(Class<T> clazz, T data) {
duke@1 197 put(key(clazz), data);
duke@1 198 }
duke@1 199 public <T> void put(Class<T> clazz, Factory<T> fac) {
duke@1 200 put(key(clazz), fac);
duke@1 201 }
duke@1 202
duke@1 203 /**
duke@1 204 * TODO: This method should be removed and Context should be made type safe.
duke@1 205 * This can be accomplished by using class literals as type tokens.
duke@1 206 */
duke@1 207 @SuppressWarnings("unchecked")
duke@1 208 private static <T> T uncheckedCast(Object o) {
duke@1 209 return (T)o;
duke@1 210 }
duke@1 211
duke@1 212 public void dump() {
duke@1 213 for (Object value : ht.values())
duke@1 214 System.err.println(value == null ? null : value.getClass());
duke@1 215 }
duke@1 216
duke@1 217 public void clear() {
duke@1 218 ht = null;
duke@1 219 kt = null;
jjg@893 220 ft = null;
duke@1 221 }
duke@1 222
duke@1 223 private static void checkState(Map<?,?> t) {
duke@1 224 if (t == null)
duke@1 225 throw new IllegalStateException();
duke@1 226 }
duke@1 227 }

mercurial