test/tools/javap/T6622260.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

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

mercurial