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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

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

mercurial