PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/Variable.java

#
Java | 109 lines | 74 code | 20 blank | 15 comment | 14 complexity | 6ae6aa20d511d02c3655fcdaaf8c667b MD5 | raw file
  1package bsh;
  2
  3public class Variable implements java.io.Serializable 
  4{
  5	static final int DECLARATION=0, ASSIGNMENT=1;
  6	/** A null type means an untyped variable */
  7	String name;
  8	Class type = null;
  9	String typeDescriptor;
 10	Object value;
 11	Modifiers modifiers;
 12	LHS lhs;
 13
 14	Variable( String name, Class type, LHS lhs ) 
 15	{
 16		this.name = name;
 17		this.lhs = lhs;
 18		this.type = type;
 19	}
 20	
 21	Variable( String name, Object value, Modifiers modifiers )
 22		throws UtilEvalError
 23	{
 24		this( name, (Class)null/*type*/, value, modifiers );
 25	}
 26
 27	/**
 28		This constructor is used in class generation.
 29	*/
 30	Variable( 
 31		String name, String typeDescriptor, Object value, Modifiers modifiers 
 32	)
 33		throws UtilEvalError
 34	{
 35		this( name, (Class)null/*type*/, value, modifiers );
 36		this.typeDescriptor = typeDescriptor;
 37	}
 38
 39	/**
 40		@param value may be null if this 
 41	*/
 42	Variable( String name, Class type, Object value, Modifiers modifiers )
 43		throws UtilEvalError
 44	{
 45
 46		this.name=name;
 47		this.type =	type;
 48		this.modifiers = modifiers;
 49		setValue( value, DECLARATION );
 50	}
 51
 52	/**
 53		Set the value of the typed variable.
 54		@param value should be an object or wrapped bsh Primitive type.
 55		if value is null the appropriate default value will be set for the
 56		type: e.g. false for boolean, zero for integer types.
 57	*/
 58	public void setValue( Object value, int context ) 
 59		throws UtilEvalError
 60	{
 61
 62		// check this.value
 63		if ( hasModifier("final") && this.value != null )
 64			throw new UtilEvalError ("Final variable, can't re-assign.");
 65
 66		if ( value == null )
 67			value = Primitive.getDefaultValue( type );
 68
 69		if ( lhs != null )
 70		{
 71			lhs.assign( value, false/*strictjava*/ );
 72			return;
 73		}
 74
 75		if ( type != null )
 76			value = Types.castObject( value, type, 
 77				context == DECLARATION ? Types.CAST : Types.ASSIGNMENT
 78			);
 79
 80		this.value= value;
 81	}
 82
 83	Object getValue() 
 84		throws UtilEvalError
 85	{ 
 86		if ( lhs != null )
 87			return lhs.getValue();
 88
 89		return value; 
 90	}
 91
 92	/** A type of null means loosely typed variable */
 93	public Class getType() { return type;	}
 94
 95	public String getTypeDescriptor() { return typeDescriptor; }
 96
 97	public Modifiers getModifiers() { return modifiers; }
 98
 99	public String getName() { return name; }
100
101	public boolean hasModifier( String name ) {
102		return modifiers != null && modifiers.hasModifier(name);
103	}
104
105	public String toString() { 
106		return "Variable: "+super.toString()+" "+name+", type:"+type
107			+", value:"+value +", lhs = "+lhs;
108	}
109}