1 /*
   2  * Copyright (c) 2010, 2016, 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 /*
  25  * @test
  26  * @bug 7004698
  27  * @summary javap does not output CharacterRangeTable attributes correctly
  28  * @modules jdk.jdeps/com.sun.tools.javap
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 import java.util.regex.*;
  34 
  35 public class T7004698 {
  36     public static void main(String... args) throws Exception {
  37         new T7004698().run();
  38     }
  39 
  40     void run() throws Exception {
  41         File srcDir = new File(System.getProperty("test.src"));
  42         File srcFile = new File(srcDir, T7004698.class.getSimpleName() + ".java");
  43         File classesDir = new File(".");
  44         compile("-Xjcov",
  45                 "-XaddExports:jdk.jdeps/com.sun.tools.javap=ALL-UNNAMED",
  46                 "-d", classesDir.getPath(),
  47                 srcFile.getPath());
  48 
  49         File classFile = new File(classesDir, T7004698.class.getSimpleName() + ".class");
  50         String out = javap("-v", classFile.getPath());
  51 
  52         Pattern attrBody = Pattern.compile("[0-9a-f, ]+//[-0-9a-z:, ]+");
  53         Pattern endOfAttr = Pattern.compile("(^$|[A-Z][A-Za-z0-9_]+:.*|})");
  54         boolean inAttr = false;
  55         int count = 0;
  56         int errors = 0;
  57         for (String line: out.split(System.getProperty("line.separator"))) {
  58             line = line.trim();
  59             if (line.equals("CharacterRangeTable:")) {
  60                 inAttr = true;
  61                 count++;
  62             } else if (inAttr) {
  63                 if (endOfAttr.matcher(line).matches()) {
  64                     inAttr = false;
  65                 } else if (!attrBody.matcher(line).matches()) {
  66                     System.err.println("unexpected line found: " + line);
  67                     errors++;
  68                 }
  69             }
  70         }
  71         if (count == 0)
  72             throw new Exception("no attribute instances found");
  73 
  74         if (errors > 0)
  75             throw new Exception(errors + " errors found");
  76     }
  77 
  78     void compile(String... args) throws Exception {
  79         StringWriter sw = new StringWriter();
  80         PrintWriter pw = new PrintWriter(sw);
  81         int rc = com.sun.tools.javac.Main.compile(args, pw);
  82         pw.close();
  83         String out = sw.toString();
  84         if (!out.isEmpty())
  85             System.err.println(out);
  86         if (rc != 0)
  87             throw new Exception("javac failed unexpectedly; rc=" + rc);
  88     }
  89 
  90     String javap(String... args) throws Exception {
  91         StringWriter sw = new StringWriter();
  92         PrintWriter pw = new PrintWriter(sw);
  93         int rc = com.sun.tools.javap.Main.run(args, pw);
  94         pw.close();
  95         String out = sw.toString();
  96         if (!out.isEmpty())
  97             System.err.println(out);
  98         if (rc != 0)
  99             throw new Exception("javap failed unexpectedly; rc=" + rc);
 100         return out;
 101     }
 102 }