T4111861.java revision 348:bc0b1f404c40
139215Sgibbs/*
2107178Snjl * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
339215Sgibbs * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4107178Snjl *
539215Sgibbs * This code is free software; you can redistribute it and/or modify it
639215Sgibbs * under the terms of the GNU General Public License version 2 only, as
739215Sgibbs * published by the Free Software Foundation.
839215Sgibbs *
939215Sgibbs * This code is distributed in the hope that it will be useful, but WITHOUT
1039215Sgibbs * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1139215Sgibbs * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1239215Sgibbs * version 2 for more details (a copy is included in the LICENSE file that
1339215Sgibbs * accompanied this code).
1439215Sgibbs *
1539215Sgibbs * You should have received a copy of the GNU General Public License version
1639215Sgibbs * 2 along with this work; if not, write to the Free Software Foundation,
1739215Sgibbs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1839215Sgibbs *
1939215Sgibbs * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2039215Sgibbs * CA 95054 USA or visit www.sun.com if you need additional information or
2139215Sgibbs * have any questions.
2239215Sgibbs */
2339215Sgibbs
2439215Sgibbsimport java.io.*;
2539215Sgibbs
2639215Sgibbs/*
2739215Sgibbs * @test
2850476Speter * @bug 4111861
2939215Sgibbs * @summary static final field contents are not displayed
3039215Sgibbs */
3139215Sgibbspublic class T4111861 {
3244498Sgibbs    public static void main(String... args) throws Exception {
33107178Snjl        new T4111861().run();
3439215Sgibbs    }
3544498Sgibbs
3639215Sgibbs    void run() throws Exception {
3739215Sgibbs        File testSrc = new File(System.getProperty("test.src", "."));
3839215Sgibbs        File a_java = new File(testSrc, "A.java");
39107178Snjl        javac("-d", ".", a_java.getPath());
4039215Sgibbs
4139215Sgibbs        String out = javap("-classpath", ".", "-constants", "A");
42107178Snjl
43109161Snjl        String a = read(a_java);
44107178Snjl
45107178Snjl        if (!filter(out).equals(filter(read(a_java)))) {
46107178Snjl            System.out.println(out);
47107178Snjl            throw new Exception("unexpected output");
48107178Snjl        }
4939215Sgibbs    }
50107178Snjl
5139215Sgibbs    String javac(String... args) throws Exception {
52107178Snjl        StringWriter sw = new StringWriter();
5339215Sgibbs        PrintWriter pw = new PrintWriter(sw);
54107178Snjl        int rc = com.sun.tools.javac.Main.compile(args, pw);
55107178Snjl        if (rc != 0)
56107178Snjl            throw new Exception("javac failed, rc=" + rc);
57107178Snjl        return sw.toString();
58107178Snjl    }
59107178Snjl
60107178Snjl    String javap(String... args) throws Exception {
61107178Snjl        StringWriter sw = new StringWriter();
62107178Snjl        PrintWriter pw = new PrintWriter(sw);
63107178Snjl        int rc = com.sun.tools.javap.Main.run(args, pw);
64107178Snjl        if (rc != 0)
65107178Snjl            throw new Exception("javap failed, rc=" + rc);
66107178Snjl        return sw.toString();
67107178Snjl    }
68107178Snjl
69107178Snjl    String read(File f) throws IOException {
70107178Snjl        StringBuilder sb = new StringBuilder();
71107178Snjl        BufferedReader in = new BufferedReader(new FileReader(f));
72107178Snjl        try {
73107178Snjl            String line;
74107178Snjl            while ((line = in.readLine()) != null) {
7544498Sgibbs                sb.append(line);
7644498Sgibbs                sb.append('\n');
7744498Sgibbs            }
7844498Sgibbs        } finally {
7939215Sgibbs            in.close();
80107178Snjl        }
81107178Snjl        return sb.toString();
82107178Snjl    }
83107178Snjl
84107178Snjl    // return those lines beginning "public static final"
85107178Snjl    String filter(String s) throws IOException {
86107178Snjl        StringBuilder sb = new StringBuilder();
87107178Snjl        BufferedReader in = new BufferedReader(new StringReader(s));
88107178Snjl        try {
89107178Snjl            String line;
90107178Snjl            while ((line = in.readLine()) != null) {
91107178Snjl                if (line.indexOf("public static final") > 0) {
92107178Snjl                    sb.append(line.trim());
93107178Snjl                    sb.append('\n');
94107178Snjl                }
95107178Snjl            }
9639215Sgibbs        } finally {
9739215Sgibbs            in.close();
9839215Sgibbs        }
9939215Sgibbs        return sb.toString();
100107178Snjl    }
101107178Snjl}
102107178Snjl