PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package bsh;
  2. public class Variable implements java.io.Serializable
  3. {
  4. static final int DECLARATION=0, ASSIGNMENT=1;
  5. /** A null type means an untyped variable */
  6. String name;
  7. Class type = null;
  8. String typeDescriptor;
  9. Object value;
  10. Modifiers modifiers;
  11. LHS lhs;
  12. Variable( String name, Class type, LHS lhs )
  13. {
  14. this.name = name;
  15. this.lhs = lhs;
  16. this.type = type;
  17. }
  18. Variable( String name, Object value, Modifiers modifiers )
  19. throws UtilEvalError
  20. {
  21. this( name, (Class)null/*type*/, value, modifiers );
  22. }
  23. /**
  24. This constructor is used in class generation.
  25. */
  26. Variable(
  27. String name, String typeDescriptor, Object value, Modifiers modifiers
  28. )
  29. throws UtilEvalError
  30. {
  31. this( name, (Class)null/*type*/, value, modifiers );
  32. this.typeDescriptor = typeDescriptor;
  33. }
  34. /**
  35. @param value may be null if this
  36. */
  37. Variable( String name, Class type, Object value, Modifiers modifiers )
  38. throws UtilEvalError
  39. {
  40. this.name=name;
  41. this.type = type;
  42. this.modifiers = modifiers;
  43. setValue( value, DECLARATION );
  44. }
  45. /**
  46. Set the value of the typed variable.
  47. @param value should be an object or wrapped bsh Primitive type.
  48. if value is null the appropriate default value will be set for the
  49. type: e.g. false for boolean, zero for integer types.
  50. */
  51. public void setValue( Object value, int context )
  52. throws UtilEvalError
  53. {
  54. // check this.value
  55. if ( hasModifier("final") && this.value != null )
  56. throw new UtilEvalError ("Final variable, can't re-assign.");
  57. if ( value == null )
  58. value = Primitive.getDefaultValue( type );
  59. if ( lhs != null )
  60. {
  61. lhs.assign( value, false/*strictjava*/ );
  62. return;
  63. }
  64. if ( type != null )
  65. value = Types.castObject( value, type,
  66. context == DECLARATION ? Types.CAST : Types.ASSIGNMENT
  67. );
  68. this.value= value;
  69. }
  70. Object getValue()
  71. throws UtilEvalError
  72. {
  73. if ( lhs != null )
  74. return lhs.getValue();
  75. return value;
  76. }
  77. /** A type of null means loosely typed variable */
  78. public Class getType() { return type; }
  79. public String getTypeDescriptor() { return typeDescriptor; }
  80. public Modifiers getModifiers() { return modifiers; }
  81. public String getName() { return name; }
  82. public boolean hasModifier( String name ) {
  83. return modifiers != null && modifiers.hasModifier(name);
  84. }
  85. public String toString() {
  86. return "Variable: "+super.toString()+" "+name+", type:"+type
  87. +", value:"+value +", lhs = "+lhs;
  88. }
  89. }