KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHTypedVariableDeclaration


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 BSHTypedVariableDeclaration extends SimpleNode
37 {
38     public Modifiers modifiers;
39     
40     BSHTypedVariableDeclaration(int id) { super(id); }
41
42     private BSHType getTypeNode() {
43         return ((BSHType)jjtGetChild(0));
44     }
45
46     Class JavaDoc evalType( CallStack callstack, Interpreter interpreter )
47         throws EvalError
48     {
49         BSHType typeNode = getTypeNode();
50         return typeNode.getType( callstack, interpreter );
51     }
52
53     BSHVariableDeclarator [] getDeclarators()
54     {
55         int n = jjtGetNumChildren();
56         int start=1;
57         BSHVariableDeclarator [] bvda = new BSHVariableDeclarator[ n-start ];
58         for (int i = start; i < n; i++)
59         {
60             bvda[i-start] = (BSHVariableDeclarator)jjtGetChild(i);
61         }
62         return bvda;
63     }
64
65     /**
66         evaluate the type and one or more variable declarators, e.g.:
67             int a, b=5, c;
68     */

69     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter)
70         throws EvalError
71     {
72         try {
73             NameSpace namespace = callstack.top();
74             BSHType typeNode = getTypeNode();
75             Class JavaDoc type = typeNode.getType( callstack, interpreter );
76
77             BSHVariableDeclarator [] bvda = getDeclarators();
78             for (int i = 0; i < bvda.length; i++)
79             {
80                 BSHVariableDeclarator dec = bvda[i];
81
82                 // Type node is passed down the chain for array initializers
83
// which need it under some circumstances
84
Object JavaDoc value = dec.eval( typeNode, callstack, interpreter);
85
86                 try {
87                     namespace.setTypedVariable(
88                         dec.name, type, value, modifiers );
89                 } catch ( UtilEvalError e ) {
90                     throw e.toEvalError( this, callstack );
91                 }
92             }
93         } catch ( EvalError e ) {
94             e.reThrow( "Typed variable declaration" );
95         }
96
97         return Primitive.VOID;
98     }
99
100     public String JavaDoc getTypeDescriptor(
101         CallStack callstack, Interpreter interpreter, String JavaDoc defaultPackage )
102     {
103         return getTypeNode().getTypeDescriptor(
104             callstack, interpreter, defaultPackage );
105     }
106 }
107
Popular Tags