langtools/test/tools/javac/NoStringToLower.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 22164 c8eb1ae29c58
child 27319 030080f03e4f
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     1
/*
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     4
 *
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     8
 *
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    14
 *
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    18
 *
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    21
 * questions.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    22
 */
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    23
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    24
/*
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    25
 * @test
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    26
 * @bug 8029800
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    27
 * @summary String.toLowerCase()/toUpperCase is generally dangerous, check it is not used in langtools
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    28
 */
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    29
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    30
import java.io.*;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    31
import java.util.*;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    32
import javax.tools.*;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    33
import com.sun.tools.classfile.*;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    34
import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    35
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    36
public class NoStringToLower {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    37
    public static void main(String... args) throws Exception {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    38
        NoStringToLower c = new NoStringToLower();
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    39
        if (c.run(args))
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    40
            return;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    41
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    42
        if (is_jtreg())
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    43
            throw new Exception(c.errors + " errors occurred");
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    44
        else
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    45
            System.exit(1);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    46
    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    47
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    48
    static boolean is_jtreg() {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    49
        return (System.getProperty("test.src") != null);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    50
    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    51
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    52
    /**
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    53
     * Main entry point.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    54
     */
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    55
    boolean run(String... args) throws Exception {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    56
        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    57
        JavaFileManager fm = c.getStandardFileManager(null, null, null);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    58
        JavaFileManager.Location javacLoc = findJavacLocation(fm);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    59
        String[] pkgs = {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    60
            "javax.annotation.processing",
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    61
            "javax.lang.model",
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    62
            "javax.tools",
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    63
            "com.sun.source",
22164
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    64
            "com.sun.tools.classfile",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    65
            "com.sun.tools.doclet",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    66
            "com.sun.tools.doclint",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    67
            "com.sun.tools.javac",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    68
            "com.sun.tools.javadoc",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    69
            "com.sun.tools.javah",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    70
            "com.sun.tools.javap",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    71
            "com.sun.tools.jdeps",
c8eb1ae29c58 8030726: tools/javac/NoStringToLower.java fails due to enforcement no use of String.toLowerCase on non-langtools classes
emc
parents: 22153
diff changeset
    72
            "com.sun.tools.sjavac"
22153
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    73
        };
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    74
        for (String pkg: pkgs) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    75
            for (JavaFileObject fo: fm.list(javacLoc,
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    76
                    pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    77
                scan(fo);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    78
            }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    79
        }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    80
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    81
        return (errors == 0);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    82
    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    83
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    84
    // depending on how the test is run, javac may be on bootclasspath or classpath
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    85
    JavaFileManager.Location findJavacLocation(JavaFileManager fm) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    86
        JavaFileManager.Location[] locns =
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    87
            { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    88
        try {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    89
            for (JavaFileManager.Location l: locns) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    90
                JavaFileObject fo = fm.getJavaFileForInput(l,
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    91
                    "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    92
                if (fo != null)
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    93
                    return l;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    94
            }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    95
        } catch (IOException e) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    96
            throw new Error(e);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    97
        }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    98
        throw new IllegalStateException("Cannot find javac");
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
    99
    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   100
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   101
    /**
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   102
     * Verify there are no references to String.toLowerCase() in a class file.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   103
     */
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   104
    void scan(JavaFileObject fo) throws IOException {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   105
        InputStream in = fo.openInputStream();
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   106
        try {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   107
            ClassFile cf = ClassFile.read(in);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   108
            for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   109
                if (cpinfo.getTag() == ConstantPool.CONSTANT_Methodref) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   110
                    CONSTANT_Methodref_info ref = (CONSTANT_Methodref_info) cpinfo;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   111
                    String methodDesc = ref.getClassInfo().getName() + "." + ref.getNameAndTypeInfo().getName() + ":" + ref.getNameAndTypeInfo().getType();
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   112
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   113
                    if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   114
                        error("found reference to String.toLowerCase() in: " + fo.getName());
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   115
                    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   116
                    if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   117
                        error("found reference to String.toLowerCase() in: " + fo.getName());
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   118
                    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   119
                }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   120
            }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   121
        } catch (ConstantPoolException ignore) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   122
        } finally {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   123
            in.close();
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   124
        }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   125
    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   126
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   127
    /**
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   128
     * Report an error.
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   129
     */
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   130
    void error(String msg) {
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   131
        System.err.println("Error: " + msg);
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   132
        errors++;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   133
    }
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   134
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   135
    int errors;
f9f06fcca59d 8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
diff changeset
   136
}