test/tools/javac/diags/CheckExamples.java

Sat, 06 Oct 2012 10:35:38 +0100

author
mcimadamore
date
Sat, 06 Oct 2012 10:35:38 +0100
changeset 1352
d4b3cb1ece84
parent 1305
9d47f4850714
child 1409
33abf479f202
permissions
-rw-r--r--

7177386: Add attribution support for method references
Summary: Add type-checking/lookup routines for method references
Reviewed-by: jjg, dlsmith

     1 /*
     2  * Copyright (c) 2010, 2012, 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 6968063 7127924
    27  * @summary provide examples of code that generate diagnostics
    28  * @build Example CheckExamples
    29  * @run main/othervm CheckExamples
    30  */
    31 /*
    32  *      See CR 7127924 for info on why othervm is used.
    33  */
    35 import java.io.*;
    36 import java.util.*;
    38 /**
    39  * Check invariants for a set of examples.
    40  * -- each example should exactly declare the keys that will be generated when
    41  *      it is run.
    42  * -- together, the examples should cover the set of resource keys in the
    43  *      compiler.properties bundle. A list of exceptions may be given in the
    44  *      not-yet.txt file. Entries on the not-yet.txt list should not be
    45  *      covered by examples.
    46  * When new keys are added to the resource bundle, it is strongly recommended
    47  * that corresponding new examples be added here, if at all practical, instead
    48  * of simply and lazily being added to the not-yet.txt list.
    49  */
    50 public class CheckExamples {
    51     /**
    52      * Standard entry point.
    53      */
    54     public static void main(String... args) throws Exception {
    55         new CheckExamples().run();
    56     }
    58     /**
    59      * Run the test.
    60      */
    61     void run() throws Exception {
    62         Set<Example> examples = getExamples();
    64         Set<String> notYetList = getNotYetList();
    65         Set<String> declaredKeys = new TreeSet<String>();
    66         for (Example e: examples) {
    67             Set<String> e_decl = e.getDeclaredKeys();
    68             Set<String> e_actual = e.getActualKeys();
    69             for (String k: e_decl) {
    70                 if (!e_actual.contains(k))
    71                     error("Example " + e + " declares key " + k + " but does not generate it");
    72             }
    73             for (String k: e_actual) {
    74                 if (!e_decl.contains(k))
    75                     error("Example " + e + " generates key " + k + " but does not declare it");
    76             }
    77             for (String k: e.getDeclaredKeys()) {
    78                 if (notYetList.contains(k))
    79                     error("Example " + e + " declares key " + k + " which is also on the \"not yet\" list");
    80                 declaredKeys.add(k);
    81             }
    82         }
    84         ResourceBundle b =
    85             ResourceBundle.getBundle("com.sun.tools.javac.resources.compiler");
    86         Set<String> resourceKeys = new TreeSet<String>(b.keySet());
    88         for (String dk: declaredKeys) {
    89             if (!resourceKeys.contains(dk))
    90                 error("Key " + dk + " is declared in tests but is not a valid key in resource bundle");
    91         }
    93         for (String nk: notYetList) {
    94             if (!resourceKeys.contains(nk))
    95                 error("Key " + nk + " is declared in not-yet list but is not a valid key in resource bundle");
    96         }
    98         for (String rk: resourceKeys) {
    99             if (!declaredKeys.contains(rk) && !notYetList.contains(rk))
   100                 error("Key " + rk + " is declared in resource bundle but is not in tests or not-yet list");
   101         }
   103         System.err.println(examples.size() + " examples checked");
   104         System.err.println(notYetList.size() + " keys on not-yet list");
   106         Counts declaredCounts = new Counts(declaredKeys);
   107         Counts resourceCounts = new Counts(resourceKeys);
   108         List<String> rows = new ArrayList<String>(Arrays.asList(Counts.prefixes));
   109         rows.add("other");
   110         rows.add("total");
   111         System.err.println();
   112         System.err.println(String.format("%-14s %15s %15s %4s",
   113                 "prefix", "#keys in tests", "#keys in javac", "%"));
   114         for (String p: rows) {
   115             int d = declaredCounts.get(p);
   116             int r = resourceCounts.get(p);
   117             System.err.print(String.format("%-14s %15d %15d", p, d, r));
   118             if (r != 0)
   119                 System.err.print(String.format(" %3d%%", (d * 100) / r));
   120             System.err.println();
   121         }
   123         if (errors > 0)
   124             throw new Exception(errors + " errors occurred.");
   125     }
   127     /**
   128      * Get the complete set of examples to be checked.
   129      */
   130     Set<Example> getExamples() {
   131         Set<Example> results = new TreeSet<Example>();
   132         File testSrc = new File(System.getProperty("test.src"));
   133         File examples = new File(testSrc, "examples");
   134         for (File f: examples.listFiles()) {
   135             if (isValidExample(f))
   136                 results.add(new Example(f));
   137         }
   138         return results;
   139     }
   141     boolean isValidExample(File f) {
   142         return (f.isDirectory() && f.list().length > 0) ||
   143                 (f.isFile() && f.getName().endsWith(".java"));
   144     }
   146     /**
   147      * Get the contents of the "not-yet" list.
   148      */
   149     Set<String> getNotYetList() {
   150         Set<String> results = new TreeSet<String>();
   151         File testSrc = new File(System.getProperty("test.src"));
   152         File notYetList = new File(testSrc, "examples.not-yet.txt");
   153         try {
   154             String[] lines = read(notYetList).split("[\r\n]");
   155             for (String line: lines) {
   156                 int hash = line.indexOf("#");
   157                 if (hash != -1)
   158                     line = line.substring(0, hash).trim();
   159                 if (line.matches("[A-Za-z0-9-_.]+"))
   160                     results.add(line);
   161             }
   162         } catch (IOException e) {
   163             throw new Error(e);
   164         }
   165         return results;
   166     }
   168     /**
   169      * Read the contents of a file.
   170      */
   171     String read(File f) throws IOException {
   172         byte[] bytes = new byte[(int) f.length()];
   173         DataInputStream in = new DataInputStream(new FileInputStream(f));
   174         try {
   175             in.readFully(bytes);
   176         } finally {
   177             in.close();
   178         }
   179         return new String(bytes);
   180     }
   182     /**
   183      * Report an error.
   184      */
   185     void error(String msg) {
   186         System.err.println("Error: " + msg);
   187         errors++;
   188     }
   190     int errors;
   192     static class Counts {
   193         static String[] prefixes = {
   194             "compiler.err.",
   195             "compiler.warn.",
   196             "compiler.note.",
   197             "compiler.misc."
   198         };
   200         Counts(Set<String> keys) {
   201             nextKey:
   202             for (String k: keys) {
   203                 for (String p: prefixes) {
   204                     if (k.startsWith(p)) {
   205                         inc(p);
   206                         continue nextKey;
   207                     }
   208                 }
   209                 inc("other");
   210             }
   211             table.put("total", keys.size());
   212         }
   214         int get(String p) {
   215              Integer i = table.get(p);
   216              return (i == null ? 0 : i);
   217         }
   219         void inc(String p) {
   220             Integer i = table.get(p);
   221             table.put(p, (i == null ? 1 : i + 1));
   222         }
   224         Map<String,Integer> table = new HashMap<String,Integer>();
   225     };
   226 }

mercurial