test/tools/javac/NoStringToLower.java

Thu, 19 Dec 2013 11:38:45 -0500

author
emc
date
Thu, 19 Dec 2013 11:38:45 -0500
changeset 2414
17ce329d7bd0
parent 2413
fe033d997ddf
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
Summary: Fix NoStringToLower test to only enforce ban on String.toLowerCase on langtools classes
Reviewed-by: vromero, jfranck
Contributed-by: paul.govereau@oracle.com

     1 /*
     2  * Copyright (c) 2013, 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 8029800
    27  * @summary String.toLowerCase()/toUpperCase is generally dangerous, check it is not used in langtools
    28  */
    30 import java.io.*;
    31 import java.util.*;
    32 import javax.tools.*;
    33 import com.sun.tools.classfile.*;
    34 import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
    36 public class NoStringToLower {
    37     public static void main(String... args) throws Exception {
    38         NoStringToLower c = new NoStringToLower();
    39         if (c.run(args))
    40             return;
    42         if (is_jtreg())
    43             throw new Exception(c.errors + " errors occurred");
    44         else
    45             System.exit(1);
    46     }
    48     static boolean is_jtreg() {
    49         return (System.getProperty("test.src") != null);
    50     }
    52     /**
    53      * Main entry point.
    54      */
    55     boolean run(String... args) throws Exception {
    56         JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    57         JavaFileManager fm = c.getStandardFileManager(null, null, null);
    58         JavaFileManager.Location javacLoc = findJavacLocation(fm);
    59         String[] pkgs = {
    60             "javax.annotation.processing",
    61             "javax.lang.model",
    62             "javax.tools",
    63             "com.sun.source",
    64             "com.sun.tools.classfile",
    65             "com.sun.tools.doclet",
    66             "com.sun.tools.doclint",
    67             "com.sun.tools.javac",
    68             "com.sun.tools.javadoc",
    69             "com.sun.tools.javah",
    70             "com.sun.tools.javap",
    71             "com.sun.tools.jdeps",
    72             "com.sun.tools.sjavac"
    73         };
    74         for (String pkg: pkgs) {
    75             for (JavaFileObject fo: fm.list(javacLoc,
    76                     pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
    77                 scan(fo);
    78             }
    79         }
    81         return (errors == 0);
    82     }
    84     // depending on how the test is run, javac may be on bootclasspath or classpath
    85     JavaFileManager.Location findJavacLocation(JavaFileManager fm) {
    86         JavaFileManager.Location[] locns =
    87             { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };
    88         try {
    89             for (JavaFileManager.Location l: locns) {
    90                 JavaFileObject fo = fm.getJavaFileForInput(l,
    91                     "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS);
    92                 if (fo != null)
    93                     return l;
    94             }
    95         } catch (IOException e) {
    96             throw new Error(e);
    97         }
    98         throw new IllegalStateException("Cannot find javac");
    99     }
   101     /**
   102      * Verify there are no references to String.toLowerCase() in a class file.
   103      */
   104     void scan(JavaFileObject fo) throws IOException {
   105         InputStream in = fo.openInputStream();
   106         try {
   107             ClassFile cf = ClassFile.read(in);
   108             for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
   109                 if (cpinfo.getTag() == ConstantPool.CONSTANT_Methodref) {
   110                     CONSTANT_Methodref_info ref = (CONSTANT_Methodref_info) cpinfo;
   111                     String methodDesc = ref.getClassInfo().getName() + "." + ref.getNameAndTypeInfo().getName() + ":" + ref.getNameAndTypeInfo().getType();
   113                     if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) {
   114                         error("found reference to String.toLowerCase() in: " + fo.getName());
   115                     }
   116                     if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) {
   117                         error("found reference to String.toLowerCase() in: " + fo.getName());
   118                     }
   119                 }
   120             }
   121         } catch (ConstantPoolException ignore) {
   122         } finally {
   123             in.close();
   124         }
   125     }
   127     /**
   128      * Report an error.
   129      */
   130     void error(String msg) {
   131         System.err.println("Error: " + msg);
   132         errors++;
   133     }
   135     int errors;
   136 }

mercurial