-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathNamespace.java
More file actions
277 lines (230 loc) · 7.61 KB
/
Namespace.java
File metadata and controls
277 lines (230 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* rich Jan 23, 2008 */
package clojure.lang;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
public class Namespace extends AReference implements Serializable {
private static final long serialVersionUID = -3134444395801383865L;
final public Symbol name;
transient final AtomicReference<IPersistentMap> mappings = new AtomicReference<IPersistentMap>();
transient final AtomicReference<IPersistentMap> aliases = new AtomicReference<IPersistentMap>();
final static ConcurrentHashMap<Symbol, Namespace> namespaces = new ConcurrentHashMap<Symbol, Namespace>();
public String toString(){
return name.toString();
}
Namespace(Symbol name){
super(name.meta());
this.name = name;
mappings.set(RT.DEFAULT_IMPORTS);
aliases.set(RT.map());
}
public static ISeq all(){
return RT.seq(namespaces.values());
}
public Symbol getName(){
return name;
}
public IPersistentMap getMappings(){
return mappings.get();
}
/**
* An interned mapping is one where a var's ns matches the current ns and its sym matches the mapping key.
* Once established, interned mappings should never change.
*/
private boolean isInternedMapping(Symbol sym, Object o){
return(o instanceof Var &&
((Var) o).ns == this &&
((Var) o).sym.equals(sym));
}
public Var intern(Symbol sym){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
Object o;
Var v = null;
while((o = map.valAt(sym)) == null)
{
if(v == null)
v = new Var(this, sym);
IPersistentMap newMap = map.assoc(sym, v);
mappings.compareAndSet(map, newMap);
map = getMappings();
}
if(isInternedMapping(sym, o))
return (Var) o;
if(v == null)
v = new Var(this, sym);
if(checkReplacement(sym, o, v)){
while (!mappings.compareAndSet(map, map.assoc(sym, v)))
map = getMappings();
return v;
}
return (Var) o;
}
/*
This method checks if a namespace's mapping is applicable and warns on problematic cases.
It will return a boolean indicating if a mapping is replaceable.
The semantics of what constitutes a legal replacement mapping is summarized as follows:
| classification | in namespace ns | newval = anything other than ns/name | newval = ns/name |
|----------------+------------------------+--------------------------------------+-------------------------------------|
| native mapping | name -> ns/name | no replace, warn-if newval not-core | no replace, warn-if newval not-core |
| alias mapping | name -> other/whatever | warn + replace | warn + replace |
*/
private boolean checkReplacement(Symbol sym, Object old, Object neu){
if(old instanceof Var) {
Namespace ons = ((Var)old).ns;
Namespace nns = neu instanceof Var ? ((Var) neu).ns : null;
if(isInternedMapping(sym, old)){
if(nns != RT.CLOJURE_NS){
RT.errPrintWriter().println("REJECTED: attempt to replace interned var "
+ old + " with " + neu + " in " + name + ", you must ns-unmap first");
return false;
}
else
return false;
}
}
RT.errPrintWriter().println("WARNING: " + sym + " already refers to: " + old + " in namespace: " + name
+ ", being replaced by: " + neu);
return true;
}
Object reference(Symbol sym, Object val){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
Object o;
while((o = map.valAt(sym)) == null)
{
IPersistentMap newMap = map.assoc(sym, val);
mappings.compareAndSet(map, newMap);
map = getMappings();
}
if(o == val)
return o;
if(checkReplacement(sym, o, val)){
while (!mappings.compareAndSet(map, map.assoc(sym, val)))
map = getMappings();
return val;
}
return o;
}
public static boolean areDifferentInstancesOfSameClassName(Class cls1, Class cls2) {
return (cls1 != cls2) && (cls1.getName().equals(cls2.getName()));
}
Class referenceClass(Symbol sym, Class val){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
Class c = (Class) map.valAt(sym);
while((c == null) || (areDifferentInstancesOfSameClassName(c, val)))
{
IPersistentMap newMap = map.assoc(sym, val);
mappings.compareAndSet(map, newMap);
map = getMappings();
c = (Class) map.valAt(sym);
}
if(c == val)
return c;
throw new IllegalStateException(sym + " already refers to: " + c + " in namespace: " + name);
}
public void unmap(Symbol sym) {
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't unintern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
while(map.containsKey(sym))
{
IPersistentMap newMap = map.without(sym);
mappings.compareAndSet(map, newMap);
map = getMappings();
}
}
public Class importClass(Symbol sym, Class c){
return referenceClass(sym, c);
}
public Class importClass(Class c){
String n = c.getName();
return importClass(Symbol.intern(n.substring(n.lastIndexOf('.') + 1)), c);
}
public Var refer(Symbol sym, Var var){
return (Var) reference(sym, var);
}
public static Namespace findOrCreate(Symbol name){
Namespace ns = namespaces.get(name);
if(ns != null)
return ns;
Namespace newns = new Namespace(name);
ns = namespaces.putIfAbsent(name, newns);
return ns == null ? newns : ns;
}
public static Namespace remove(Symbol name){
if(name.equals(RT.CLOJURE_NS.name))
throw new IllegalArgumentException("Cannot remove clojure namespace");
return namespaces.remove(name);
}
public static Namespace find(Symbol name){
return namespaces.get(name);
}
public Object getMapping(Symbol name){
return mappings.get().valAt(name);
}
public Var findInternedVar(Symbol symbol){
Object o = mappings.get().valAt(symbol);
if(o != null && o instanceof Var && ((Var) o).ns == this)
return (Var) o;
return null;
}
public IPersistentMap getAliases(){
return aliases.get();
}
public Namespace lookupAlias(Symbol alias){
IPersistentMap map = getAliases();
return (Namespace) map.valAt(alias);
}
public void addAlias(Symbol alias, Namespace ns){
if (alias == null || ns == null)
throw new NullPointerException("Expecting Symbol + Namespace");
IPersistentMap map = getAliases();
while(!map.containsKey(alias))
{
IPersistentMap newMap = map.assoc(alias, ns);
aliases.compareAndSet(map, newMap);
map = getAliases();
}
// you can rebind an alias, but only to the initially-aliased namespace.
if(!map.valAt(alias).equals(ns))
throw new IllegalStateException("Alias " + alias + " already exists in namespace "
+ name + ", aliasing " + map.valAt(alias));
}
public void removeAlias(Symbol alias) {
IPersistentMap map = getAliases();
while(map.containsKey(alias))
{
IPersistentMap newMap = map.without(alias);
aliases.compareAndSet(map, newMap);
map = getAliases();
}
}
private Object readResolve() throws ObjectStreamException {
// ensures that serialized namespaces are "deserialized" to the
// namespace in the present runtime
return findOrCreate(name);
}
}