Mon, 03 Oct 2016 11:21:14 -0700
Added tag jdk8u122-b02 for changeset 2baeb96fa198
jjg@46 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
jjg@46 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@46 | 4 | * |
jjg@46 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@46 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@46 | 7 | * published by the Free Software Foundation. |
jjg@46 | 8 | * |
jjg@46 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@46 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@46 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@46 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@46 | 13 | * accompanied this code). |
jjg@46 | 14 | * |
jjg@46 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@46 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@46 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@46 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
jjg@46 | 22 | */ |
jjg@46 | 23 | |
jjg@46 | 24 | /* |
jjg@46 | 25 | * @test |
jjg@46 | 26 | * @bug 6622260 |
jjg@46 | 27 | * @summary javap prints negative bytes incorrectly in hex |
jjg@46 | 28 | */ |
jjg@46 | 29 | |
jjg@46 | 30 | import java.io.*; |
jjg@46 | 31 | |
jjg@46 | 32 | public class T6622260 { |
jjg@46 | 33 | public static void main(String[] args) throws Exception { |
jjg@46 | 34 | new T6622260().run(); |
jjg@46 | 35 | } |
jjg@46 | 36 | |
jjg@46 | 37 | public void run() throws IOException { |
jjg@46 | 38 | File javaFile = writeTestFile(); |
jjg@46 | 39 | File classFile = compileTestFile(javaFile); |
jjg@46 | 40 | modifyClassFile(classFile); |
jjg@46 | 41 | String output = javap(classFile); |
jjg@46 | 42 | verify(output); |
jjg@46 | 43 | } |
jjg@46 | 44 | |
jjg@46 | 45 | File writeTestFile() throws IOException { |
jjg@46 | 46 | File f = new File("Test.java"); |
jjg@46 | 47 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
jjg@46 | 48 | out.println("@Deprecated class Test { int f; void m() { } }"); |
jjg@46 | 49 | out.close(); |
jjg@46 | 50 | return f; |
jjg@46 | 51 | } |
jjg@46 | 52 | |
jjg@46 | 53 | File compileTestFile(File f) { |
jjg@46 | 54 | int rc = com.sun.tools.javac.Main.compile(new String[] { f.getPath() }); |
jjg@46 | 55 | if (rc != 0) |
jjg@46 | 56 | throw new Error("compilation failed. rc=" + rc); |
jjg@46 | 57 | String path = f.getPath(); |
jjg@46 | 58 | return new File(path.substring(0, path.length() - 5) + ".class"); |
jjg@46 | 59 | } |
jjg@46 | 60 | |
jjg@46 | 61 | void modifyClassFile(File f) throws IOException { |
jjg@46 | 62 | String newAttributeName = "NonstandardAttribute"; |
jjg@46 | 63 | byte[] newAttributeData = { 0, 1, 2, 127, (byte)128, (byte)129, (byte)254, (byte)255 }; |
jjg@46 | 64 | |
jjg@46 | 65 | DataInputStream in = new DataInputStream(new FileInputStream(f)); |
jjg@46 | 66 | byte[] data = new byte[(int) f.length()]; |
jjg@46 | 67 | in.readFully(data); |
jjg@46 | 68 | in.close(); |
jjg@46 | 69 | |
jjg@46 | 70 | in = new DataInputStream(new ByteArrayInputStream(data)); |
jjg@46 | 71 | in.skipBytes(4); // magic |
jjg@46 | 72 | in.skipBytes(2); // minor |
jjg@46 | 73 | in.skipBytes(2); // minor |
jjg@46 | 74 | |
jjg@46 | 75 | int constantPoolPos = data.length - in.available(); |
jjg@46 | 76 | int constant_pool_count = skipConstantPool(in); |
jjg@46 | 77 | |
jjg@46 | 78 | int flagsPos = data.length - in.available(); |
jjg@46 | 79 | in.skipBytes(2); // access_flags |
jjg@46 | 80 | in.skipBytes(2); // this_class |
jjg@46 | 81 | in.skipBytes(2); // super_class |
jjg@46 | 82 | |
jjg@46 | 83 | int interfaces_count = in.readUnsignedShort(); |
jjg@46 | 84 | in.skipBytes(interfaces_count * 2); |
jjg@46 | 85 | |
jjg@46 | 86 | int field_count = in.readUnsignedShort(); |
jjg@46 | 87 | for (int i = 0; i < field_count; i++) { |
jjg@46 | 88 | in.skipBytes(6); // access_flags, name_index, descriptor_index |
jjg@46 | 89 | skipAttributes(in); |
jjg@46 | 90 | } |
jjg@46 | 91 | |
jjg@46 | 92 | int method_count = in.readUnsignedShort(); |
jjg@46 | 93 | for (int i = 0; i < method_count; i++) { |
jjg@46 | 94 | in.skipBytes(6); // access_flags, name_index, descriptor_index |
jjg@46 | 95 | skipAttributes(in); |
jjg@46 | 96 | } |
jjg@46 | 97 | |
jjg@46 | 98 | int classAttributesPos = data.length - in.available(); |
jjg@46 | 99 | int attributes_count = in.readUnsignedShort(); |
jjg@46 | 100 | |
jjg@46 | 101 | f.renameTo(new File(f.getPath() + ".BAK")); |
jjg@46 | 102 | DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); |
jjg@46 | 103 | |
jjg@46 | 104 | // copy head |
jjg@46 | 105 | out.write(data, 0, constantPoolPos); |
jjg@46 | 106 | |
jjg@46 | 107 | // copy constant pool, adding in name of new attribute |
jjg@46 | 108 | out.writeShort(constant_pool_count + 1); |
jjg@46 | 109 | out.write(data, constantPoolPos + 2, flagsPos - constantPoolPos - 2); |
jjg@46 | 110 | out.write(1); // CONSTANT_Utf8 |
jjg@46 | 111 | out.writeUTF(newAttributeName); |
jjg@46 | 112 | |
jjg@46 | 113 | // copy flags, class, superclass, interfaces, fields and methods |
jjg@46 | 114 | out.write(data, flagsPos, classAttributesPos - flagsPos); |
jjg@46 | 115 | |
jjg@46 | 116 | // copy class attributes, adding in new attribute |
jjg@46 | 117 | out.writeShort(attributes_count + 1); |
jjg@46 | 118 | out.write(data, classAttributesPos + 2, data.length - classAttributesPos - 2); |
jjg@46 | 119 | out.writeShort(constant_pool_count); // index of new attribute name |
jjg@46 | 120 | out.writeInt(newAttributeData.length); |
jjg@46 | 121 | out.write(newAttributeData); |
jjg@46 | 122 | out.close(); |
jjg@46 | 123 | } |
jjg@46 | 124 | |
jjg@46 | 125 | int skipConstantPool(DataInputStream in) throws IOException { |
jjg@46 | 126 | int constant_pool_count = in.readUnsignedShort(); |
jjg@46 | 127 | for (int i = 1; i < constant_pool_count; i++) { |
jjg@46 | 128 | int tag = in.readUnsignedByte(); |
jjg@46 | 129 | switch (tag) { |
jjg@46 | 130 | case 1: // CONSTANT_Utf8 |
jjg@46 | 131 | int length = in.readUnsignedShort(); |
jjg@46 | 132 | in.skipBytes(length); // bytes |
jjg@46 | 133 | break; |
jjg@46 | 134 | |
jjg@46 | 135 | case 3: // CONSTANT_Integer |
jjg@46 | 136 | case 4: // CONSTANT_Float |
jjg@46 | 137 | in.skipBytes(4); // bytes |
jjg@46 | 138 | break; |
jjg@46 | 139 | |
jjg@46 | 140 | case 5: // CONSTANT_Long |
jjg@46 | 141 | case 6: // CONSTANT_Double |
jjg@46 | 142 | in.skipBytes(8); // high_bytes, low_bytes |
jjg@46 | 143 | break; |
jjg@46 | 144 | |
jjg@46 | 145 | case 7: // CONSTANT_Class |
jjg@46 | 146 | in.skipBytes(2); // name_index |
jjg@46 | 147 | break; |
jjg@46 | 148 | |
jjg@46 | 149 | case 8: // CONSTANT_String |
jjg@46 | 150 | in.skipBytes(2); // string_index |
jjg@46 | 151 | break; |
jjg@46 | 152 | |
jjg@46 | 153 | case 9: // CONSTANT_FieldRef |
jjg@46 | 154 | case 10: // CONSTANT_Methodref |
jjg@46 | 155 | case 11: // CONSTANT_InterfaceMethodref |
jjg@46 | 156 | in.skipBytes(4); // class_index, name_and_type_index |
jjg@46 | 157 | break; |
jjg@46 | 158 | |
jjg@46 | 159 | case 12: // CONSTANT_NameAndType |
jjg@46 | 160 | in.skipBytes(4); // name_index, descriptor_index |
jjg@46 | 161 | break; |
jjg@46 | 162 | |
jjg@46 | 163 | default: |
jjg@46 | 164 | throw new Error("constant pool tag: " + tag); |
jjg@46 | 165 | } |
jjg@46 | 166 | } |
jjg@46 | 167 | return constant_pool_count; |
jjg@46 | 168 | } |
jjg@46 | 169 | |
jjg@46 | 170 | int skipAttributes(DataInputStream in) throws IOException { |
jjg@46 | 171 | int attributes_count = in.readUnsignedShort(); |
jjg@46 | 172 | for (int i = 0; i < attributes_count; i++) { |
jjg@46 | 173 | in.skipBytes(2); // attribute_name_index; |
jjg@46 | 174 | int length = in.readInt(); |
jjg@46 | 175 | in.skipBytes(length); // info |
jjg@46 | 176 | } |
jjg@46 | 177 | return attributes_count; |
jjg@46 | 178 | } |
jjg@46 | 179 | |
jjg@46 | 180 | String javap(File f) { |
jjg@46 | 181 | StringWriter sw = new StringWriter(); |
jjg@46 | 182 | PrintWriter out = new PrintWriter(sw); |
jjg@46 | 183 | int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out); |
jjg@46 | 184 | if (rc != 0) |
jjg@46 | 185 | throw new Error("javap failed. rc=" + rc); |
jjg@46 | 186 | out.close(); |
jjg@46 | 187 | return sw.toString(); |
jjg@46 | 188 | } |
jjg@46 | 189 | |
jjg@46 | 190 | void verify(String output) { |
jjg@46 | 191 | System.out.println(output); |
mcimadamore@146 | 192 | output = output.substring(output.indexOf("Test.java")); |
jjg@46 | 193 | if (output.indexOf("-") >= 0) |
jjg@46 | 194 | throw new Error("- found in output"); |
jjg@46 | 195 | if (output.indexOf("FFFFFF") >= 0) |
jjg@46 | 196 | throw new Error("FFFFFF found in output"); |
jjg@46 | 197 | } |
jjg@46 | 198 | } |