/src/main/java/com/alibaba/fastjson/asm/Item.java

https://github.com/alibaba/fastjson · Java · 177 lines · 68 code · 16 blank · 93 comment · 7 complexity · 97b831b4abcd5742eeb556810bb900f1 MD5 · raw file

  1. /***
  2. * ASM: a very small and fast Java bytecode manipulation framework
  3. * Copyright (c) 2000-2007 INRIA, France Telecom
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  28. * THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.alibaba.fastjson.asm;
  31. /**
  32. * A constant pool item. Constant pool items can be created with the 'newXXX' methods in the {@link ClassWriter} class.
  33. *
  34. * @author Eric Bruneton
  35. */
  36. final class Item {
  37. /**
  38. * Index of this item in the constant pool.
  39. */
  40. int index;
  41. int type;
  42. /**
  43. * Value of this item, for an integer item.
  44. */
  45. int intVal;
  46. /**
  47. * Value of this item, for a long item.
  48. */
  49. long longVal;
  50. /**
  51. * First part of the value of this item, for items that do not hold a primitive value.
  52. */
  53. String strVal1;
  54. /**
  55. * Second part of the value of this item, for items that do not hold a primitive value.
  56. */
  57. String strVal2;
  58. /**
  59. * Third part of the value of this item, for items that do not hold a primitive value.
  60. */
  61. String strVal3;
  62. /**
  63. * The hash code value of this constant pool item.
  64. */
  65. int hashCode;
  66. /**
  67. * Link to another constant pool item, used for collision lists in the constant pool's hash table.
  68. */
  69. Item next;
  70. /**
  71. * Constructs an uninitialized {@link Item}.
  72. */
  73. Item(){
  74. }
  75. /**
  76. * Constructs a copy of the given item.
  77. *
  78. * @param index index of the item to be constructed.
  79. * @param i the item that must be copied into the item to be constructed.
  80. */
  81. Item(final int index, final Item i){
  82. this.index = index;
  83. type = i.type;
  84. intVal = i.intVal;
  85. longVal = i.longVal;
  86. strVal1 = i.strVal1;
  87. strVal2 = i.strVal2;
  88. strVal3 = i.strVal3;
  89. hashCode = i.hashCode;
  90. }
  91. /**
  92. * Sets this item to an item that do not hold a primitive value.
  93. *
  94. * @param type the type of this item.
  95. * @param strVal1 first part of the value of this item.
  96. * @param strVal2 second part of the value of this item.
  97. * @param strVal3 third part of the value of this item.
  98. */
  99. void set(final int type, final String strVal1, final String strVal2, final String strVal3) {
  100. this.type = type;
  101. this.strVal1 = strVal1;
  102. this.strVal2 = strVal2;
  103. this.strVal3 = strVal3;
  104. switch (type) {
  105. case 1 /* ClassWriter.UTF8 */:
  106. case 8 /* ClassWriter.STR */:
  107. case 7 /* ClassWriter.CLASS */:
  108. case 13 /* ClassWriter.TYPE_NORMAL */:
  109. hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
  110. return;
  111. case 12 /* ClassWriter.NAME_TYPE */:
  112. hashCode = 0x7FFFFFFF & (type + strVal1.hashCode() * strVal2.hashCode());
  113. return;
  114. // ClassWriter.FIELD:
  115. // ClassWriter.METH:
  116. // ClassWriter.IMETH:
  117. default:
  118. hashCode = 0x7FFFFFFF & (type + strVal1.hashCode() * strVal2.hashCode() * strVal3.hashCode());
  119. }
  120. }
  121. /**
  122. * Sets this item to an integer item.
  123. *
  124. * @param intVal the value of this item.
  125. */
  126. void set(final int intVal) {
  127. this.type = 3 /* ClassWriter.INT */;
  128. this.intVal = intVal;
  129. this.hashCode = 0x7FFFFFFF & (type + intVal);
  130. }
  131. /**
  132. * Indicates if the given item is equal to this one. <i>This method assumes that the two items have the same
  133. * {@link #type}</i>.
  134. *
  135. * @param i the item to be compared to this one. Both items must have the same {@link #type}.
  136. * @return <tt>true</tt> if the given item if equal to this one, <tt>false</tt> otherwise.
  137. */
  138. boolean isEqualTo(final Item i) {
  139. switch (type) {
  140. case 1 /* ClassWriter.UTF8 */:
  141. case 8 /* ClassWriter.STR */:
  142. case 7 /* ClassWriter.CLASS */ :
  143. case 13 /* ClassWriter.TYPE_NORMAL */ :
  144. return i.strVal1.equals(strVal1);
  145. case 15 /* ClassWriter.TYPE_MERGED */ :
  146. case 5 /* ClassWriter.LONG */ :
  147. case 6 /* ClassWriter.DOUBLE */:
  148. return i.longVal == longVal;
  149. case 3 /* ClassWriter.INT */ :
  150. case 4 /* ClassWriter.FLOAT */:
  151. return i.intVal == intVal;
  152. case 12 /* ClassWriter.NAME_TYPE */:
  153. return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2);
  154. // case ClassWriter.FIELD:
  155. // case ClassWriter.METH:
  156. // case ClassWriter.IMETH:
  157. default:
  158. return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2) && i.strVal3.equals(strVal3);
  159. }
  160. }
  161. }