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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 104
5e89c4ca637c
child 184
905e151a185a
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 2001-2008 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any 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 *
duke@1 94 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 95 * 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> {
duke@1 111 T make();
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> */
duke@1 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");
duke@1 127 }
duke@1 128
duke@1 129 /** Set the value for the key in this context. */
duke@1 130 public <T> void put(Key<T> key, T data) {
duke@1 131 if (data instanceof Factory)
duke@1 132 throw new AssertionError("T extends Context.Factory");
duke@1 133 checkState(ht);
duke@1 134 Object old = ht.put(key, data);
duke@1 135 if (old != null && !(old instanceof Factory) && old != data && data != null)
duke@1 136 throw new AssertionError("duplicate context value");
duke@1 137 }
duke@1 138
duke@1 139 /** Get the value for the key in this context. */
duke@1 140 public <T> T get(Key<T> key) {
duke@1 141 checkState(ht);
duke@1 142 Object o = ht.get(key);
duke@1 143 if (o instanceof Factory) {
duke@1 144 Factory fac = (Factory)o;
duke@1 145 o = fac.make();
duke@1 146 if (o instanceof Factory)
duke@1 147 throw new AssertionError("T extends Context.Factory");
duke@1 148 assert ht.get(key) == o;
duke@1 149 }
duke@1 150
duke@1 151 /* The following cast can't fail unless there was
duke@1 152 * cheating elsewhere, because of the invariant on ht.
duke@1 153 * Since we found a key of type Key<T>, the value must
duke@1 154 * be of type T.
duke@1 155 */
duke@1 156 return Context.<T>uncheckedCast(o);
duke@1 157 }
duke@1 158
duke@1 159 public Context() {}
duke@1 160
duke@1 161 private Map<Class<?>, Key<?>> kt = new HashMap<Class<?>, Key<?>>();
duke@1 162
duke@1 163 private <T> Key<T> key(Class<T> clss) {
duke@1 164 checkState(kt);
duke@1 165 Key<T> k = uncheckedCast(kt.get(clss));
duke@1 166 if (k == null) {
duke@1 167 k = new Key<T>();
duke@1 168 kt.put(clss, k);
duke@1 169 }
duke@1 170 return k;
duke@1 171 }
duke@1 172
duke@1 173 public <T> T get(Class<T> clazz) {
duke@1 174 return get(key(clazz));
duke@1 175 }
duke@1 176
duke@1 177 public <T> void put(Class<T> clazz, T data) {
duke@1 178 put(key(clazz), data);
duke@1 179 }
duke@1 180 public <T> void put(Class<T> clazz, Factory<T> fac) {
duke@1 181 put(key(clazz), fac);
duke@1 182 }
duke@1 183
duke@1 184 /**
duke@1 185 * TODO: This method should be removed and Context should be made type safe.
duke@1 186 * This can be accomplished by using class literals as type tokens.
duke@1 187 */
duke@1 188 @SuppressWarnings("unchecked")
duke@1 189 private static <T> T uncheckedCast(Object o) {
duke@1 190 return (T)o;
duke@1 191 }
duke@1 192
duke@1 193 public void dump() {
duke@1 194 for (Object value : ht.values())
duke@1 195 System.err.println(value == null ? null : value.getClass());
duke@1 196 }
duke@1 197
duke@1 198 public void clear() {
duke@1 199 ht = null;
duke@1 200 kt = null;
duke@1 201 }
duke@1 202
duke@1 203 private static void checkState(Map<?,?> t) {
duke@1 204 if (t == null)
duke@1 205 throw new IllegalStateException();
duke@1 206 }
duke@1 207 }

mercurial