PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 189 lines | 70 code | 16 blank | 103 comment | 9 complexity | 064e2b5d3f153f72fff0f18618a7bded 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. /**
  42. * Type of this constant pool item. A single class is used to represent all constant pool item types, in order to
  43. * minimize the bytecode size of this package. The value of this field is one of {@link ClassWriter#INT},
  44. * {@link ClassWriter#LONG}, {@link ClassWriter#FLOAT}, {@link ClassWriter#DOUBLE}, {@link ClassWriter#UTF8},
  45. * {@link ClassWriter#STR}, {@link ClassWriter#CLASS}, {@link ClassWriter#NAME_TYPE}, {@link ClassWriter#FIELD},
  46. * {@link ClassWriter#METH}, {@link ClassWriter#IMETH}. Special Item types are used for Items that are stored in the
  47. * ClassWriter {@link ClassWriter#typeTable}, instead of the constant pool, in order to avoid clashes with normal
  48. * constant pool items in the ClassWriter constant pool's hash table. These special item types are
  49. * {@link ClassWriter#TYPE_NORMAL}, {@link ClassWriter#TYPE_UNINIT} and {@link ClassWriter#TYPE_MERGED}.
  50. */
  51. int type;
  52. /**
  53. * Value of this item, for an integer item.
  54. */
  55. int intVal;
  56. /**
  57. * Value of this item, for a long item.
  58. */
  59. long longVal;
  60. /**
  61. * First part of the value of this item, for items that do not hold a primitive value.
  62. */
  63. String strVal1;
  64. /**
  65. * Second part of the value of this item, for items that do not hold a primitive value.
  66. */
  67. String strVal2;
  68. /**
  69. * Third part of the value of this item, for items that do not hold a primitive value.
  70. */
  71. String strVal3;
  72. /**
  73. * The hash code value of this constant pool item.
  74. */
  75. int hashCode;
  76. /**
  77. * Link to another constant pool item, used for collision lists in the constant pool's hash table.
  78. */
  79. Item next;
  80. /**
  81. * Constructs an uninitialized {@link Item}.
  82. */
  83. Item(){
  84. }
  85. /**
  86. * Constructs a copy of the given item.
  87. *
  88. * @param index index of the item to be constructed.
  89. * @param i the item that must be copied into the item to be constructed.
  90. */
  91. Item(final int index, final Item i){
  92. this.index = index;
  93. type = i.type;
  94. intVal = i.intVal;
  95. longVal = i.longVal;
  96. strVal1 = i.strVal1;
  97. strVal2 = i.strVal2;
  98. strVal3 = i.strVal3;
  99. hashCode = i.hashCode;
  100. }
  101. /**
  102. * Sets this item to an item that do not hold a primitive value.
  103. *
  104. * @param type the type of this item.
  105. * @param strVal1 first part of the value of this item.
  106. * @param strVal2 second part of the value of this item.
  107. * @param strVal3 third part of the value of this item.
  108. */
  109. void set(final int type, final String strVal1, final String strVal2, final String strVal3) {
  110. this.type = type;
  111. this.strVal1 = strVal1;
  112. this.strVal2 = strVal2;
  113. this.strVal3 = strVal3;
  114. switch (type) {
  115. case ClassWriter.UTF8:
  116. case ClassWriter.STR:
  117. case ClassWriter.CLASS:
  118. case ClassWriter.TYPE_NORMAL:
  119. hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
  120. return;
  121. case ClassWriter.NAME_TYPE:
  122. hashCode = 0x7FFFFFFF & (type + strVal1.hashCode() * strVal2.hashCode());
  123. return;
  124. // ClassWriter.FIELD:
  125. // ClassWriter.METH:
  126. // ClassWriter.IMETH:
  127. default:
  128. hashCode = 0x7FFFFFFF & (type + strVal1.hashCode() * strVal2.hashCode() * strVal3.hashCode());
  129. }
  130. }
  131. /**
  132. * Sets this item to an integer item.
  133. *
  134. * @param intVal the value of this item.
  135. */
  136. void set(final int intVal) {
  137. this.type = ClassWriter.INT;
  138. this.intVal = intVal;
  139. this.hashCode = 0x7FFFFFFF & (type + intVal);
  140. }
  141. /**
  142. * Indicates if the given item is equal to this one. <i>This method assumes that the two items have the same
  143. * {@link #type}</i>.
  144. *
  145. * @param i the item to be compared to this one. Both items must have the same {@link #type}.
  146. * @return <tt>true</tt> if the given item if equal to this one, <tt>false</tt> otherwise.
  147. */
  148. boolean isEqualTo(final Item i) {
  149. switch (type) {
  150. case ClassWriter.UTF8:
  151. case ClassWriter.STR:
  152. case ClassWriter.CLASS:
  153. case ClassWriter.TYPE_NORMAL:
  154. return i.strVal1.equals(strVal1);
  155. case ClassWriter.TYPE_MERGED:
  156. case ClassWriter.LONG:
  157. case ClassWriter.DOUBLE:
  158. return i.longVal == longVal;
  159. case ClassWriter.INT:
  160. case ClassWriter.FLOAT:
  161. return i.intVal == intVal;
  162. case ClassWriter.TYPE_UNINIT:
  163. return i.intVal == intVal && i.strVal1.equals(strVal1);
  164. case ClassWriter.NAME_TYPE:
  165. return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2);
  166. // case ClassWriter.FIELD:
  167. // case ClassWriter.METH:
  168. // case ClassWriter.IMETH:
  169. default:
  170. return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2) && i.strVal3.equals(strVal3);
  171. }
  172. }
  173. }