jlahoda@2413: /* jlahoda@2413: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. jlahoda@2413: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jlahoda@2413: * jlahoda@2413: * This code is free software; you can redistribute it and/or modify it jlahoda@2413: * under the terms of the GNU General Public License version 2 only, as jlahoda@2413: * published by the Free Software Foundation. jlahoda@2413: * jlahoda@2413: * This code is distributed in the hope that it will be useful, but WITHOUT jlahoda@2413: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jlahoda@2413: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jlahoda@2413: * version 2 for more details (a copy is included in the LICENSE file that jlahoda@2413: * accompanied this code). jlahoda@2413: * jlahoda@2413: * You should have received a copy of the GNU General Public License version jlahoda@2413: * 2 along with this work; if not, write to the Free Software Foundation, jlahoda@2413: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jlahoda@2413: * jlahoda@2413: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jlahoda@2413: * or visit www.oracle.com if you need additional information or have any jlahoda@2413: * questions. jlahoda@2413: */ jlahoda@2413: jlahoda@2413: /* jlahoda@2413: * @test jlahoda@2413: * @bug 8029800 jlahoda@2413: * @summary String.toLowerCase()/toUpperCase is generally dangerous, check it is not used in langtools jlahoda@2413: */ jlahoda@2413: jlahoda@2413: import java.io.*; jlahoda@2413: import java.util.*; jlahoda@2413: import javax.tools.*; jlahoda@2413: import com.sun.tools.classfile.*; jlahoda@2413: import com.sun.tools.classfile.ConstantPool.CONSTANT_Methodref_info; jlahoda@2413: jlahoda@2413: public class NoStringToLower { jlahoda@2413: public static void main(String... args) throws Exception { jlahoda@2413: NoStringToLower c = new NoStringToLower(); jlahoda@2413: if (c.run(args)) jlahoda@2413: return; jlahoda@2413: jlahoda@2413: if (is_jtreg()) jlahoda@2413: throw new Exception(c.errors + " errors occurred"); jlahoda@2413: else jlahoda@2413: System.exit(1); jlahoda@2413: } jlahoda@2413: jlahoda@2413: static boolean is_jtreg() { jlahoda@2413: return (System.getProperty("test.src") != null); jlahoda@2413: } jlahoda@2413: jlahoda@2413: /** jlahoda@2413: * Main entry point. jlahoda@2413: */ jlahoda@2413: boolean run(String... args) throws Exception { jlahoda@2413: JavaCompiler c = ToolProvider.getSystemJavaCompiler(); jlahoda@2413: JavaFileManager fm = c.getStandardFileManager(null, null, null); jlahoda@2413: JavaFileManager.Location javacLoc = findJavacLocation(fm); jlahoda@2413: String[] pkgs = { jlahoda@2413: "javax.annotation.processing", jlahoda@2413: "javax.lang.model", jlahoda@2413: "javax.tools", jlahoda@2413: "com.sun.source", emc@2414: "com.sun.tools.classfile", emc@2414: "com.sun.tools.doclet", emc@2414: "com.sun.tools.doclint", emc@2414: "com.sun.tools.javac", emc@2414: "com.sun.tools.javadoc", emc@2414: "com.sun.tools.javah", emc@2414: "com.sun.tools.javap", emc@2414: "com.sun.tools.jdeps", emc@2414: "com.sun.tools.sjavac" jlahoda@2413: }; jlahoda@2413: for (String pkg: pkgs) { jlahoda@2413: for (JavaFileObject fo: fm.list(javacLoc, jlahoda@2413: pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) { jlahoda@2413: scan(fo); jlahoda@2413: } jlahoda@2413: } jlahoda@2413: jlahoda@2413: return (errors == 0); jlahoda@2413: } jlahoda@2413: jlahoda@2413: // depending on how the test is run, javac may be on bootclasspath or classpath jlahoda@2413: JavaFileManager.Location findJavacLocation(JavaFileManager fm) { jlahoda@2413: JavaFileManager.Location[] locns = jlahoda@2413: { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH }; jlahoda@2413: try { jlahoda@2413: for (JavaFileManager.Location l: locns) { jlahoda@2413: JavaFileObject fo = fm.getJavaFileForInput(l, jlahoda@2413: "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS); jlahoda@2413: if (fo != null) jlahoda@2413: return l; jlahoda@2413: } jlahoda@2413: } catch (IOException e) { jlahoda@2413: throw new Error(e); jlahoda@2413: } jlahoda@2413: throw new IllegalStateException("Cannot find javac"); jlahoda@2413: } jlahoda@2413: jlahoda@2413: /** jlahoda@2413: * Verify there are no references to String.toLowerCase() in a class file. jlahoda@2413: */ jlahoda@2413: void scan(JavaFileObject fo) throws IOException { jlahoda@2413: InputStream in = fo.openInputStream(); jlahoda@2413: try { jlahoda@2413: ClassFile cf = ClassFile.read(in); jlahoda@2413: for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) { jlahoda@2413: if (cpinfo.getTag() == ConstantPool.CONSTANT_Methodref) { jlahoda@2413: CONSTANT_Methodref_info ref = (CONSTANT_Methodref_info) cpinfo; jlahoda@2413: String methodDesc = ref.getClassInfo().getName() + "." + ref.getNameAndTypeInfo().getName() + ":" + ref.getNameAndTypeInfo().getType(); jlahoda@2413: jlahoda@2413: if ("java/lang/String.toLowerCase:()Ljava/lang/String;".equals(methodDesc)) { jlahoda@2413: error("found reference to String.toLowerCase() in: " + fo.getName()); jlahoda@2413: } jlahoda@2413: if ("java/lang/String.toUpperCase:()Ljava/lang/String;".equals(methodDesc)) { jlahoda@2413: error("found reference to String.toLowerCase() in: " + fo.getName()); jlahoda@2413: } jlahoda@2413: } jlahoda@2413: } jlahoda@2413: } catch (ConstantPoolException ignore) { jlahoda@2413: } finally { jlahoda@2413: in.close(); jlahoda@2413: } jlahoda@2413: } jlahoda@2413: jlahoda@2413: /** jlahoda@2413: * Report an error. jlahoda@2413: */ jlahoda@2413: void error(String msg) { jlahoda@2413: System.err.println("Error: " + msg); jlahoda@2413: errors++; jlahoda@2413: } jlahoda@2413: jlahoda@2413: int errors; jlahoda@2413: }