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

Thu, 24 Jul 2008 19:06:57 +0100

author
mcimadamore
date
Thu, 24 Jul 2008 19:06:57 +0100
changeset 80
5c9cdeb740f2
parent 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

6717241: some diagnostic argument is prematurely converted into a String object
Summary: removed early toString() conversions applied to diagnostic arguments
Reviewed-by: jjg

duke@1 1 /*
duke@1 2 * Copyright 2001-2006 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 com.sun.tools.javac.Main;
duke@1 29 import java.util.*;
duke@1 30
duke@1 31 /**
duke@1 32 * Support for an abstract context, modelled loosely after ThreadLocal
duke@1 33 * but using a user-provided context instead of the current thread.
duke@1 34 *
duke@1 35 * <p>Within the compiler, a single Context is used for each
duke@1 36 * invocation of the compiler. The context is then used to ensure a
duke@1 37 * single copy of each compiler phase exists per compiler invocation.
duke@1 38 *
duke@1 39 * <p>The context can be used to assist in extending the compiler by
duke@1 40 * extending its components. To do that, the extended component must
duke@1 41 * be registered before the base component. We break initialization
duke@1 42 * cycles by (1) registering a factory for the component rather than
duke@1 43 * the component itself, and (2) a convention for a pattern of usage
duke@1 44 * in which each base component registers itself by calling an
duke@1 45 * instance method that is overridden in extended components. A base
duke@1 46 * phase supporting extension would look something like this:
duke@1 47 *
duke@1 48 * <p><pre>
duke@1 49 * public class Phase {
duke@1 50 * protected static final Context.Key<Phase> phaseKey =
duke@1 51 * new Context.Key<Phase>();
duke@1 52 *
duke@1 53 * public static Phase instance(Context context) {
duke@1 54 * Phase instance = context.get(phaseKey);
duke@1 55 * if (instance == null)
duke@1 56 * // the phase has not been overridden
duke@1 57 * instance = new Phase(context);
duke@1 58 * return instance;
duke@1 59 * }
duke@1 60 *
duke@1 61 * protected Phase(Context context) {
duke@1 62 * context.put(phaseKey, this);
duke@1 63 * // other intitialization follows...
duke@1 64 * }
duke@1 65 * }
duke@1 66 * </pre>
duke@1 67 *
duke@1 68 * <p>In the compiler, we simply use Phase.instance(context) to get
duke@1 69 * the reference to the phase. But in extensions of the compiler, we
duke@1 70 * must register extensions of the phases to replace the base phase,
duke@1 71 * and this must be done before any reference to the phase is accessed
duke@1 72 * using Phase.instance(). An extended phase might be declared thus:
duke@1 73 *
duke@1 74 * <p><pre>
duke@1 75 * public class NewPhase extends Phase {
duke@1 76 * protected NewPhase(Context context) {
duke@1 77 * super(context);
duke@1 78 * }
duke@1 79 * public static void preRegister(final Context context) {
duke@1 80 * context.put(phaseKey, new Context.Factory<Phase>() {
duke@1 81 * public Phase make() {
duke@1 82 * return new NewPhase(context);
duke@1 83 * }
duke@1 84 * });
duke@1 85 * }
duke@1 86 * }
duke@1 87 * </pre>
duke@1 88 *
duke@1 89 * <p>And is registered early in the extended compiler like this
duke@1 90 *
duke@1 91 * <p><pre>
duke@1 92 * NewPhase.preRegister(context);
duke@1 93 * </pre>
duke@1 94 *
duke@1 95 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 96 * you write code that depends on this, you do so at your own risk.
duke@1 97 * This code and its internal interfaces are subject to change or
duke@1 98 * deletion without notice.</b>
duke@1 99 */
duke@1 100 public class Context {
duke@1 101 /** The client creates an instance of this class for each key.
duke@1 102 */
duke@1 103 public static class Key<T> {
duke@1 104 // note: we inherit identity equality from Object.
duke@1 105 }
duke@1 106
duke@1 107 /**
duke@1 108 * The client can register a factory for lazy creation of the
duke@1 109 * instance.
duke@1 110 */
duke@1 111 public static interface Factory<T> {
duke@1 112 T make();
duke@1 113 };
duke@1 114
duke@1 115 /**
duke@1 116 * The underlying map storing the data.
duke@1 117 * We maintain the invariant that this table contains only
duke@1 118 * mappings of the form
duke@1 119 * Key<T> -> T or Key<T> -> Factory<T> */
duke@1 120 private Map<Key,Object> ht = new HashMap<Key,Object>();
duke@1 121
duke@1 122 /** Set the factory for the key in this context. */
duke@1 123 public <T> void put(Key<T> key, Factory<T> fac) {
duke@1 124 checkState(ht);
duke@1 125 Object old = ht.put(key, fac);
duke@1 126 if (old != null)
duke@1 127 throw new AssertionError("duplicate context value");
duke@1 128 }
duke@1 129
duke@1 130 /** Set the value for the key in this context. */
duke@1 131 public <T> void put(Key<T> key, T data) {
duke@1 132 if (data instanceof Factory)
duke@1 133 throw new AssertionError("T extends Context.Factory");
duke@1 134 checkState(ht);
duke@1 135 Object old = ht.put(key, data);
duke@1 136 if (old != null && !(old instanceof Factory) && old != data && data != null)
duke@1 137 throw new AssertionError("duplicate context value");
duke@1 138 }
duke@1 139
duke@1 140 /** Get the value for the key in this context. */
duke@1 141 public <T> T get(Key<T> key) {
duke@1 142 checkState(ht);
duke@1 143 Object o = ht.get(key);
duke@1 144 if (o instanceof Factory) {
duke@1 145 Factory fac = (Factory)o;
duke@1 146 o = fac.make();
duke@1 147 if (o instanceof Factory)
duke@1 148 throw new AssertionError("T extends Context.Factory");
duke@1 149 assert ht.get(key) == o;
duke@1 150 }
duke@1 151
duke@1 152 /* The following cast can't fail unless there was
duke@1 153 * cheating elsewhere, because of the invariant on ht.
duke@1 154 * Since we found a key of type Key<T>, the value must
duke@1 155 * be of type T.
duke@1 156 */
duke@1 157 return Context.<T>uncheckedCast(o);
duke@1 158 }
duke@1 159
duke@1 160 public Context() {}
duke@1 161
duke@1 162 private Map<Class<?>, Key<?>> kt = new HashMap<Class<?>, Key<?>>();
duke@1 163
duke@1 164 private <T> Key<T> key(Class<T> clss) {
duke@1 165 checkState(kt);
duke@1 166 Key<T> k = uncheckedCast(kt.get(clss));
duke@1 167 if (k == null) {
duke@1 168 k = new Key<T>();
duke@1 169 kt.put(clss, k);
duke@1 170 }
duke@1 171 return k;
duke@1 172 }
duke@1 173
duke@1 174 public <T> T get(Class<T> clazz) {
duke@1 175 return get(key(clazz));
duke@1 176 }
duke@1 177
duke@1 178 public <T> void put(Class<T> clazz, T data) {
duke@1 179 put(key(clazz), data);
duke@1 180 }
duke@1 181 public <T> void put(Class<T> clazz, Factory<T> fac) {
duke@1 182 put(key(clazz), fac);
duke@1 183 }
duke@1 184
duke@1 185 /**
duke@1 186 * TODO: This method should be removed and Context should be made type safe.
duke@1 187 * This can be accomplished by using class literals as type tokens.
duke@1 188 */
duke@1 189 @SuppressWarnings("unchecked")
duke@1 190 private static <T> T uncheckedCast(Object o) {
duke@1 191 return (T)o;
duke@1 192 }
duke@1 193
duke@1 194 public void dump() {
duke@1 195 for (Object value : ht.values())
duke@1 196 System.err.println(value == null ? null : value.getClass());
duke@1 197 }
duke@1 198
duke@1 199 public void clear() {
duke@1 200 ht = null;
duke@1 201 kt = null;
duke@1 202 }
duke@1 203
duke@1 204 private static void checkState(Map<?,?> t) {
duke@1 205 if (t == null)
duke@1 206 throw new IllegalStateException();
duke@1 207 }
duke@1 208 }

mercurial