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