KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHMethodDeclaration


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34 package bsh;
35
36 class BSHMethodDeclaration extends SimpleNode
37 {
38     public String JavaDoc name;
39
40     // Begin Child node structure evaluated by insureNodesParsed
41

42     BSHReturnType returnTypeNode;
43     BSHFormalParameters paramsNode;
44     BSHBlock blockNode;
45     // index of the first throws clause child node
46
int firstThrowsClause;
47
48     // End Child node structure evaluated by insureNodesParsed
49

50     public Modifiers modifiers;
51
52     // Unsafe caching of type here.
53
Class JavaDoc returnType; // null (none), Void.TYPE, or a Class
54
int numThrows = 0;
55
56     BSHMethodDeclaration(int id) { super(id); }
57
58     /**
59         Set the returnTypeNode, paramsNode, and blockNode based on child
60         node structure. No evaluation is done here.
61     */

62     synchronized void insureNodesParsed()
63     {
64         if ( paramsNode != null ) // there is always a paramsNode
65
return;
66
67         Object JavaDoc firstNode = jjtGetChild(0);
68         firstThrowsClause = 1;
69         if ( firstNode instanceof BSHReturnType )
70         {
71             returnTypeNode = (BSHReturnType)firstNode;
72             paramsNode = (BSHFormalParameters)jjtGetChild(1);
73             if ( jjtGetNumChildren() > 2+numThrows )
74                 blockNode = (BSHBlock)jjtGetChild(2+numThrows); // skip throws
75
++firstThrowsClause;
76         }
77         else
78         {
79             paramsNode = (BSHFormalParameters)jjtGetChild(0);
80             blockNode = (BSHBlock)jjtGetChild(1+numThrows); // skip throws
81
}
82     }
83
84     /**
85         Evaluate the return type node.
86         @return the type or null indicating loosely typed return
87     */

88     Class JavaDoc evalReturnType( CallStack callstack, Interpreter interpreter )
89         throws EvalError
90     {
91         insureNodesParsed();
92         if ( returnTypeNode != null )
93             return returnTypeNode.evalReturnType( callstack, interpreter );
94         else
95             return null;
96     }
97
98     String JavaDoc getReturnTypeDescriptor(
99         CallStack callstack, Interpreter interpreter, String JavaDoc defaultPackage )
100     {
101         insureNodesParsed();
102         if ( returnTypeNode == null )
103             return null;
104         else
105             return returnTypeNode.getTypeDescriptor(
106                 callstack, interpreter, defaultPackage );
107     }
108
109     BSHReturnType getReturnTypeNode() {
110         insureNodesParsed();
111         return returnTypeNode;
112     }
113
114     /**
115         Evaluate the declaration of the method. That is, determine the
116         structure of the method and install it into the caller's namespace.
117     */

118     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter )
119         throws EvalError
120     {
121         returnType = evalReturnType( callstack, interpreter );
122         evalNodes( callstack, interpreter );
123
124         // Install an *instance* of this method in the namespace.
125
// See notes in BshMethod
126

127 // This is not good...
128
// need a way to update eval without re-installing...
129
// so that we can re-eval params, etc. when classloader changes
130
// look into this
131

132         NameSpace namespace = callstack.top();
133         BshMethod bshMethod = new BshMethod( this, namespace, modifiers );
134         try {
135             namespace.setMethod( name, bshMethod );
136         } catch ( UtilEvalError e ) {
137             throw e.toEvalError(this,callstack);
138         }
139
140         return Primitive.VOID;
141     }
142
143     private void evalNodes( CallStack callstack, Interpreter interpreter )
144         throws EvalError
145     {
146         insureNodesParsed();
147         
148         // validate that the throws names are class names
149
for(int i=firstThrowsClause; i<numThrows+firstThrowsClause; i++)
150             ((BSHAmbiguousName)jjtGetChild(i)).toClass(
151                 callstack, interpreter );
152
153         paramsNode.eval( callstack, interpreter );
154
155         // if strictJava mode, check for loose parameters and return type
156
if ( interpreter.getStrictJava() )
157         {
158             for(int i=0; i<paramsNode.paramTypes.length; i++)
159                 if ( paramsNode.paramTypes[i] == null )
160                     // Warning: Null callstack here. Don't think we need
161
// a stack trace to indicate how we sourced the method.
162
throw new EvalError(
163                 "(Strict Java Mode) Undeclared argument type, parameter: " +
164                     paramsNode.getParamNames()[i] + " in method: "
165                     + name, this, null );
166
167             if ( returnType == null )
168                 // Warning: Null callstack here. Don't think we need
169
// a stack trace to indicate how we sourced the method.
170
throw new EvalError(
171                 "(Strict Java Mode) Undeclared return type for method: "
172                     + name, this, null );
173         }
174     }
175
176     public String JavaDoc toString() {
177         return "MethodDeclaration: "+name;
178     }
179 }
180
Popular Tags