|
|||||||||||||||||||
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 | |||||||||||||||
MethodNode.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: MethodNode.java,v 1.13 2004/07/10 03:31:38 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;
|
|
47 |
|
|
48 |
import org.codehaus.groovy.ast.stmt.Statement;
|
|
49 |
import org.objectweb.asm.Constants;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Represents a method declaration
|
|
53 |
*
|
|
54 |
* @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
|
|
55 |
* @version $Revision: 1.13 $
|
|
56 |
*/
|
|
57 |
public class MethodNode extends MetadataNode implements Constants { |
|
58 |
|
|
59 |
private String name;
|
|
60 |
private int modifiers; |
|
61 |
private String returnType;
|
|
62 |
private Parameter[] parameters;
|
|
63 |
private Statement code;
|
|
64 |
private boolean dynamicReturnType; |
|
65 |
private VariableScope variableScope;
|
|
66 |
ClassNode declaringClass; |
|
67 |
|
|
68 | 0 |
public MethodNode(String name, int modifiers, String returnType, Parameter[] parameters, Statement code) { |
69 | 0 |
this.name = name;
|
70 | 0 |
this.modifiers = modifiers;
|
71 | 0 |
this.parameters = parameters;
|
72 | 0 |
this.code = code;
|
73 |
|
|
74 | 0 |
if (returnType == null || returnType.length() == 0) { |
75 | 0 |
this.returnType = "java.lang.Object"; |
76 | 0 |
this.dynamicReturnType = true; |
77 |
} |
|
78 |
else {
|
|
79 | 0 |
this.returnType = ensureJavaTypeNameSyntax(returnType);
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* The type descriptor for a method node is a string containing the name of the method, its return type,
|
|
85 |
* and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
|
|
86 |
* without parameter names, and with $dynamic as the type for any dynamically typed values.
|
|
87 |
* @return
|
|
88 |
*/
|
|
89 |
// TODO: add test case for type descriptor
|
|
90 | 0 |
public String getTypeDescriptor() {
|
91 | 0 |
StringBuffer buf = new StringBuffer();
|
92 |
// buf.append(dynamicReturnType ? "$dynamic" : cleanupTypeName(returnType));
|
|
93 |
//
|
|
94 | 0 |
buf.append(ensureJavaTypeNameSyntax(returnType)); // br to replace the above. Dynamic type returns Object.
|
95 |
//
|
|
96 | 0 |
buf.append(' '); |
97 | 0 |
buf.append(name); |
98 | 0 |
buf.append('('); |
99 | 0 |
for (int i=0; i < parameters.length; i++) { |
100 | 0 |
if (i>0) buf.append(',');
|
101 | 0 |
Parameter param = parameters[i]; |
102 | 0 |
buf.append(ensureJavaTypeNameSyntax(param.getType())); |
103 |
} |
|
104 | 0 |
buf.append(')'); |
105 | 0 |
return buf.toString();
|
106 |
} |
|
107 |
|
|
108 | 0 |
public static String ensureJavaTypeNameSyntax(String typename) { |
109 |
// if the typename begins with "[", ends with ";", or is
|
|
110 |
// one character long, it's in .class syntax.
|
|
111 | 0 |
if (typename.charAt(0) == '[') {
|
112 | 0 |
return ensureJavaTypeNameSyntax(typename.substring(1)) + "[]"; |
113 |
} |
|
114 | 0 |
if (typename.length() == 1) {
|
115 | 0 |
switch (typename.charAt(0)) {
|
116 | 0 |
case 'B': return "byte"; |
117 | 0 |
case 'C': return "char"; |
118 | 0 |
case 'D': return "double"; |
119 | 0 |
case 'F': return "float"; |
120 | 0 |
case 'J': return "long"; |
121 | 0 |
case 'I': return "int"; |
122 | 0 |
case 'S': return "short"; |
123 | 0 |
case 'V': return "void"; |
124 | 0 |
case 'Z': return "boolean"; |
125 |
} |
|
126 |
} |
|
127 | 0 |
if (typename.endsWith(";")) { |
128 |
// Type should be "Lclassname;"
|
|
129 | 0 |
return typename.substring(1, typename.length() - 1);
|
130 |
} |
|
131 | 0 |
return typename;
|
132 |
|
|
133 |
} |
|
134 |
|
|
135 | 0 |
public boolean isVoidMethod() { |
136 | 0 |
return "void".equals(returnType); |
137 |
} |
|
138 |
|
|
139 | 0 |
public Statement getCode() {
|
140 | 0 |
return code;
|
141 |
} |
|
142 |
|
|
143 | 0 |
public void setCode(Statement code) { |
144 | 0 |
this.code = code;
|
145 |
} |
|
146 |
|
|
147 | 0 |
public int getModifiers() { |
148 | 0 |
return modifiers;
|
149 |
} |
|
150 |
|
|
151 | 0 |
public void setModifiers(int modifiers) { |
152 | 0 |
this.modifiers = modifiers;
|
153 |
} |
|
154 |
|
|
155 | 0 |
public String getName() {
|
156 | 0 |
return name;
|
157 |
} |
|
158 |
|
|
159 | 0 |
public Parameter[] getParameters() {
|
160 | 0 |
return parameters;
|
161 |
} |
|
162 |
|
|
163 | 0 |
public String getReturnType() {
|
164 | 0 |
return returnType;
|
165 |
} |
|
166 |
|
|
167 | 0 |
public VariableScope getVariableScope() {
|
168 | 0 |
return variableScope;
|
169 |
} |
|
170 |
|
|
171 | 0 |
public void setVariableScope(VariableScope variableScope) { |
172 | 0 |
this.variableScope = variableScope;
|
173 |
} |
|
174 |
|
|
175 | 0 |
public boolean isDynamicReturnType() { |
176 | 0 |
return dynamicReturnType;
|
177 |
} |
|
178 |
|
|
179 | 0 |
public ClassNode getDeclaringClass() {
|
180 | 0 |
return declaringClass;
|
181 |
} |
|
182 |
|
|
183 | 0 |
public boolean isAbstract() { |
184 | 0 |
return (modifiers & ACC_ABSTRACT) != 0;
|
185 |
} |
|
186 |
|
|
187 | 0 |
public boolean isStatic() { |
188 | 0 |
return (modifiers & ACC_STATIC) != 0;
|
189 |
} |
|
190 |
|
|
191 | 0 |
public String toString() {
|
192 | 0 |
return super.toString() + "[name: " + name + "]"; |
193 |
} |
|
194 |
|
|
195 | 0 |
public void setReturnType(String returnType) { |
196 | 0 |
this.returnType = returnType;
|
197 |
} |
|
198 |
/**
|
|
199 |
* @param declaringClass The declaringClass to set.
|
|
200 |
*/
|
|
201 | 0 |
public void setDeclaringClass(ClassNode declaringClass) { |
202 | 0 |
this.declaringClass = declaringClass;
|
203 |
} |
|
204 |
} |
|
205 |
|
|