1 package bsh; 2 3 public class Variable implements java.io.Serializable 4 { 5 static final int DECLARATION=0, ASSIGNMENT=1; 6 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, value, modifiers ); 25 } 26 27 30 Variable( 31 String name, String typeDescriptor, Object value, Modifiers modifiers 32 ) 33 throws UtilEvalError 34 { 35 this( name, (Class )null, value, modifiers ); 36 this.typeDescriptor = typeDescriptor; 37 } 38 39 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 58 public void setValue( Object value, int context ) 59 throws UtilEvalError 60 { 61 62 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 ); 72 return; 73 } 74 75 if ( type != null ) 78 value = Types.castObject( value, type, 79 context == DECLARATION ? Types.CAST : Types.ASSIGNMENT 80 ); 81 82 this.value= value; 83 } 84 85 90 Object getValue() 91 throws UtilEvalError 92 { 93 if ( lhs != null ) 94 return lhs.getValue(); 95 96 return value; 97 } 98 99 100 public Class getType() { return type; } 101 102 public String getTypeDescriptor() { return typeDescriptor; } 103 104 public Modifiers getModifiers() { return modifiers; } 105 106 public String getName() { return name; } 107 108 public boolean hasModifier( String name ) { 109 return modifiers != null && modifiers.hasModifier(name); 110 } 111 112 public String toString() { 113 return "Variable: "+super.toString()+" "+name+", type:"+type 114 +", value:"+value +", lhs = "+lhs; 115 } 116 } 117 | Popular Tags |