KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHFormalParameters


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 BSHFormalParameters extends SimpleNode
37 {
38     private String JavaDoc [] paramNames;
39     /**
40         For loose type parameters the paramTypes are null.
41     */

42     // unsafe caching of types
43
Class JavaDoc [] paramTypes;
44     int numArgs;
45     String JavaDoc [] typeDescriptors;
46
47     BSHFormalParameters(int id) { super(id); }
48
49     void insureParsed()
50     {
51         if ( paramNames != null )
52             return;
53
54         this.numArgs = jjtGetNumChildren();
55         String JavaDoc [] paramNames = new String JavaDoc[numArgs];
56
57         for(int i=0; i<numArgs; i++)
58         {
59             BSHFormalParameter param = (BSHFormalParameter)jjtGetChild(i);
60             paramNames[i] = param.name;
61         }
62
63         this.paramNames = paramNames;
64     }
65
66     public String JavaDoc [] getParamNames() {
67         insureParsed();
68         return paramNames;
69     }
70
71     public String JavaDoc [] getTypeDescriptors(
72         CallStack callstack, Interpreter interpreter, String JavaDoc defaultPackage )
73     {
74         if ( typeDescriptors != null )
75             return typeDescriptors;
76
77         insureParsed();
78         String JavaDoc [] typeDesc = new String JavaDoc[numArgs];
79
80         for(int i=0; i<numArgs; i++)
81         {
82             BSHFormalParameter param = (BSHFormalParameter)jjtGetChild(i);
83             typeDesc[i] = param.getTypeDescriptor(
84                 callstack, interpreter, defaultPackage );
85         }
86
87         this.typeDescriptors = typeDesc;
88         return typeDesc;
89     }
90
91     /**
92         Evaluate the types.
93         Note that type resolution does not require the interpreter instance.
94     */

95     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter )
96         throws EvalError
97     {
98         if ( paramTypes != null )
99             return paramTypes;
100
101         insureParsed();
102         Class JavaDoc [] paramTypes = new Class JavaDoc[numArgs];
103
104         for(int i=0; i<numArgs; i++)
105         {
106             BSHFormalParameter param = (BSHFormalParameter)jjtGetChild(i);
107             paramTypes[i] = (Class JavaDoc)param.eval( callstack, interpreter );
108         }
109
110         this.paramTypes = paramTypes;
111
112         return paramTypes;
113     }
114 }
115
116
Popular Tags