|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Expression.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: Expression.java,v 1.3 2004/07/10 03:31:37 bran Exp $
|
|
3 |
|
|
4 |
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
|
|
6 |
Redistribution and use of this software and associated documentation
|
|
7 |
("Software"), with or without modification, are permitted provided
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
44 |
|
|
45 |
*/
|
|
46 |
package org.codehaus.groovy.ast.expr;
|
|
47 |
|
|
48 |
import java.util.ArrayList;
|
|
49 |
import java.util.Iterator;
|
|
50 |
import java.util.List;
|
|
51 |
|
|
52 |
import org.codehaus.groovy.ast.ASTNode;
|
|
53 |
import org.codehaus.groovy.classgen.AsmClassGenerator2;
|
|
54 |
import groovy.lang.GroovyRuntimeException;
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Represents a base class for expressions which evaluate as an object
|
|
58 |
*
|
|
59 |
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
|
|
60 |
* @version $Revision: 1.3 $
|
|
61 |
*/
|
|
62 |
public abstract class Expression extends ASTNode { |
|
63 |
protected boolean typeResolved = false; |
|
64 |
|
|
65 | 0 |
public boolean isResolveFailed() { |
66 | 0 |
return resolveFailed;
|
67 |
} |
|
68 |
|
|
69 | 0 |
public void setResolveFailed(boolean resolveFailed) { |
70 | 0 |
this.resolveFailed = resolveFailed;
|
71 |
} |
|
72 |
|
|
73 | 0 |
public String getFailure() {
|
74 | 0 |
return failure;
|
75 |
} |
|
76 |
|
|
77 | 0 |
public void setFailure(String failure) { |
78 | 0 |
this.failure = failure;
|
79 |
} |
|
80 |
|
|
81 |
String failure = "";
|
|
82 |
private boolean resolveFailed = false; |
|
83 |
|
|
84 | 0 |
public Class getTypeClass() {
|
85 | 0 |
return typeClass;
|
86 |
} |
|
87 |
|
|
88 | 0 |
public void setTypeClass(Class typeClass) { |
89 | 0 |
if (typeClass != null) { |
90 | 0 |
this.typeClass = typeClass;
|
91 | 0 |
type = typeClass.getName(); |
92 | 0 |
setTypeResolved(true);
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
public Class typeClass = null; |
|
97 |
|
|
98 | 0 |
public String getType() {
|
99 | 0 |
if (type != null){ |
100 | 0 |
return type;
|
101 |
} else {
|
|
102 | 0 |
Class cls = getTypeClass(); |
103 | 0 |
return cls != null? cls.getName() : null;//"java.lang.Object"; |
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
/**
|
|
108 |
* true if the datatype can be changed, false otherwise.
|
|
109 |
* @return
|
|
110 |
*/
|
|
111 | 0 |
public boolean isDynamic() { |
112 | 0 |
return true; |
113 |
} |
|
114 |
|
|
115 |
protected String type = null; |
|
116 |
/**
|
|
117 |
* Return a copy of the expression calling the transformer on any nested expressions
|
|
118 |
* @param transformer
|
|
119 |
* @return
|
|
120 |
*/
|
|
121 |
public abstract Expression transformExpression(ExpressionTransformer transformer);
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Transforms the list of expressions
|
|
125 |
* @return a new list of transformed expressions
|
|
126 |
*/
|
|
127 | 0 |
protected List transformExpressions(List expressions, ExpressionTransformer transformer) {
|
128 | 0 |
List list = new ArrayList(expressions.size());
|
129 | 0 |
for (Iterator iter = expressions.iterator(); iter.hasNext(); ) {
|
130 | 0 |
list.add(transformer.transform((Expression) iter.next())); |
131 |
} |
|
132 | 0 |
return list;
|
133 |
} |
|
134 |
|
|
135 | 0 |
public void setType(String name) { |
136 | 0 |
if (name == null) |
137 | 0 |
throw new GroovyRuntimeException("cannot set null on type"); |
138 |
// handle primitives first
|
|
139 | 0 |
if (name.equals("int")) { |
140 | 0 |
setTypeClass(Integer.TYPE); |
141 | 0 |
return;
|
142 |
} |
|
143 | 0 |
else if (name.equals("long")) { |
144 | 0 |
setTypeClass(Long.TYPE); |
145 | 0 |
return;
|
146 |
} |
|
147 | 0 |
else if (name.equals("short")) { |
148 | 0 |
setTypeClass(Short.TYPE); |
149 | 0 |
return;
|
150 |
} |
|
151 | 0 |
else if (name.equals("float")) { |
152 | 0 |
setTypeClass(Float.TYPE); |
153 | 0 |
return;
|
154 |
} |
|
155 | 0 |
else if (name.equals("double")) { |
156 | 0 |
setTypeClass(Double.TYPE); |
157 | 0 |
return;
|
158 |
} |
|
159 | 0 |
else if (name.equals("byte")) { |
160 | 0 |
setTypeClass(Byte.TYPE); |
161 | 0 |
return;
|
162 |
} |
|
163 | 0 |
else if (name.equals("char")) { |
164 | 0 |
setTypeClass(Character.TYPE); |
165 | 0 |
return;
|
166 |
} |
|
167 | 0 |
else if (name.equals("boolean")) { |
168 | 0 |
setTypeClass(Boolean.TYPE); |
169 | 0 |
return;
|
170 |
} |
|
171 |
|
|
172 | 0 |
if (name.endsWith("[]")) { |
173 | 0 |
String prefix = "[";
|
174 | 0 |
name = name.substring(0, name.length() - 2); |
175 |
|
|
176 | 0 |
if (name.equals("int")) { |
177 | 0 |
type = prefix + "I";
|
178 |
} |
|
179 | 0 |
else if (name.equals("long")) { |
180 | 0 |
type = prefix + "J";
|
181 |
} |
|
182 | 0 |
else if (name.equals("short")) { |
183 | 0 |
type = prefix + "S";
|
184 |
} |
|
185 | 0 |
else if (name.equals("float")) { |
186 | 0 |
type = prefix + "F";
|
187 |
} |
|
188 | 0 |
else if (name.equals("double")) { |
189 | 0 |
type = prefix + "D";
|
190 |
} |
|
191 | 0 |
else if (name.equals("byte")) { |
192 | 0 |
type = prefix + "B";
|
193 |
} |
|
194 | 0 |
else if (name.equals("char")) { |
195 | 0 |
type = prefix + "C";
|
196 |
} |
|
197 | 0 |
else if (name.equals("boolean")) { |
198 | 0 |
type = prefix + "Z";
|
199 |
} else {
|
|
200 | 0 |
type = prefix + "L" + name + ";"; |
201 |
} |
|
202 |
} |
|
203 |
else {
|
|
204 | 0 |
type = name; |
205 |
} |
|
206 | 0 |
if (type == null) { |
207 | 0 |
System.out.println("Expression.setType(): null");
|
208 | 0 |
System.out.println("name = " + name);
|
209 |
} |
|
210 | 0 |
try {
|
211 | 0 |
this.setTypeClass(Class.forName(type, false, this.getClass().getClassLoader())); |
212 |
} catch (Throwable e) {
|
|
213 | 0 |
this.typeResolved = false; |
214 |
} |
|
215 |
} |
|
216 |
|
|
217 | 0 |
public boolean isTypeResolved() { |
218 | 0 |
return typeResolved;
|
219 |
} |
|
220 |
|
|
221 | 0 |
public void setTypeResolved(boolean b) { |
222 | 0 |
this.typeResolved = b;
|
223 | 0 |
this.resolveFailed = false; |
224 |
} |
|
225 |
|
|
226 | 0 |
public void resolve(AsmClassGenerator2 cg) { |
227 | 0 |
if (shouldContinue()) {
|
228 | 0 |
resolveType(cg); |
229 |
} |
|
230 |
} |
|
231 |
|
|
232 |
protected abstract void resolveType(AsmClassGenerator2 resolver); |
|
233 |
|
|
234 | 0 |
protected boolean shouldContinue() { |
235 | 0 |
return !isResolveFailed() && !isTypeResolved();
|
236 |
} |
|
237 |
|
|
238 |
} |
|
239 |
|
|