aoqi@0: /* aoqi@0: * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6622260 aoqi@0: * @summary javap prints negative bytes incorrectly in hex aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: aoqi@0: public class T6622260 { aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: new T6622260().run(); aoqi@0: } aoqi@0: aoqi@0: public void run() throws IOException { aoqi@0: File javaFile = writeTestFile(); aoqi@0: File classFile = compileTestFile(javaFile); aoqi@0: modifyClassFile(classFile); aoqi@0: String output = javap(classFile); aoqi@0: verify(output); aoqi@0: } aoqi@0: aoqi@0: File writeTestFile() throws IOException { aoqi@0: File f = new File("Test.java"); aoqi@0: PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); aoqi@0: out.println("@Deprecated class Test { int f; void m() { } }"); aoqi@0: out.close(); aoqi@0: return f; aoqi@0: } aoqi@0: aoqi@0: File compileTestFile(File f) { aoqi@0: int rc = com.sun.tools.javac.Main.compile(new String[] { f.getPath() }); aoqi@0: if (rc != 0) aoqi@0: throw new Error("compilation failed. rc=" + rc); aoqi@0: String path = f.getPath(); aoqi@0: return new File(path.substring(0, path.length() - 5) + ".class"); aoqi@0: } aoqi@0: aoqi@0: void modifyClassFile(File f) throws IOException { aoqi@0: String newAttributeName = "NonstandardAttribute"; aoqi@0: byte[] newAttributeData = { 0, 1, 2, 127, (byte)128, (byte)129, (byte)254, (byte)255 }; aoqi@0: aoqi@0: DataInputStream in = new DataInputStream(new FileInputStream(f)); aoqi@0: byte[] data = new byte[(int) f.length()]; aoqi@0: in.readFully(data); aoqi@0: in.close(); aoqi@0: aoqi@0: in = new DataInputStream(new ByteArrayInputStream(data)); aoqi@0: in.skipBytes(4); // magic aoqi@0: in.skipBytes(2); // minor aoqi@0: in.skipBytes(2); // minor aoqi@0: aoqi@0: int constantPoolPos = data.length - in.available(); aoqi@0: int constant_pool_count = skipConstantPool(in); aoqi@0: aoqi@0: int flagsPos = data.length - in.available(); aoqi@0: in.skipBytes(2); // access_flags aoqi@0: in.skipBytes(2); // this_class aoqi@0: in.skipBytes(2); // super_class aoqi@0: aoqi@0: int interfaces_count = in.readUnsignedShort(); aoqi@0: in.skipBytes(interfaces_count * 2); aoqi@0: aoqi@0: int field_count = in.readUnsignedShort(); aoqi@0: for (int i = 0; i < field_count; i++) { aoqi@0: in.skipBytes(6); // access_flags, name_index, descriptor_index aoqi@0: skipAttributes(in); aoqi@0: } aoqi@0: aoqi@0: int method_count = in.readUnsignedShort(); aoqi@0: for (int i = 0; i < method_count; i++) { aoqi@0: in.skipBytes(6); // access_flags, name_index, descriptor_index aoqi@0: skipAttributes(in); aoqi@0: } aoqi@0: aoqi@0: int classAttributesPos = data.length - in.available(); aoqi@0: int attributes_count = in.readUnsignedShort(); aoqi@0: aoqi@0: f.renameTo(new File(f.getPath() + ".BAK")); aoqi@0: DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); aoqi@0: aoqi@0: // copy head aoqi@0: out.write(data, 0, constantPoolPos); aoqi@0: aoqi@0: // copy constant pool, adding in name of new attribute aoqi@0: out.writeShort(constant_pool_count + 1); aoqi@0: out.write(data, constantPoolPos + 2, flagsPos - constantPoolPos - 2); aoqi@0: out.write(1); // CONSTANT_Utf8 aoqi@0: out.writeUTF(newAttributeName); aoqi@0: aoqi@0: // copy flags, class, superclass, interfaces, fields and methods aoqi@0: out.write(data, flagsPos, classAttributesPos - flagsPos); aoqi@0: aoqi@0: // copy class attributes, adding in new attribute aoqi@0: out.writeShort(attributes_count + 1); aoqi@0: out.write(data, classAttributesPos + 2, data.length - classAttributesPos - 2); aoqi@0: out.writeShort(constant_pool_count); // index of new attribute name aoqi@0: out.writeInt(newAttributeData.length); aoqi@0: out.write(newAttributeData); aoqi@0: out.close(); aoqi@0: } aoqi@0: aoqi@0: int skipConstantPool(DataInputStream in) throws IOException { aoqi@0: int constant_pool_count = in.readUnsignedShort(); aoqi@0: for (int i = 1; i < constant_pool_count; i++) { aoqi@0: int tag = in.readUnsignedByte(); aoqi@0: switch (tag) { aoqi@0: case 1: // CONSTANT_Utf8 aoqi@0: int length = in.readUnsignedShort(); aoqi@0: in.skipBytes(length); // bytes aoqi@0: break; aoqi@0: aoqi@0: case 3: // CONSTANT_Integer aoqi@0: case 4: // CONSTANT_Float aoqi@0: in.skipBytes(4); // bytes aoqi@0: break; aoqi@0: aoqi@0: case 5: // CONSTANT_Long aoqi@0: case 6: // CONSTANT_Double aoqi@0: in.skipBytes(8); // high_bytes, low_bytes aoqi@0: break; aoqi@0: aoqi@0: case 7: // CONSTANT_Class aoqi@0: in.skipBytes(2); // name_index aoqi@0: break; aoqi@0: aoqi@0: case 8: // CONSTANT_String aoqi@0: in.skipBytes(2); // string_index aoqi@0: break; aoqi@0: aoqi@0: case 9: // CONSTANT_FieldRef aoqi@0: case 10: // CONSTANT_Methodref aoqi@0: case 11: // CONSTANT_InterfaceMethodref aoqi@0: in.skipBytes(4); // class_index, name_and_type_index aoqi@0: break; aoqi@0: aoqi@0: case 12: // CONSTANT_NameAndType aoqi@0: in.skipBytes(4); // name_index, descriptor_index aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: throw new Error("constant pool tag: " + tag); aoqi@0: } aoqi@0: } aoqi@0: return constant_pool_count; aoqi@0: } aoqi@0: aoqi@0: int skipAttributes(DataInputStream in) throws IOException { aoqi@0: int attributes_count = in.readUnsignedShort(); aoqi@0: for (int i = 0; i < attributes_count; i++) { aoqi@0: in.skipBytes(2); // attribute_name_index; aoqi@0: int length = in.readInt(); aoqi@0: in.skipBytes(length); // info aoqi@0: } aoqi@0: return attributes_count; aoqi@0: } aoqi@0: aoqi@0: String javap(File f) { aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter out = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out); aoqi@0: if (rc != 0) aoqi@0: throw new Error("javap failed. rc=" + rc); aoqi@0: out.close(); aoqi@0: return sw.toString(); aoqi@0: } aoqi@0: aoqi@0: void verify(String output) { aoqi@0: System.out.println(output); aoqi@0: output = output.substring(output.indexOf("Test.java")); aoqi@0: if (output.indexOf("-") >= 0) aoqi@0: throw new Error("- found in output"); aoqi@0: if (output.indexOf("FFFFFF") >= 0) aoqi@0: throw new Error("FFFFFF found in output"); aoqi@0: } aoqi@0: }