PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 116 lines | 29 code | 12 blank | 75 comment | 3 complexity | 809136a690fa682d3290e4ef85c1d3e7 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. * An {@link FieldVisitor} that generates Java fields in bytecode form.
  33. *
  34. * @author Eric Bruneton
  35. */
  36. final class FieldWriter implements FieldVisitor {
  37. /**
  38. * Next field writer (see {@link ClassWriter#firstField firstField}).
  39. */
  40. FieldWriter next;
  41. /**
  42. * Access flags of this field.
  43. */
  44. private final int access;
  45. /**
  46. * The index of the constant pool item that contains the name of this method.
  47. */
  48. private final int name;
  49. /**
  50. * The index of the constant pool item that contains the descriptor of this field.
  51. */
  52. private final int desc;
  53. // ------------------------------------------------------------------------
  54. // Constructor
  55. // ------------------------------------------------------------------------
  56. /**
  57. * Constructs a new {@link FieldWriter}.
  58. *
  59. * @param cw the class writer to which this field must be added.
  60. * @param access the field's access flags (see {@link Opcodes}).
  61. * @param name the field's name.
  62. * @param desc the field's descriptor (see {@link Type}).
  63. * @param signature the field's signature. May be <tt>null</tt>.
  64. * @param value the field's constant value. May be <tt>null</tt>.
  65. */
  66. FieldWriter(final ClassWriter cw, final int access, final String name, final String desc){
  67. if (cw.firstField == null) {
  68. cw.firstField = this;
  69. } else {
  70. cw.lastField.next = this;
  71. }
  72. cw.lastField = this;
  73. this.access = access;
  74. this.name = cw.newUTF8(name);
  75. this.desc = cw.newUTF8(desc);
  76. }
  77. // ------------------------------------------------------------------------
  78. // Implementation of the FieldVisitor interface
  79. // ------------------------------------------------------------------------
  80. public void visitEnd() {
  81. }
  82. // ------------------------------------------------------------------------
  83. // Utility methods
  84. // ------------------------------------------------------------------------
  85. /**
  86. * Returns the size of this field.
  87. *
  88. * @return the size of this field.
  89. */
  90. int getSize() {
  91. return 8;
  92. }
  93. /**
  94. * Puts the content of this field into the given byte vector.
  95. *
  96. * @param out where the content of this field must be put.
  97. */
  98. void put(final ByteVector out) {
  99. int mask = Opcodes.ACC_DEPRECATED | ClassWriter.ACC_SYNTHETIC_ATTRIBUTE | ((access & ClassWriter.ACC_SYNTHETIC_ATTRIBUTE) / (ClassWriter.ACC_SYNTHETIC_ATTRIBUTE / Opcodes.ACC_SYNTHETIC));
  100. out.putShort(access & ~mask).putShort(name).putShort(desc);
  101. int attributeCount = 0;
  102. out.putShort(attributeCount);
  103. }
  104. }