1 /*
   2  * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 
  26 /*
  27  * @test
  28  * @bug 4111861
  29  * @summary static final field contents are not displayed
  30  * @modules jdk.jdeps
  31  */
  32 public class T4111861 {
  33     public static void main(String... args) throws Exception {
  34         new T4111861().run();
  35     }
  36 
  37     void run() throws Exception {
  38         File testSrc = new File(System.getProperty("test.src", "."));
  39         File a_java = new File(testSrc, "A.java");
  40         javac("-d", ".", a_java.getPath());
  41 
  42         String out = javap("-classpath", ".", "-constants", "A");
  43 
  44         String a = read(a_java);
  45 
  46         if (!filter(out).equals(filter(read(a_java)))) {
  47             System.out.println(out);
  48             throw new Exception("unexpected output");
  49         }
  50     }
  51 
  52     String javac(String... args) throws Exception {
  53         StringWriter sw = new StringWriter();
  54         PrintWriter pw = new PrintWriter(sw);
  55         int rc = com.sun.tools.javac.Main.compile(args, pw);
  56         if (rc != 0)
  57             throw new Exception("javac failed, rc=" + rc);
  58         return sw.toString();
  59     }
  60 
  61     String javap(String... args) throws Exception {
  62         StringWriter sw = new StringWriter();
  63         PrintWriter pw = new PrintWriter(sw);
  64         int rc = com.sun.tools.javap.Main.run(args, pw);
  65         if (rc != 0)
  66             throw new Exception("javap failed, rc=" + rc);
  67         return sw.toString();
  68     }
  69 
  70     String read(File f) throws IOException {
  71         StringBuilder sb = new StringBuilder();
  72         BufferedReader in = new BufferedReader(new FileReader(f));
  73         try {
  74             String line;
  75             while ((line = in.readLine()) != null) {
  76                 sb.append(line);
  77                 sb.append('\n');
  78             }
  79         } finally {
  80             in.close();
  81         }
  82         return sb.toString();
  83     }
  84 
  85     // return those lines beginning "public static final"
  86     String filter(String s) throws IOException {
  87         StringBuilder sb = new StringBuilder();
  88         BufferedReader in = new BufferedReader(new StringReader(s));
  89         try {
  90             String line;
  91             while ((line = in.readLine()) != null) {
  92                 if (line.indexOf("public static final") > 0) {
  93                     sb.append(line.trim());
  94                     sb.append('\n');
  95                 }
  96             }
  97         } finally {
  98             in.close();
  99         }
 100         return sb.toString();
 101     }
 102 }