001    /*
002     $Id: MethodNode.java,v 1.16 2005/05/27 10:13:08 russel Exp $
003    
004     Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005    
006     Redistribution and use of this software and associated documentation
007     ("Software"), with or without modification, are permitted provided
008     that the following conditions are met:
009    
010     1. Redistributions of source code must retain copyright
011        statements and notices.  Redistributions must also contain a
012        copy of this document.
013    
014     2. Redistributions in binary form must reproduce the
015        above copyright notice, this list of conditions and the
016        following disclaimer in the documentation and/or other
017        materials provided with the distribution.
018    
019     3. The name "groovy" must not be used to endorse or promote
020        products derived from this Software without prior written
021        permission of The Codehaus.  For written permission,
022        please contact info@codehaus.org.
023    
024     4. Products derived from this Software may not be called "groovy"
025        nor may "groovy" appear in their names without prior written
026        permission of The Codehaus. "groovy" is a registered
027        trademark of The Codehaus.
028    
029     5. Due credit should be given to The Codehaus -
030        http://groovy.codehaus.org/
031    
032     THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033     ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034     NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
036     THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043     OF THE POSSIBILITY OF SUCH DAMAGE.
044    
045     */
046    package org.codehaus.groovy.ast;
047    
048    import org.codehaus.groovy.ast.stmt.Statement;
049    import org.codehaus.groovy.classgen.VariableScopeCodeVisitor;
050    import org.objectweb.asm.Opcodes;
051    
052    /**
053     * Represents a method declaration
054     *
055     * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
056     * @version $Revision: 1.16 $
057     */
058    public class MethodNode extends AnnotatedNode implements Opcodes {
059    
060        private String name;
061        private int modifiers;
062        private String returnType;
063        private Parameter[] parameters;
064        private Statement code;
065        private boolean dynamicReturnType;
066        private VariableScope variableScope;
067    
068        public MethodNode(String name, int modifiers, String returnType, Parameter[] parameters, Statement code) {
069            this.name = name;
070            this.modifiers = modifiers;
071            this.parameters = parameters;
072            this.code = code;
073    
074            if (returnType == null || returnType.length() == 0) {
075                this.returnType = "java.lang.Object";
076                this.dynamicReturnType = true;
077            }
078            else {
079                this.returnType = ensureJavaTypeNameSyntax(returnType);
080            }
081        }
082    
083        /**
084         * The type descriptor for a method node is a string containing the name of the method, its return type,
085         * and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
086         * without parameter names, and with $dynamic as the type for any dynamically typed values.
087         *
088         * @return
089         */
090        // TODO: add test case for type descriptor
091        public String getTypeDescriptor() {
092            StringBuffer buf = new StringBuffer();
093            // buf.append(dynamicReturnType ? "$dynamic" : cleanupTypeName(returnType));
094            //
095            buf.append(ensureJavaTypeNameSyntax(returnType)); // br  to replace the above. Dynamic type returns Object.
096            //
097            buf.append(' ');
098            buf.append(name);
099            buf.append('(');
100            for (int i = 0; i < parameters.length; i++) {
101                if (i > 0) {
102                    buf.append(',');
103                }
104                Parameter param = parameters[i];
105                buf.append(ensureJavaTypeNameSyntax(param.getType()));
106            }
107            buf.append(')');
108            return buf.toString();
109        }
110    
111        public static String ensureJavaTypeNameSyntax(String typename) {
112            // if the typename begins with "[", ends with ";", or is
113            // one character long, it's in .class syntax.
114            if (typename.charAt(0) == '[') {
115                return ensureJavaTypeNameSyntax(typename.substring(1)) + "[]";
116            }
117            if (typename.length() == 1) {
118                switch (typename.charAt(0)) {
119                    case 'B':
120                        return "byte";
121                    case 'C':
122                        return "char";
123                    case 'D':
124                        return "double";
125                    case 'F':
126                        return "float";
127                    case 'J':
128                        return "long";
129                    case 'I':
130                        return "int";
131                    case 'S':
132                        return "short";
133                    case 'V':
134                        return "void";
135                    case 'Z':
136                        return "boolean";
137                }
138            }
139            if (typename.endsWith(";")) {
140                // Type should be "Lclassname;"
141                return typename.substring(1, typename.length() - 1);
142            }
143            return typename;
144    
145        }
146    
147        public boolean isVoidMethod() {
148            return "void".equals(returnType);
149        }
150    
151        public Statement getCode() {
152            return code;
153        }
154    
155        public void setCode(Statement code) {
156            this.code = code;
157        }
158    
159        public int getModifiers() {
160            return modifiers;
161        }
162    
163        public void setModifiers(int modifiers) {
164            this.modifiers = modifiers;
165        }
166    
167        public String getName() {
168            return name;
169        }
170    
171        public Parameter[] getParameters() {
172            return parameters;
173        }
174    
175        public String getReturnType() {
176            return returnType;
177        }
178    
179        public VariableScope getVariableScope() {
180            if (variableScope == null) {
181                variableScope = createVariableScope();
182            }
183            return variableScope;
184        }
185    
186        public void setVariableScope(VariableScope variableScope) {
187            this.variableScope = variableScope;
188        }
189    
190        public boolean isDynamicReturnType() {
191            return dynamicReturnType;
192        }
193    
194        public boolean isAbstract() {
195            return (modifiers & ACC_ABSTRACT) != 0;
196        }
197    
198        public boolean isStatic() {
199            return (modifiers & ACC_STATIC) != 0;
200        }
201    
202        public String toString() {
203            return super.toString() + "[name: " + name + "]";
204        }
205    
206        public void setReturnType(String returnType) {
207            this.returnType = returnType;
208        }
209    
210    
211        protected VariableScope createVariableScope() {
212            VariableScope variableScope = new VariableScope();
213            VariableScopeCodeVisitor visitor = new VariableScopeCodeVisitor(variableScope);
214            visitor.setParameters(getParameters());
215            Statement code = getCode();
216            if (code != null) {
217                code.visit(visitor);
218            }
219            addFieldsToVisitor(variableScope);
220            return variableScope;
221        }
222    }