T6271787.java revision 553:9d9f26857129
160107Sobrien/*
28119Sasami * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
38119Sasami * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
48119Sasami *
58119Sasami * This code is free software; you can redistribute it and/or modify it
68119Sasami * under the terms of the GNU General Public License version 2 only, as
78119Sasami * published by the Free Software Foundation.
88119Sasami *
932822Syokota * This code is distributed in the hope that it will be useful, but WITHOUT
108119Sasami * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
118119Sasami * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
128119Sasami * version 2 for more details (a copy is included in the LICENSE file that
1332822Syokota * accompanied this code).
148119Sasami *
158119Sasami * You should have received a copy of the GNU General Public License version
168119Sasami * 2 along with this work; if not, write to the Free Software Foundation,
178119Sasami * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1832822Syokota *
1932822Syokota * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
208119Sasami * or visit www.oracle.com if you need additional information or have any
2138141Syokota * questions.
228119Sasami */
238119Sasami
248119Sasami/*
258119Sasami * @test
268119Sasami * @bug 6271787
278119Sasami * @summary javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
288119Sasami */
298119Sasami
308119Sasamiimport java.io.*;
318119Sasami
3232822Syokotapublic class T6271787 {
3332822Syokota    public static void main(String[] args) throws Exception {
348119Sasami        new T6271787().run();
358119Sasami    }
368119Sasami
378119Sasami    public void run() throws IOException {
388119Sasami        File javaFile = writeTestFile();
398119Sasami        File classFile = compileTestFile(javaFile);
408119Sasami
418119Sasami        verify(classFile,
428119Sasami               "LocalVariableTypeTable:",
438119Sasami               "0       5     0  this   LTest<TT;>;" // should consider decoding this in javap
448119Sasami               );
458119Sasami
468119Sasami        if (errors > 0)
478119Sasami            throw new Error(errors + " found.");
488119Sasami    }
4932822Syokota
508119Sasami    File writeTestFile() throws IOException {
518119Sasami        File f = new File("Test.java");
528119Sasami        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
538119Sasami        out.println("class Test<T> { }");
548119Sasami        out.close();
558119Sasami        return f;
568119Sasami    }
578119Sasami
588119Sasami    File compileTestFile(File f) {
598119Sasami        int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });
608119Sasami        if (rc != 0)
6143334Syokota            throw new Error("compilation failed. rc=" + rc);
628119Sasami        String path = f.getPath();
6332822Syokota        return new File(path.substring(0, path.length() - 5) + ".class");
648119Sasami    }
658119Sasami
668119Sasami    String javap(File f) {
678119Sasami        StringWriter sw = new StringWriter();
688119Sasami        PrintWriter out = new PrintWriter(sw);
698119Sasami        int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out);
708119Sasami        if (rc != 0)
718119Sasami            throw new Error("javap failed. rc=" + rc);
728119Sasami        out.close();
738119Sasami        return sw.toString();
748119Sasami    }
758119Sasami
768119Sasami    void verify(File classFile, String... expects) {
778119Sasami        String output = javap(classFile);
788119Sasami        for (String expect: expects) {
798119Sasami            if (output.indexOf(expect)< 0)
808119Sasami                error(expect + " not found");
818119Sasami        }
8238141Syokota    }
838119Sasami
848119Sasami    void error(String msg) {
858119Sasami        System.err.println(msg);
868119Sasami        errors++;
878119Sasami    }
888119Sasami
8938141Syokota    int errors;
908119Sasami}
918119Sasami