test/tools/javap/T7004698.java

Thu, 04 Apr 2013 19:05:42 -0700

author
katleman
date
Thu, 04 Apr 2013 19:05:42 -0700
changeset 1662
4a48f3173534
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8-b84 for changeset cfb65ca92082

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

mercurial