PageRenderTime 61ms CodeModel.GetById 51ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/BSHClassDeclaration.java

#
Java | 156 lines | 80 code | 21 blank | 55 comment | 6 complexity | 1c06a598849385bab1fa099bd6f7714c MD5 | raw file
  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
 35package bsh;
 36
 37/**
 38*/
 39class BSHClassDeclaration extends SimpleNode
 40{
 41	/**
 42		The class instance initializer method name.
 43		A BshMethod by this name is installed by the class delcaration into 
 44		the scripted class and it is called to initialize instances of the
 45		class.
 46	*/
 47	static final String CLASSINITNAME = "_bshClassInit";
 48
 49	String name;
 50	Modifiers modifiers;
 51	int numInterfaces;
 52	Class [] interfaces;
 53	boolean extend;
 54
 55	BSHClassDeclaration(int id) { super(id); }
 56
 57	/**
 58	*/
 59	public Object eval( CallStack callstack, Interpreter interpreter )
 60		throws EvalError
 61	{
 62		int child = 0;
 63		NameSpace enclosingNameSpace = callstack.top();
 64
 65		NameSpace superNameSpace;
 66		if ( extend ) 
 67		{
 68			BSHAmbiguousName superNode = (BSHAmbiguousName)jjtGetChild(child++);
 69			Object superClass = superNode.toObject( 
 70				callstack, interpreter, false /*forceclass*/ );
 71
 72			if ( superClass instanceof ClassIdentifier )
 73				throw new EvalError(
 74					"BeanShell Scripted Classes cannot currently extend "
 75					+"Java types.", this, callstack );
 76
 77			if ( ClassNameSpace.isScriptedClass( superClass ) )
 78				superNameSpace = ((This)superClass).getNameSpace();
 79			else
 80				throw new EvalError(
 81					"Class cannot extend type: "
 82						+superClass.getClass(), this, callstack );
 83		}
 84		else
 85			superNameSpace = enclosingNameSpace;
 86
 87		// Get interfaces
 88		interfaces = new Class[numInterfaces];
 89		for( int i=0; i<numInterfaces; i++) {
 90			BSHAmbiguousName node = (BSHAmbiguousName)jjtGetChild(child++);
 91			interfaces[i] = node.toClass(callstack, interpreter);
 92			if ( !interfaces[i].isInterface() )
 93				throw new EvalError(
 94					"Type: "+node.text+" is not an interface!", 
 95					this, callstack );
 96		}
 97
 98		BSHBlock block;
 99		if ( child < jjtGetNumChildren() )
100			block = (BSHBlock)jjtGetChild(child);
101		else
102			block = new BSHBlock( ParserTreeConstants.JJTBLOCK );
103
104		/*
105			Install this class in the enclosingNameSpace.
106			Make a scripted object representing the class and install it under
107			the name of the class.
108		*/
109
110		NameSpace classStaticNameSpace = 
111			new ClassNameSpace( superNameSpace, name, ClassNameSpace.CLASS );
112
113		/*
114			Evaluate the block in the classStaticNameSpace
115			push it onto the stack and then call the block eval with the
116			overrideNamespace option so that it uses ours.  Then pop it.
117		*/
118		callstack.push( classStaticNameSpace );
119		// evaluate the static portion of the block in it
120		block.eval( callstack, interpreter, true/*override*/ );
121
122		// Add the block as a default constructor method
123
124		BshMethod classInitializer =
125			new BshMethod( 
126				CLASSINITNAME, 
127				null /*returnType*/,
128				new String [0] /*argNames*/,
129				new Class [0] /*argTypes*/,
130				block,
131				classStaticNameSpace/*declaringNameSpace*/,
132				new Modifiers()
133			);
134
135		try {
136			classStaticNameSpace.setMethod( CLASSINITNAME, classInitializer );	
137		} catch ( UtilEvalError e ) {
138			throw e.toEvalError(this, callstack);
139		}
140		
141		callstack.pop();
142
143		try {
144			enclosingNameSpace.setVariable( 
145				name, classStaticNameSpace.getThis( interpreter), false );
146		} catch ( UtilEvalError e ) {
147			throw e.toEvalError( this, callstack );
148		}
149
150		return Primitive.VOID;
151	}
152
153	public String toString() {
154		return "ClassDeclaration: "+name;
155	}
156}