PageRenderTime 100ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/o.n.bootstrap/src/org/netbeans/PatchByteCode.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 181 lines | 113 code | 8 blank | 60 comment | 14 complexity | 9884b7138bf39bec8d17a05720e16099 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans;
  45. import java.io.UnsupportedEncodingException;
  46. import org.openide.modules.PatchedPublic;
  47. /**
  48. * Tool to patch bytecode, currently just to make access modifiers public.
  49. * Determines when and what to patch based on class-retention annotations.
  50. * @see PatchedPublic
  51. * @see #patch
  52. */
  53. public final class PatchByteCode {
  54. private static final byte[] RUNTIME_INVISIBLE_ANNOTATIONS, PATCHED_PUBLIC;
  55. static {
  56. try {
  57. RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations".getBytes("UTF-8"); // NOI18N
  58. PATCHED_PUBLIC = ("L" + PatchedPublic.class.getName().replace('.', '/') + ";").getBytes("UTF-8"); // NOI18N
  59. } catch (UnsupportedEncodingException x) {
  60. throw new ExceptionInInitializerError(x);
  61. }
  62. }
  63. /**
  64. * Patches a class if it is needed.
  65. * @param arr the bytecode
  66. * @return the enhanced bytecode
  67. */
  68. public static byte[] patch(byte[] data) {
  69. int constant_pool_count = u2(data, /* magic + minor_version + major_version */ 8);
  70. int[] constantPoolOffsets = new int[constant_pool_count];
  71. int pos = 10; // 8 + constant_pool_count
  72. for (int i = 1; i < constant_pool_count; i++) {
  73. int tag = u1(data, pos++);
  74. //System.err.println("tag " + tag + " at #" + i + " at location " + pos);
  75. constantPoolOffsets[i] = pos;
  76. switch (tag) {
  77. case 1: // CONSTANT_Utf8
  78. int len = u2(data, pos);
  79. //try {System.err.println("UTF-8 constant: " + new String(data, pos + 2, len, "UTF-8"));} catch (UnsupportedEncodingException x) {}
  80. pos += len + 2;
  81. break;
  82. case 3: // CONSTANT_Integer
  83. case 4: // CONSTANT_Float
  84. case 9: // CONSTANT_Fieldref
  85. case 10: // CONSTANT_Methodref
  86. case 11: // CONSTANT_InterfaceMethodref
  87. case 12: // CONSTANT_NameAndType
  88. pos += 4;
  89. break;
  90. case 7: // CONSTANT_Class
  91. case 8: // CONSTANT_String
  92. pos += 2;
  93. break;
  94. case 5: // CONSTANT_Long
  95. case 6: // CONSTANT_Double
  96. pos += 8;
  97. i++; // next entry is ignored
  98. break;
  99. default:
  100. throw new IllegalArgumentException("illegal constant pool tag " + tag + " at index " + i + " out of " + constant_pool_count);
  101. }
  102. }
  103. pos += 6; // access_flags + this_class + super_class
  104. int interfaces_count = u2(data, pos);
  105. pos += 2; // interfaces_count
  106. pos += 2 * interfaces_count; // interfaces
  107. int fields_count = u2(data, pos);
  108. pos += 2; // fields_count
  109. for (int i = 0; i < fields_count; i++) {
  110. pos += 6; // access_flags + name_index + descriptor_index
  111. int attributes_count = u2(data, pos);
  112. pos += 2; // attributes_count
  113. for (int j = 0; j < attributes_count; j++) {
  114. pos += 2; // attribute_name_index
  115. int attribute_length = u4(data, pos);
  116. pos += 4; // attribute_length
  117. pos += attribute_length; // info
  118. }
  119. }
  120. int methods_count = u2(data, pos);
  121. pos += 2; // methods_count
  122. for (int i = 0; i < methods_count; i++) {
  123. int locationOfAccessFlags = pos;
  124. pos += 6; // access_flags + name_index + descriptor_index
  125. int attributes_count = u2(data, pos);
  126. pos += 2; // attributes_count
  127. for (int j = 0; j < attributes_count; j++) {
  128. int locationOfAttributeName = constantPoolOffsets[u2(data, pos)];
  129. pos += 2; // attribute_name_index
  130. int attribute_length = u4(data, pos);
  131. pos += 4; // attribute_length
  132. if (utf8Matches(data, locationOfAttributeName, RUNTIME_INVISIBLE_ANNOTATIONS)) {
  133. int num_annotations = u2(data, pos);
  134. int pos2 = pos + 2; // num_annotations
  135. for (int k = 0; k < num_annotations; k++) {
  136. if (utf8Matches(data, constantPoolOffsets[u2(data, pos2)], PATCHED_PUBLIC)) {
  137. // Got it, we are setting the method to be public.
  138. data[locationOfAccessFlags + 1] &= 0xF9; // - ACC_PRIVATE - ACC_PROTECTED
  139. data[locationOfAccessFlags + 1] |= 0x01; // + ACC_PUBLIC
  140. }
  141. // XXX skip over annotation body so we can support >1 annotation on the member
  142. // (i.e. @PatchedPublic occurs only after other annotations)
  143. // but it is tedious to calculate the length of element_value structs
  144. continue;
  145. }
  146. }
  147. pos += attribute_length; // info
  148. }
  149. }
  150. return data;
  151. }
  152. private static int u1(byte[] data, int off) {
  153. byte b = data[off];
  154. return b >= 0 ? b : b + 256;
  155. }
  156. private static int u2(byte[] data, int off) {
  157. return (u1(data, off) << 8) + u1(data, off + 1);
  158. }
  159. private static int u4(byte[] data, int off) {
  160. return (u2(data, off) << 16) + u2(data, off + 2);
  161. }
  162. private static boolean utf8Matches(byte[] data, int off, byte[] expected) {
  163. if (u2(data, off) != expected.length) {
  164. return false;
  165. }
  166. for (int i = 0; i < expected.length; i++) {
  167. if (data[off + 2 + i] != expected[i]) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. }