-
Notifications
You must be signed in to change notification settings - Fork 183
/
BlockNameSpace.java
185 lines (169 loc) · 7.19 KB
/
BlockNameSpace.java
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
/*****************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
* *
* *
* This file is part of the BeanShell Java Scripting distribution. *
* Documentation and updates may be found at http://www.beanshell.org/ *
* Patrick Niemeyer (pat@pat.net) *
* Author of Learning Java, O'Reilly & Associates *
* *
*****************************************************************************/
package bsh;
/**
A specialized namespace for Blocks (e.g. the body of a "for" statement).
The Block acts like a child namespace but only for typed variables
declared within it (block local scope) or untyped variables explicitly set
in it via setBlockVariable(). Otherwise variable assignment
(including untyped variable usage) acts like it is part of the containing
block.
<p>
*/
/*
Note: This class essentially just delegates most of its methods to its
parent. The setVariable() indirection is very small. We could probably
fold this functionality back into the base NameSpace as a special case.
But this has changed a few times so I'd like to leave this abstraction for
now.
*/
class BlockNameSpace extends NameSpace
{
public BlockNameSpace( NameSpace parent )
throws EvalError
{
super( parent, parent.getName()+ "/BlockNameSpace" );
this.isMethod = parent.isMethod;
}
/**
Override the standard namespace behavior to make assignments
happen in our parent (enclosing) namespace, unless the variable has
already been assigned here via a typed declaration or through
the special setBlockVariable() (used for untyped args in try/catch).
<p>
i.e. only allow typed var declaration to happen in this namespace.
Typed vars are handled in the ordinary way local scope. All untyped
assignments are delegated to the enclosing context.
*/
/*
Note: it may see like with the new 1.3 scoping this test could be
removed, but it cannot. When recurse is false we still need to set the
variable in our parent, not here.
*/
public Variable setVariable(
String name, Object value, boolean strictJava, boolean recurse )
throws UtilEvalError
{
if ( weHaveVar( name ) )
// set the var here in the block namespace
return super.setVariable( name, value, strictJava, false );
else
// set the var in the enclosing (parent) namespace
return getParent().setVariable( name, value, strictJava, recurse );
}
/**
Set an untyped variable in the block namespace.
The BlockNameSpace would normally delegate this set to the parent.
Typed variables are naturally set locally.
This is used in try/catch block argument.
*/
public void setBlockVariable( String name, Object value )
throws UtilEvalError
{
super.setVariable( name, value, false/*strict?*/, false );
}
/**
We have the variable: either it was declared here with a type, giving
it block local scope or an untyped var was explicitly set here via
setBlockVariable().
*/
private boolean weHaveVar( String name )
{
// super.variables.containsKey( name ) not any faster, I checked
try {
return super.getVariableImpl( name, false ) != null;
} catch ( UtilEvalError e ) { return false; }
}
/**
Get the actual BlockNameSpace 'this' reference.
<p/>
Normally a 'this' reference to a BlockNameSpace (e.g. if () { } )
resolves to the parent namespace (e.g. the namespace containing the
"if" statement). However when code inside the BlockNameSpace needs to
resolve things relative to 'this' we must use the actual block's 'this'
reference. Name.java is smart enough to handle this using
getBlockThis().
@see #getThis( Interpreter )
This getBlockThis( Interpreter declaringInterpreter )
{
return super.getThis( declaringInterpreter );
}
*/
//
// Begin methods which simply delegate to our parent (enclosing scope)
//
/**
This method recurses to find the nearest non-BlockNameSpace parent.
public NameSpace getParent()
{
NameSpace parent = super.getParent();
if ( parent instanceof BlockNameSpace )
return parent.getParent();
else
return parent;
}
*/
/** do we need this? */
private NameSpace getNonBlockParent()
{
NameSpace parent = super.getParent();
if ( parent instanceof BlockNameSpace )
return ((BlockNameSpace)parent).getNonBlockParent();
else
return parent;
}
/**
Get a 'this' reference is our parent's 'this' for the object closure.
e.g. Normally a 'this' reference to a BlockNameSpace (e.g. if () { } )
resolves to the parent namespace (e.g. the namespace containing the
"if" statement).
@see #getBlockThis( Interpreter )
*/
public This getThis( Interpreter declaringInterpreter ) {
return getNonBlockParent().getThis( declaringInterpreter );
}
/**
super is our parent's super
*/
public This getSuper( Interpreter declaringInterpreter ) {
return getNonBlockParent().getSuper( declaringInterpreter );
}
/**
delegate import to our parent
*/
public void importClass(String name) {
getParent().importClass( name );
}
/**
delegate import to our parent
*/
public void importPackage(String name) {
getParent().importPackage( name );
}
public void setMethod(BshMethod method) {
getParent().setMethod( method );
}
}