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