T6271787.java revision 3294:9adfb22ff08f
11590Srgrimes/* 239230Sgibbs * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. 339230Sgibbs * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 439230Sgibbs * 539230Sgibbs * This code is free software; you can redistribute it and/or modify it 639230Sgibbs * under the terms of the GNU General Public License version 2 only, as 739230Sgibbs * published by the Free Software Foundation. 839230Sgibbs * 939230Sgibbs * This code is distributed in the hope that it will be useful, but WITHOUT 1039230Sgibbs * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1139230Sgibbs * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1239230Sgibbs * version 2 for more details (a copy is included in the LICENSE file that 1339230Sgibbs * accompanied this code). 1439230Sgibbs * 1539230Sgibbs * You should have received a copy of the GNU General Public License version 1639230Sgibbs * 2 along with this work; if not, write to the Free Software Foundation, 1739230Sgibbs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1839230Sgibbs * 1939230Sgibbs * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2039230Sgibbs * or visit www.oracle.com if you need additional information or have any 2139230Sgibbs * questions. 2239230Sgibbs */ 2339230Sgibbs 2439230Sgibbs/* 2539230Sgibbs * @test 2639230Sgibbs * @bug 6271787 2739230Sgibbs * @summary javap dumps LocalVariableTypeTable attribute in hex, needs to print a table 2839230Sgibbs * @modules jdk.jdeps/com.sun.tools.javap 291590Srgrimes */ 301590Srgrimes 311590Srgrimesimport java.io.*; 321590Srgrimes 331590Srgrimespublic class T6271787 { 341590Srgrimes public static void main(String[] args) throws Exception { 351590Srgrimes new T6271787().run(); 361590Srgrimes } 371590Srgrimes 381590Srgrimes public void run() throws IOException { 391590Srgrimes File javaFile = writeTestFile(); 401590Srgrimes File classFile = compileTestFile(javaFile); 411590Srgrimes 421590Srgrimes verify(classFile, 431590Srgrimes "LocalVariableTypeTable:", 441590Srgrimes "0 5 0 this LTest<TT;>;" // should consider decoding this in javap 451590Srgrimes ); 461590Srgrimes 471590Srgrimes if (errors > 0) 481590Srgrimes throw new Error(errors + " found."); 491590Srgrimes } 501590Srgrimes 511590Srgrimes File writeTestFile() throws IOException { 521590Srgrimes File f = new File("Test.java"); 531590Srgrimes PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 541590Srgrimes out.println("class Test<T> { }"); 551590Srgrimes out.close(); 561590Srgrimes return f; 571590Srgrimes } 581590Srgrimes 591590Srgrimes File compileTestFile(File f) { 601590Srgrimes int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); 6187715Smarkm if (rc != 0) 621590Srgrimes throw new Error("compilation failed. rc=" + rc); 6387715Smarkm String path = f.getPath(); 6487715Smarkm return new File(path.substring(0, path.length() - 5) + ".class"); 6587715Smarkm } 6687715Smarkm 6787715Smarkm String javap(File f) { 6887715Smarkm StringWriter sw = new StringWriter(); 691590Srgrimes PrintWriter out = new PrintWriter(sw); 701590Srgrimes int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out); 7174671Stmm if (rc != 0) 721590Srgrimes throw new Error("javap failed. rc=" + rc); 7387715Smarkm out.close(); 7487715Smarkm return sw.toString(); 751590Srgrimes } 761590Srgrimes 7787715Smarkm void verify(File classFile, String... expects) { 7887715Smarkm String output = javap(classFile); 7987715Smarkm for (String expect: expects) { 801590Srgrimes if (output.indexOf(expect)< 0) 811590Srgrimes error(expect + " not found"); 8240060Sobrien } 831590Srgrimes } 8439230Sgibbs 851590Srgrimes void error(String msg) { 861590Srgrimes System.err.println(msg); 871590Srgrimes errors++; 881590Srgrimes } 8939230Sgibbs 901590Srgrimes int errors; 911590Srgrimes} 9239230Sgibbs