KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > Variable


1 package bsh;
2
3 public class Variable implements java.io.Serializable JavaDoc
4 {
5     static final int DECLARATION=0, ASSIGNMENT=1;
6     /** A null type means an untyped variable */
7     String JavaDoc name;
8     Class JavaDoc type = null;
9     String JavaDoc typeDescriptor;
10     Object JavaDoc value;
11     Modifiers modifiers;
12     LHS lhs;
13
14     Variable( String JavaDoc name, Class JavaDoc type, LHS lhs )
15     {
16         this.name = name;
17         this.lhs = lhs;
18         this.type = type;
19     }
20     
21     Variable( String JavaDoc name, Object JavaDoc value, Modifiers modifiers )
22         throws UtilEvalError
23     {
24         this( name, (Class JavaDoc)null/*type*/, value, modifiers );
25     }
26
27     /**
28         This constructor is used in class generation.
29     */

30     Variable(
31         String JavaDoc name, String JavaDoc typeDescriptor, Object JavaDoc value, Modifiers modifiers
32     )
33         throws UtilEvalError
34     {
35         this( name, (Class JavaDoc)null/*type*/, value, modifiers );
36         this.typeDescriptor = typeDescriptor;
37     }
38
39     /**
40         @param value may be null if this
41     */

42     Variable( String JavaDoc name, Class JavaDoc type, Object JavaDoc 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 JavaDoc 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         // TODO: should add isJavaCastable() test for strictJava
76
// (as opposed to isJavaAssignable())
77
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     /*
86         Note: UtilEvalError here comes from lhs.getValue().
87         A Variable can represent an LHS for the case of an imported class or
88         object field.
89     */

90     Object JavaDoc getValue()
91         throws UtilEvalError
92     {
93         if ( lhs != null )
94             return lhs.getValue();
95
96         return value;
97     }
98
99     /** A type of null means loosely typed variable */
100     public Class JavaDoc getType() { return type; }
101
102     public String JavaDoc getTypeDescriptor() { return typeDescriptor; }
103
104     public Modifiers getModifiers() { return modifiers; }
105
106     public String JavaDoc getName() { return name; }
107
108     public boolean hasModifier( String JavaDoc name ) {
109         return modifiers != null && modifiers.hasModifier(name);
110     }
111
112     public String JavaDoc toString() {
113         return "Variable: "+super.toString()+" "+name+", type:"+type
114             +", value:"+value +", lhs = "+lhs;
115     }
116 }
117
Popular Tags