001    package bsh;
002    
003    public class Variable implements java.io.Serializable 
004    {
005            static final int DECLARATION=0, ASSIGNMENT=1;
006            /** A null type means an untyped variable */
007            String name;
008            Class type = null;
009            String typeDescriptor;
010            Object value;
011            Modifiers modifiers;
012            LHS lhs;
013    
014            Variable( String name, Class type, LHS lhs ) 
015            {
016                    this.name = name;
017                    this.lhs = lhs;
018                    this.type = type;
019            }
020            
021            Variable( String name, Object value, Modifiers modifiers )
022                    throws UtilEvalError
023            {
024                    this( name, (Class)null/*type*/, value, modifiers );
025            }
026    
027            /**
028                    This constructor is used in class generation.
029            */
030            Variable( 
031                    String name, String typeDescriptor, Object value, Modifiers modifiers 
032            )
033                    throws UtilEvalError
034            {
035                    this( name, (Class)null/*type*/, value, modifiers );
036                    this.typeDescriptor = typeDescriptor;
037            }
038    
039            /**
040                    @param value may be null if this 
041            */
042            Variable( String name, Class type, Object value, Modifiers modifiers )
043                    throws UtilEvalError
044            {
045    
046                    this.name=name;
047                    this.type =     type;
048                    this.modifiers = modifiers;
049                    setValue( value, DECLARATION );
050            }
051    
052            /**
053                    Set the value of the typed variable.
054                    @param value should be an object or wrapped bsh Primitive type.
055                    if value is null the appropriate default value will be set for the
056                    type: e.g. false for boolean, zero for integer types.
057            */
058            public void setValue( Object value, int context ) 
059                    throws UtilEvalError
060            {
061    
062                    // check this.value
063                    if ( hasModifier("final") && this.value != null )
064                            throw new UtilEvalError ("Final variable, can't re-assign.");
065    
066                    if ( value == null )
067                            value = Primitive.getDefaultValue( type );
068    
069                    if ( lhs != null )
070                    {
071                            lhs.assign( value, false/*strictjava*/ );
072                            return;
073                    }
074    
075                    // TODO: should add isJavaCastable() test for strictJava
076                    // (as opposed to isJavaAssignable())
077                    if ( type != null )
078                            value = Types.castObject( value, type, 
079                                    context == DECLARATION ? Types.CAST : Types.ASSIGNMENT
080                            );
081    
082                    this.value= value;
083            }
084    
085            /*
086                    Note: UtilEvalError here comes from lhs.getValue().
087                    A Variable can represent an LHS for the case of an imported class or
088                    object field.
089            */
090            Object getValue() 
091                    throws UtilEvalError
092            { 
093                    if ( lhs != null )
094                            return lhs.getValue();
095    
096                    return value; 
097            }
098    
099            /** A type of null means loosely typed variable */
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    }