KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > BSHClassDeclaration


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 /**
37 */

38 class BSHClassDeclaration extends SimpleNode
39 {
40     /**
41         The class instance initializer method name.
42         A BshMethod by this name is installed by the class delcaration into
43         the static class body namespace.
44         It is called once to initialize the static members of the class space
45         and each time an instances is created to initialize the instance
46         members.
47     */

48     static final String JavaDoc CLASSINITNAME = "_bshClassInit";
49
50     String JavaDoc name;
51     Modifiers modifiers;
52     int numInterfaces;
53     boolean extend;
54     boolean isInterface;
55
56     BSHClassDeclaration(int id) { super(id); }
57
58     /**
59     */

60     public Object JavaDoc eval( CallStack callstack, Interpreter interpreter )
61         throws EvalError
62     {
63         int child = 0;
64
65         // resolve superclass if any
66
Class JavaDoc superClass = null;
67         if ( extend )
68         {
69             BSHAmbiguousName superNode = (BSHAmbiguousName)jjtGetChild(child++);
70             superClass = superNode.toClass( callstack, interpreter );
71         }
72
73         // Get interfaces
74
Class JavaDoc [] interfaces = new Class JavaDoc[numInterfaces];
75         for( int i=0; i<numInterfaces; i++) {
76             BSHAmbiguousName node = (BSHAmbiguousName)jjtGetChild(child++);
77             interfaces[i] = node.toClass(callstack, interpreter);
78             if ( !interfaces[i].isInterface() )
79                 throw new EvalError(
80                     "Type: "+node.text+" is not an interface!",
81                     this, callstack );
82         }
83
84         BSHBlock block;
85         // Get the class body BSHBlock
86
if ( child < jjtGetNumChildren() )
87             block = (BSHBlock)jjtGetChild(child);
88         else
89             block = new BSHBlock( ParserTreeConstants.JJTBLOCK );
90
91         try {
92             return ClassGenerator.getClassGenerator().generateClass(
93                 name, modifiers, interfaces, superClass, block, isInterface,
94                 callstack, interpreter );
95         } catch ( UtilEvalError e ) {
96             throw e.toEvalError( this, callstack );
97         }
98
99     }
100
101     public String JavaDoc toString() {
102         return "ClassDeclaration: "+name;
103     }
104 }
105
Popular Tags