< prev index next >

test/langtools/tools/javac/diags/CheckResourceKeys.java

Print this page


   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  */


 101         Set<String> resourceKeys = getResourceKeys();
 102 
 103         if (findDeadKeys)
 104             findDeadKeys(codeStrings, resourceKeys);
 105 
 106         if (findMissingKeys)
 107             findMissingKeys(codeStrings, resourceKeys);
 108 
 109         return (errors == 0);
 110     }
 111 
 112     /**
 113      * Find keys in resource bundles which are probably no longer required.
 114      * A key is probably required if there is a string fragment in the code
 115      * that is part of the resource key, or if the key is well-known
 116      * according to various pragmatic rules.
 117      */
 118     void findDeadKeys(Set<String> codeStrings, Set<String> resourceKeys) {
 119         String[] prefixes = {
 120             "compiler.err.", "compiler.warn.", "compiler.note.", "compiler.misc.",
 121             "javac."

 122         };
 123         for (String rk: resourceKeys) {
 124             // some keys are used directly, without a prefix.
 125             if (codeStrings.contains(rk))
 126                 continue;
 127 
 128             // remove standard prefix
 129             String s = null;
 130             for (int i = 0; i < prefixes.length && s == null; i++) {
 131                 if (rk.startsWith(prefixes[i])) {
 132                     s = rk.substring(prefixes[i].length());
 133                 }
 134             }
 135             if (s == null) {
 136                 error("Resource key does not start with a standard prefix: " + rk);
 137                 continue;
 138             }
 139 
 140             if (codeStrings.contains(s))
 141                 continue;


 379             ClassFile cf = ClassFile.read(in);
 380             for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
 381                 if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) {
 382                     String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value;
 383                     if (v.matches("[A-Za-z0-9-_.]+"))
 384                         results.add(v);
 385                 }
 386             }
 387         } catch (ConstantPoolException ignore) {
 388         } finally {
 389             in.close();
 390         }
 391     }
 392 
 393     /**
 394      * Get the set of keys from the javac resource bundles.
 395      */
 396     Set<String> getResourceKeys() {
 397         Module jdk_compiler = ModuleLayer.boot().findModule("jdk.compiler").get();
 398         Set<String> results = new TreeSet<String>();
 399         for (String name : new String[]{"javac", "compiler"}) {
 400             ResourceBundle b =
 401                     ResourceBundle.getBundle("com.sun.tools.javac.resources." + name, jdk_compiler);
 402             results.addAll(b.keySet());
 403         }
 404         return results;
 405     }
 406 
 407     /**
 408      * Report an error.
 409      */
 410     void error(String msg) {
 411         System.err.println("Error: " + msg);
 412         errors++;
 413     }
 414 
 415     int errors;
 416 }
   1 /*
   2  * Copyright (c) 2010, 2018, 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  */


 101         Set<String> resourceKeys = getResourceKeys();
 102 
 103         if (findDeadKeys)
 104             findDeadKeys(codeStrings, resourceKeys);
 105 
 106         if (findMissingKeys)
 107             findMissingKeys(codeStrings, resourceKeys);
 108 
 109         return (errors == 0);
 110     }
 111 
 112     /**
 113      * Find keys in resource bundles which are probably no longer required.
 114      * A key is probably required if there is a string fragment in the code
 115      * that is part of the resource key, or if the key is well-known
 116      * according to various pragmatic rules.
 117      */
 118     void findDeadKeys(Set<String> codeStrings, Set<String> resourceKeys) {
 119         String[] prefixes = {
 120             "compiler.err.", "compiler.warn.", "compiler.note.", "compiler.misc.",
 121             "javac.",
 122             "launcher.err."
 123         };
 124         for (String rk: resourceKeys) {
 125             // some keys are used directly, without a prefix.
 126             if (codeStrings.contains(rk))
 127                 continue;
 128 
 129             // remove standard prefix
 130             String s = null;
 131             for (int i = 0; i < prefixes.length && s == null; i++) {
 132                 if (rk.startsWith(prefixes[i])) {
 133                     s = rk.substring(prefixes[i].length());
 134                 }
 135             }
 136             if (s == null) {
 137                 error("Resource key does not start with a standard prefix: " + rk);
 138                 continue;
 139             }
 140 
 141             if (codeStrings.contains(s))
 142                 continue;


 380             ClassFile cf = ClassFile.read(in);
 381             for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
 382                 if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) {
 383                     String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value;
 384                     if (v.matches("[A-Za-z0-9-_.]+"))
 385                         results.add(v);
 386                 }
 387             }
 388         } catch (ConstantPoolException ignore) {
 389         } finally {
 390             in.close();
 391         }
 392     }
 393 
 394     /**
 395      * Get the set of keys from the javac resource bundles.
 396      */
 397     Set<String> getResourceKeys() {
 398         Module jdk_compiler = ModuleLayer.boot().findModule("jdk.compiler").get();
 399         Set<String> results = new TreeSet<String>();
 400         for (String name : new String[]{"javac", "compiler", "launcher"}) {
 401             ResourceBundle b =
 402                     ResourceBundle.getBundle("com.sun.tools.javac.resources." + name, jdk_compiler);
 403             results.addAll(b.keySet());
 404         }
 405         return results;
 406     }
 407 
 408     /**
 409      * Report an error.
 410      */
 411     void error(String msg) {
 412         System.err.println("Error: " + msg);
 413         errors++;
 414     }
 415 
 416     int errors;
 417 }
< prev index next >