You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BooleanMemberValue.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 2004 Bill Burke. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode.annotation;
  17. import javassist.ClassPool;
  18. import javassist.bytecode.ConstPool;
  19. import java.io.IOException;
  20. import java.lang.reflect.Method;
  21. /**
  22. * Boolean constant value.
  23. *
  24. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  25. * @author Shigeru Chiba
  26. */
  27. public class BooleanMemberValue extends MemberValue {
  28. int valueIndex;
  29. /**
  30. * Constructs a boolean constant value. The initial value is specified
  31. * by the constant pool entry at the given index.
  32. *
  33. * @param index the index of a CONSTANT_Integer_info structure.
  34. */
  35. public BooleanMemberValue(int index, ConstPool cp) {
  36. super('Z', cp);
  37. this.valueIndex = index;
  38. }
  39. /**
  40. * Constructs a boolean constant value.
  41. *
  42. * @param b the initial value.
  43. */
  44. public BooleanMemberValue(boolean b, ConstPool cp) {
  45. super('Z', cp);
  46. setValue(b);
  47. }
  48. /**
  49. * Constructs a boolean constant value. The initial value is false.
  50. */
  51. public BooleanMemberValue(ConstPool cp) {
  52. super('Z', cp);
  53. setValue(false);
  54. }
  55. Object getValue(ClassLoader cl, ClassPool cp, Method m) {
  56. return new Boolean(getValue());
  57. }
  58. Class getType(ClassLoader cl) {
  59. return boolean.class;
  60. }
  61. /**
  62. * Obtains the value of the member.
  63. */
  64. public boolean getValue() {
  65. return cp.getIntegerInfo(valueIndex) != 0;
  66. }
  67. /**
  68. * Sets the value of the member.
  69. */
  70. public void setValue(boolean newValue) {
  71. valueIndex = cp.addIntegerInfo(newValue ? 1 : 0);
  72. }
  73. /**
  74. * Obtains the string representation of this object.
  75. */
  76. public String toString() {
  77. return getValue() ? "true" : "false";
  78. }
  79. /**
  80. * Writes the value.
  81. */
  82. public void write(AnnotationsWriter writer) throws IOException {
  83. writer.constValueIndex(getValue());
  84. }
  85. /**
  86. * Accepts a visitor.
  87. */
  88. public void accept(MemberValueVisitor visitor) {
  89. visitor.visitBooleanMemberValue(this);
  90. }
  91. }