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.

Conversions.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.runtime.internal;
  14. public final class Conversions {
  15. // Can't make instances of me
  16. private Conversions() {}
  17. // we might want to keep a cache of small integers around
  18. public static Object intObject(int i) {
  19. return new Integer(i);
  20. }
  21. public static Object shortObject(short i) {
  22. return new Short(i);
  23. }
  24. public static Object byteObject(byte i) {
  25. return new Byte(i);
  26. }
  27. public static Object charObject(char i) {
  28. return new Character(i);
  29. }
  30. public static Object longObject(long i) {
  31. return new Long(i);
  32. }
  33. public static Object floatObject(float i) {
  34. return new Float(i);
  35. }
  36. public static Object doubleObject(double i) {
  37. return new Double(i);
  38. }
  39. public static Object booleanObject(boolean i) {
  40. return new Boolean(i);
  41. }
  42. public static Object voidObject() {
  43. return null;
  44. }
  45. public static int intValue(Object o) {
  46. if (o == null) {
  47. return 0;
  48. } else if (o instanceof Number) {
  49. return ((Number)o).intValue();
  50. } else {
  51. throw new ClassCastException(o.getClass().getName() +
  52. " can not be converted to int");
  53. }
  54. }
  55. public static long longValue(Object o) {
  56. if (o == null) {
  57. return 0;
  58. } else if (o instanceof Number) {
  59. return ((Number)o).longValue();
  60. } else {
  61. throw new ClassCastException(o.getClass().getName() +
  62. " can not be converted to long");
  63. }
  64. }
  65. public static float floatValue(Object o) {
  66. if (o == null) {
  67. return 0;
  68. } else if (o instanceof Number) {
  69. return ((Number)o).floatValue();
  70. } else {
  71. throw new ClassCastException(o.getClass().getName() +
  72. " can not be converted to float");
  73. }
  74. }
  75. public static double doubleValue(Object o) {
  76. if (o == null) {
  77. return 0;
  78. } else if (o instanceof Number) {
  79. return ((Number)o).doubleValue();
  80. } else {
  81. throw new ClassCastException(o.getClass().getName() +
  82. " can not be converted to double");
  83. }
  84. }
  85. public static byte byteValue(Object o) {
  86. if (o == null) {
  87. return 0;
  88. } else if (o instanceof Number) {
  89. return ((Number)o).byteValue();
  90. } else {
  91. throw new ClassCastException(o.getClass().getName() +
  92. " can not be converted to byte");
  93. }
  94. }
  95. public static short shortValue(Object o) {
  96. if (o == null) {
  97. return 0;
  98. } else if (o instanceof Number) {
  99. return ((Number)o).shortValue();
  100. } else {
  101. throw new ClassCastException(o.getClass().getName() +
  102. " can not be converted to short");
  103. }
  104. }
  105. public static char charValue(Object o) {
  106. if (o == null) {
  107. return 0;
  108. } else if (o instanceof Character) {
  109. return ((Character)o).charValue();
  110. } else {
  111. throw new ClassCastException(o.getClass().getName() +
  112. " can not be converted to char");
  113. }
  114. }
  115. public static boolean booleanValue(Object o) {
  116. if (o == null) {
  117. return false;
  118. } else if (o instanceof Boolean) {
  119. return ((Boolean)o).booleanValue();
  120. } else {
  121. throw new ClassCastException(o.getClass().getName() +
  122. " can not be converted to boolean");
  123. }
  124. }
  125. /**
  126. * identity function for now. This is not typed to "void" because we happen
  127. * to know that in Java, any void context (i.e., {@link ExprStmt})
  128. * can also handle a return value.
  129. */
  130. public static Object voidValue(Object o) {
  131. if (o == null) {
  132. return o;
  133. } else {
  134. // !!! this may be an error in the future
  135. return o;
  136. }
  137. }
  138. }