Path: blob/master/test/langtools/tools/javap/BadAttributeName.java
41144 views
/*1* Copyright (c) 2014, 2019, 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 823468726* @summary change javap reporting on unknown attributes27* @modules jdk.jdeps/com.sun.tools.javap28* @run main BadAttributeName29*/303132import java.io.*;33import java.nio.file.*;3435public class BadAttributeName {3637public static String source = "public class Test {\n" +38" public static void main(String[] args) {}\n" +39"}";4041public static void main(String[] args) throws Exception {42final File srcFile = new File("Test.java");43Files.writeString(Path.of("Test.java"), source);4445final String[] javacOpts = {"Test.java"};4647if (com.sun.tools.javac.Main.compile(javacOpts) != 0) {48throw new Exception("Can't compile embedded test.");49}5051RandomAccessFile raf = new RandomAccessFile("Test.class", "rw");52String sourceFile = "SourceFile";53long namePos = getConstantPoolUTF8Pos(raf, sourceFile);54if (namePos < 0) {55throw new Exception("The class file contains no SourceFile attribute.");56}5758raf.seek(namePos); // Jump to the SourceFile name59// Create a "custom" attribute by reusing/renaming an unimportant existing one60String customAttr = "CustomAttribute".substring(0, sourceFile.length());61raf.writeUTF(customAttr);62raf.close();6364String[] opts = { "-v", "Test.class" };65StringWriter sw = new StringWriter();66PrintWriter pout = new PrintWriter(sw);6768com.sun.tools.javap.Main.run(opts, pout);69pout.flush();7071String expect = customAttr + ": length = 0x2 (unknown attribute)";72if (sw.toString().indexOf(expect) == -1) {73sw.toString().lines().forEach(System.out::println);74throw new Exception("expected text not found: " + expect);75}76}7778private static long getConstantPoolUTF8Pos(RandomAccessFile cfile, String name) throws Exception {79cfile.seek(0);80int v1, v2;81v1 = cfile.readInt();82// System.out.println("Magic: " + String.format("%X", v1));8384v1 = cfile.readUnsignedShort();85v2 = cfile.readUnsignedShort();86// System.out.println("Version: " + String.format("%d.%d", v1, v2));8788v1 = cfile.readUnsignedShort();89// System.out.println("CPool size: " + v1);90// Exhaust the constant pool91for (; v1 > 1; v1--) {92// System.out.print(".");93byte tag = cfile.readByte();94switch (tag) {95case 7 : // Class96case 8 : // String97// Data is 2 bytes long98cfile.skipBytes(2);99break;100case 3 : // Integer101case 4 : // Float102case 9 : // FieldRef103case 10 : // MethodRef104case 11 : // InterfaceMethodRef105case 12 : // Name and Type106// Data is 4 bytes long107cfile.skipBytes(4);108break;109case 5 : // Long110case 6 : // Double111// Data is 8 bytes long112cfile.skipBytes(8);113break;114case 1 : // Utf8115long fp = cfile.getFilePointer();116String s = cfile.readUTF();117if (s.equals(name)) {118return fp;119}120break;121default :122throw new Exception("Unexpected tag in CPool: [" + tag + "] at "123+ Long.toHexString(cfile.getFilePointer()));124}125}126// System.out.println();127128// Bummer! Name not found!129return -1L;130}131}132133134