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