PageRenderTime 416ms CodeModel.GetById 407ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/BSHType.java

#
Java | 127 lines | 48 code | 14 blank | 65 comment | 5 complexity | b01f953a3dfa8a51b22fb79c3db4be83 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
 37import java.lang.reflect.Array;
 38
 39class BSHType extends SimpleNode 
 40	implements BshClassManager.Listener
 41{
 42	/**
 43		baseType is used during evaluation of full type and retained for the
 44		case where we are an array type.
 45		In the case where we are not an array this will be the same as type.
 46	*/
 47	private Class baseType;
 48	/** 
 49		If we are an array type this will be non zero and indicate the 
 50		dimensionality of the array.  e.g. 2 for String[][];
 51	*/
 52    private int arrayDims;
 53
 54	/** 
 55		Internal cache of the fully expressed type. 
 56		i.e. primtive, class, or array.  Cleared on classloader change.
 57	*/
 58    private Class type;
 59
 60    BSHType(int id) { 
 61		super(id); 
 62		BshClassManager.addCMListener(this);
 63	}
 64
 65	/**
 66		Used by the grammar to indicate dimensions of array types 
 67		during parsing.
 68	*/
 69    public void addArrayDimension() { 
 70		arrayDims++; 
 71	}
 72
 73    /**
 74		 Returns a class for the type
 75	*/
 76    public Class getType( NameSpace namespace ) 
 77		throws EvalError
 78    {
 79        // return cached type if available
 80		if (type != null)
 81			return type;
 82
 83        //  first node will either be PrimitiveType or AmbiguousName
 84        SimpleNode node = (SimpleNode)jjtGetChild(0);
 85
 86        if(node instanceof BSHPrimitiveType)
 87            baseType = ((BSHPrimitiveType)node).getType();
 88        else 
 89            baseType = ((BSHAmbiguousName)node).toClass( namespace );
 90
 91        if(arrayDims > 0) {
 92            try {
 93                // Get the type by constructing a prototype array with
 94				// arbitrary (zero) length in each dimension.
 95                int[] dims = new int[arrayDims]; // int array default zeros
 96                Object obj = Array.newInstance(baseType, dims);
 97                type = obj.getClass(); 
 98            } catch(Exception e) {
 99                throw new EvalError("Couldn't construct array type", this);
100            }
101        } else
102            type = baseType;
103
104        return type;
105    }
106
107	/**
108		baseType is used during evaluation of full type and retained for the
109		case where we are an array type.
110		In the case where we are not an array this will be the same as type.
111	*/
112	public Class getBaseType() {
113		return baseType;
114	}
115	/** 
116		If we are an array type this will be non zero and indicate the 
117		dimensionality of the array.  e.g. 2 for String[][];
118	*/
119	public int getArrayDims() {
120		return arrayDims;
121	}
122
123	public void classLoaderChanged() {
124		type = null;
125		baseType = null;
126	}
127}