Mon, 13 Dec 2010 15:11:00 -0800
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
Reviewed-by: jjg, darcy
jjg@657 | 1 | /* |
jjg@657 | 2 | * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
jjg@657 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@657 | 4 | * |
jjg@657 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@657 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@657 | 7 | * published by the Free Software Foundation. |
jjg@657 | 8 | * |
jjg@657 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@657 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@657 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@657 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@657 | 13 | * accompanied this code). |
jjg@657 | 14 | * |
jjg@657 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@657 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@657 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@657 | 18 | * |
jjg@657 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@657 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@657 | 21 | * questions. |
jjg@657 | 22 | */ |
jjg@657 | 23 | |
jjg@657 | 24 | /* |
jjg@657 | 25 | * @test |
jjg@657 | 26 | * @bug 6960424 |
jjg@657 | 27 | * @summary new option -Xpkginfo for better control of when package-info.class is generated |
jjg@657 | 28 | */ |
jjg@657 | 29 | |
jjg@657 | 30 | import java.io.*; |
jjg@657 | 31 | import java.util.*; |
jjg@657 | 32 | |
jjg@657 | 33 | public class TestPkgInfo { |
jjg@657 | 34 | enum OptKind { |
jjg@657 | 35 | NONE(null), |
jjg@657 | 36 | ALWAYS("-Xpkginfo:always"), |
jjg@657 | 37 | NONEMPTY("-Xpkginfo:nonempty"), |
jjg@657 | 38 | LEGACY("-Xpkginfo:legacy"); |
jjg@657 | 39 | OptKind(String opt) { this.opt = opt; } |
jjg@657 | 40 | final String opt; |
jjg@657 | 41 | }; |
jjg@657 | 42 | |
jjg@657 | 43 | public static void main(String... args) throws Exception { |
jjg@657 | 44 | new TestPkgInfo().run(args); |
jjg@657 | 45 | } |
jjg@657 | 46 | |
jjg@657 | 47 | public void run(String... args) throws Exception { |
jjg@657 | 48 | boolean[] booleanValues = { false, true }; |
jjg@657 | 49 | for (OptKind ok: OptKind.values()) { |
jjg@657 | 50 | for (boolean sr: booleanValues) { |
jjg@657 | 51 | for (boolean cr: booleanValues) { |
jjg@657 | 52 | for (boolean rr: booleanValues) { |
jjg@657 | 53 | try { |
jjg@657 | 54 | test(ok, sr, cr, rr); |
jjg@657 | 55 | } catch (Exception e) { |
jjg@657 | 56 | error("Exception: " + e); |
jjg@657 | 57 | } |
jjg@657 | 58 | if (errors > 0) throw new AssertionError(); |
jjg@657 | 59 | } |
jjg@657 | 60 | } |
jjg@657 | 61 | } |
jjg@657 | 62 | } |
jjg@657 | 63 | |
jjg@657 | 64 | if (errors > 0) |
jjg@657 | 65 | throw new Exception(errors + " errors occurred"); |
jjg@657 | 66 | } |
jjg@657 | 67 | |
jjg@657 | 68 | void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception { |
jjg@657 | 69 | count++; |
jjg@657 | 70 | System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr); |
jjg@657 | 71 | |
jjg@657 | 72 | StringBuilder sb = new StringBuilder(); |
jjg@657 | 73 | |
jjg@657 | 74 | // create annotated package statement with all combinations of retention policy |
jjg@657 | 75 | if (sr) sb.append("@SR\n"); |
jjg@657 | 76 | if (cr) sb.append("@CR\n"); |
jjg@657 | 77 | if (rr) sb.append("@RR\n"); |
jjg@657 | 78 | sb.append("package p;\n"); |
jjg@657 | 79 | sb.append("\n"); |
jjg@657 | 80 | |
jjg@657 | 81 | sb.append("import java.lang.annotation.*;\n"); |
jjg@657 | 82 | sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n"); |
jjg@657 | 83 | sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n"); |
jjg@657 | 84 | sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n"); |
jjg@657 | 85 | |
jjg@657 | 86 | // test specific tmp directory |
jjg@657 | 87 | File tmpDir = new File("tmp.test" + count); |
jjg@657 | 88 | File classesDir = new File(tmpDir, "classes"); |
jjg@657 | 89 | classesDir.mkdirs(); |
jjg@657 | 90 | File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java"); |
jjg@657 | 91 | writeFile(pkginfo_java, sb.toString()); |
jjg@657 | 92 | |
jjg@657 | 93 | // build up list of options and files to be compiled |
jjg@657 | 94 | List<String> opts = new ArrayList<String>(); |
jjg@657 | 95 | List<File> files = new ArrayList<File>(); |
jjg@657 | 96 | |
jjg@657 | 97 | opts.add("-d"); |
jjg@657 | 98 | opts.add(classesDir.getPath()); |
jjg@657 | 99 | if (ok.opt != null) |
jjg@657 | 100 | opts.add(ok.opt); |
jjg@657 | 101 | //opts.add("-verbose"); |
jjg@657 | 102 | files.add(pkginfo_java); |
jjg@657 | 103 | |
jjg@657 | 104 | compile(opts, files); |
jjg@657 | 105 | |
jjg@657 | 106 | File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class"); |
jjg@657 | 107 | boolean exists = pkginfo_class.exists(); |
jjg@657 | 108 | |
jjg@657 | 109 | boolean expected; |
jjg@657 | 110 | switch (ok) { |
jjg@657 | 111 | case ALWAYS: |
jjg@657 | 112 | expected = true; |
jjg@657 | 113 | break; |
jjg@657 | 114 | |
jjg@657 | 115 | case LEGACY: |
jjg@657 | 116 | case NONE: |
jjg@657 | 117 | expected = (sr || cr || rr ); // any annotation |
jjg@657 | 118 | break; |
jjg@657 | 119 | |
jjg@657 | 120 | case NONEMPTY: |
jjg@657 | 121 | expected = (cr || rr ); // any annotation in class file |
jjg@657 | 122 | break; |
jjg@657 | 123 | |
jjg@657 | 124 | default: |
jjg@657 | 125 | throw new IllegalStateException(); |
jjg@657 | 126 | } |
jjg@657 | 127 | |
jjg@657 | 128 | if (exists && !expected) |
jjg@657 | 129 | error("package-info.class found but not expected"); |
jjg@657 | 130 | if (!exists && expected) |
jjg@657 | 131 | error("package-info.class expected but not found"); |
jjg@657 | 132 | } |
jjg@657 | 133 | |
jjg@657 | 134 | /** Compile files with options provided. */ |
jjg@657 | 135 | void compile(List<String> opts, List<File> files) throws Exception { |
jjg@657 | 136 | System.err.println("javac: " + opts + " " + files); |
jjg@657 | 137 | List<String> args = new ArrayList<String>(); |
jjg@657 | 138 | args.addAll(opts); |
jjg@657 | 139 | for (File f: files) |
jjg@657 | 140 | args.add(f.getPath()); |
jjg@657 | 141 | StringWriter sw = new StringWriter(); |
jjg@657 | 142 | PrintWriter pw = new PrintWriter(sw); |
jjg@657 | 143 | int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); |
jjg@657 | 144 | pw.flush(); |
jjg@657 | 145 | if (sw.getBuffer().length() > 0) |
jjg@657 | 146 | System.err.println(sw.toString()); |
jjg@657 | 147 | if (rc != 0) |
jjg@657 | 148 | throw new Exception("compilation failed: rc=" + rc); |
jjg@657 | 149 | } |
jjg@657 | 150 | |
jjg@657 | 151 | /** Write a file with a given body. */ |
jjg@657 | 152 | void writeFile(File f, String body) throws Exception { |
jjg@657 | 153 | if (f.getParentFile() != null) |
jjg@657 | 154 | f.getParentFile().mkdirs(); |
jjg@657 | 155 | Writer out = new FileWriter(f); |
jjg@657 | 156 | try { |
jjg@657 | 157 | out.write(body); |
jjg@657 | 158 | } finally { |
jjg@657 | 159 | out.close(); |
jjg@657 | 160 | } |
jjg@657 | 161 | } |
jjg@657 | 162 | |
jjg@657 | 163 | /** Report an error. */ |
jjg@657 | 164 | void error(String msg) { |
jjg@657 | 165 | System.err.println("Error: " + msg); |
jjg@657 | 166 | errors++; |
jjg@657 | 167 | } |
jjg@657 | 168 | |
jjg@657 | 169 | /** Test case counter. */ |
jjg@657 | 170 | int count; |
jjg@657 | 171 | |
jjg@657 | 172 | /** Number of errors found. */ |
jjg@657 | 173 | int errors; |
jjg@657 | 174 | } |