Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/T6622260.java
32285 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 662226026* @summary javap prints negative bytes incorrectly in hex27*/2829import java.io.*;3031public class T6622260 {32public static void main(String[] args) throws Exception {33new T6622260().run();34}3536public void run() throws IOException {37File javaFile = writeTestFile();38File classFile = compileTestFile(javaFile);39modifyClassFile(classFile);40String output = javap(classFile);41verify(output);42}4344File writeTestFile() throws IOException {45File f = new File("Test.java");46PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));47out.println("@Deprecated class Test { int f; void m() { } }");48out.close();49return f;50}5152File compileTestFile(File f) {53int rc = com.sun.tools.javac.Main.compile(new String[] { f.getPath() });54if (rc != 0)55throw new Error("compilation failed. rc=" + rc);56String path = f.getPath();57return new File(path.substring(0, path.length() - 5) + ".class");58}5960void modifyClassFile(File f) throws IOException {61String newAttributeName = "NonstandardAttribute";62byte[] newAttributeData = { 0, 1, 2, 127, (byte)128, (byte)129, (byte)254, (byte)255 };6364DataInputStream in = new DataInputStream(new FileInputStream(f));65byte[] data = new byte[(int) f.length()];66in.readFully(data);67in.close();6869in = new DataInputStream(new ByteArrayInputStream(data));70in.skipBytes(4); // magic71in.skipBytes(2); // minor72in.skipBytes(2); // minor7374int constantPoolPos = data.length - in.available();75int constant_pool_count = skipConstantPool(in);7677int flagsPos = data.length - in.available();78in.skipBytes(2); // access_flags79in.skipBytes(2); // this_class80in.skipBytes(2); // super_class8182int interfaces_count = in.readUnsignedShort();83in.skipBytes(interfaces_count * 2);8485int field_count = in.readUnsignedShort();86for (int i = 0; i < field_count; i++) {87in.skipBytes(6); // access_flags, name_index, descriptor_index88skipAttributes(in);89}9091int method_count = in.readUnsignedShort();92for (int i = 0; i < method_count; i++) {93in.skipBytes(6); // access_flags, name_index, descriptor_index94skipAttributes(in);95}9697int classAttributesPos = data.length - in.available();98int attributes_count = in.readUnsignedShort();99100f.renameTo(new File(f.getPath() + ".BAK"));101DataOutputStream out = new DataOutputStream(new FileOutputStream(f));102103// copy head104out.write(data, 0, constantPoolPos);105106// copy constant pool, adding in name of new attribute107out.writeShort(constant_pool_count + 1);108out.write(data, constantPoolPos + 2, flagsPos - constantPoolPos - 2);109out.write(1); // CONSTANT_Utf8110out.writeUTF(newAttributeName);111112// copy flags, class, superclass, interfaces, fields and methods113out.write(data, flagsPos, classAttributesPos - flagsPos);114115// copy class attributes, adding in new attribute116out.writeShort(attributes_count + 1);117out.write(data, classAttributesPos + 2, data.length - classAttributesPos - 2);118out.writeShort(constant_pool_count); // index of new attribute name119out.writeInt(newAttributeData.length);120out.write(newAttributeData);121out.close();122}123124int skipConstantPool(DataInputStream in) throws IOException {125int constant_pool_count = in.readUnsignedShort();126for (int i = 1; i < constant_pool_count; i++) {127int tag = in.readUnsignedByte();128switch (tag) {129case 1: // CONSTANT_Utf8130int length = in.readUnsignedShort();131in.skipBytes(length); // bytes132break;133134case 3: // CONSTANT_Integer135case 4: // CONSTANT_Float136in.skipBytes(4); // bytes137break;138139case 5: // CONSTANT_Long140case 6: // CONSTANT_Double141in.skipBytes(8); // high_bytes, low_bytes142break;143144case 7: // CONSTANT_Class145in.skipBytes(2); // name_index146break;147148case 8: // CONSTANT_String149in.skipBytes(2); // string_index150break;151152case 9: // CONSTANT_FieldRef153case 10: // CONSTANT_Methodref154case 11: // CONSTANT_InterfaceMethodref155in.skipBytes(4); // class_index, name_and_type_index156break;157158case 12: // CONSTANT_NameAndType159in.skipBytes(4); // name_index, descriptor_index160break;161162default:163throw new Error("constant pool tag: " + tag);164}165}166return constant_pool_count;167}168169int skipAttributes(DataInputStream in) throws IOException {170int attributes_count = in.readUnsignedShort();171for (int i = 0; i < attributes_count; i++) {172in.skipBytes(2); // attribute_name_index;173int length = in.readInt();174in.skipBytes(length); // info175}176return attributes_count;177}178179String javap(File f) {180StringWriter sw = new StringWriter();181PrintWriter out = new PrintWriter(sw);182int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out);183if (rc != 0)184throw new Error("javap failed. rc=" + rc);185out.close();186return sw.toString();187}188189void verify(String output) {190System.out.println(output);191output = output.substring(output.indexOf("Test.java"));192if (output.indexOf("-") >= 0)193throw new Error("- found in output");194if (output.indexOf("FFFFFF") >= 0)195throw new Error("FFFFFF found in output");196}197}198199200