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  */
  23 
  24 /*
  25  * @test
  26  * @bug 8013357
  27  * @summary javac should generate method parameters correctly.
  28  */
  29 import java.io.File;
  30 import java.io.FileWriter;
  31 import java.io.IOException;
  32 import java.io.PrintWriter;
  33 import java.io.StringWriter;
  34 
  35 public class ObjectZeroCompare {
  36     static final String LeftObject_name = "LeftObject";
  37     static final String LeftObject_contents =
  38         "public class LeftObject {\n" +
  39         "  LeftObject() {}\n" +
  40         "  boolean foo0(Object o) {\n" +
  41         "    return o == 0;\n" +
  42         "  }\n" +
  43         "}\n";
  44     static final String RightObject_name = "RightObject";
  45     static final String RightObject_contents =
  46         "public class RightObject {\n" +
  47         "  RightObject() {}\n" +
  48         "  boolean foo0(Object o) {\n" +
  49         "    return 0 == o;\n" +
  50         "  }\n" +
  51         "}\n";
  52     static final String LeftUncastable_name = "LeftUncastable";
  53     static final String LeftUncastable_contents =
  54         "public class LeftUncastable {\n" +
  55         "  LeftUncastable() {}\n" +
  56         "  boolean foo0(String o, Integer i) {\n" +
  57         "    return o == i;\n" +
  58         "  }\n" +
  59         "}\n";
  60     static final String RightUncastable_name = "RightUncastable";
  61     static final String RightUncastable_contents =
  62         "public class RightUncastable {\n" +
  63         "  RightUncastable() {}\n" +
  64         "  boolean foo0(String o, Integer i) {\n" +
  65         "    return i == o;\n" +
  66         "  }\n" +
  67         "}\n";
  68     static final String LeftCastable_name = "LeftCastable";
  69     static final String LeftCastable_contents =
  70         "public class LeftCastable {\n" +
  71         "  LeftCastable() {}\n" +
  72         "  boolean foo0(Number n, Integer i) {\n" +
  73         "    return n == i;\n" +
  74         "  }\n" +
  75         "}\n";
  76     static final String RightCastable_name = "RightCastable";
  77     static final String RightCastable_contents =
  78         "public class RightCastable {\n" +
  79         "  RightCastable() {}\n" +
  80         "  boolean foo0(Number n, Integer i) {\n" +
  81         "    return i == n;\n" +
  82         "  }\n" +
  83         "}\n";
  84     static final String LeftNumber_name = "LeftNumber";
  85     static final String LeftNumber_contents =
  86         "public class LeftNumber {\n" +
  87         "  LeftNumber() {}\n" +
  88         "  boolean bar0(Number o) {\n" +
  89         "    return o == 0;\n" +
  90         "  }\n" +
  91         "}\n";
  92     static final String RightNumber_name = "RightNumber";
  93     static final String RightNumber_contents =
  94         "public class RightNumber {\n" +
  95         "  RightNumber() {}\n" +
  96         "  boolean bar0(Number o) {\n" +
  97         "    return 0 == o;\n" +
  98         "  }\n" +
  99         "}\n";
 100     static final String LeftInteger_name = "LeftInteger";
 101     static final String LeftInteger_contents =
 102         "public class LeftInteger {\n" +
 103         "  LeftInteger() {}\n" +
 104         "  boolean baz0(Integer o) {\n" +
 105         "    return 0 == o;\n" +
 106         "  }\n" +
 107         "}\n";
 108     static final String RightInteger_name = "RightInteger";
 109     static final String RightInteger_contents =
 110         "public class RightInteger {\n" +
 111         "  RightInteger() {}\n" +
 112         "  boolean baz0(Integer o) {\n" +
 113         "    return 0 == o;\n" +
 114         "  }\n" +
 115         "}\n";
 116     static final String LeftDouble_name = "LeftDouble";
 117     static final String LeftDouble_contents =
 118         "public class LeftDouble {\n" +
 119         "  LeftDouble() {}\n" +
 120         "  boolean qux0(Double o) {\n" +
 121         "    return o == 0.0;\n" +
 122         "  }\n" +
 123         "}\n";
 124     static final String RightDouble_name = "RightDouble";
 125     static final String RightDouble_contents =
 126         "public class RightDouble {\n" +
 127         "  RightDouble() {}\n" +
 128         "  boolean qux0(Double o) {\n" +
 129         "    return o == 0.0;\n" +
 130         "  }\n" +
 131         "}\n";
 132     static final String LeftBoolean_name = "LeftBoolean";
 133     static final String LeftBoolean_contents =
 134         "public class LeftBoolean {\n" +
 135         "  LeftBoolean() {}\n" +
 136         "  boolean quux0(Boolean b) {\n" +
 137         "    return true == b;\n" +
 138         "  }\n" +
 139         "}\n";
 140     static final String RightBoolean_name = "RightBoolean";
 141     static final String RightBoolean_contents =
 142         "public class RightBoolean {\n" +
 143         "  RightBoolean() {}\n" +
 144         "  boolean quux0(Boolean b) {\n" +
 145         "    return true == b;\n" +
 146         "  }\n" +
 147         "}\n";
 148     static final File classesdir = new File("8013357");
 149 
 150     private int errors = 0;
 151 
 152     public static void main(String... args) throws Exception {
 153         new ObjectZeroCompare().run();
 154     }
 155 
 156     void run() throws Exception {
 157         classesdir.mkdir();
 158         final File LeftObject_java =
 159             writeFile(classesdir, LeftObject_name + ".java",
 160                       LeftObject_contents);
 161         final File RightObject_java =
 162             writeFile(classesdir, RightObject_name + ".java",
 163                       RightObject_contents);
 164         final File LeftUncastable_java =
 165             writeFile(classesdir, LeftUncastable_name + ".java",
 166                       LeftUncastable_contents);
 167         final File RightUncastable_java =
 168             writeFile(classesdir, RightUncastable_name + ".java",
 169                       RightUncastable_contents);
 170         final File LeftNumber_java =
 171             writeFile(classesdir, LeftNumber_name + ".java",
 172                       LeftNumber_contents);
 173         final File RightNumber_java =
 174             writeFile(classesdir, RightNumber_name + ".java",
 175                       RightNumber_contents);
 176         final File LeftCastable_java =
 177             writeFile(classesdir, LeftCastable_name + ".java",
 178                       LeftCastable_contents);
 179         final File RightCastable_java =
 180             writeFile(classesdir, RightCastable_name + ".java",
 181                       RightCastable_contents);
 182         final File LeftInteger_java =
 183             writeFile(classesdir, LeftInteger_name + ".java",
 184                       LeftInteger_contents);
 185         final File RightInteger_java =
 186             writeFile(classesdir, RightInteger_name + ".java",
 187                       RightInteger_contents);
 188         final File LeftDouble_java =
 189             writeFile(classesdir, LeftDouble_name + ".java",
 190                       LeftDouble_contents);
 191         final File RightDouble_java =
 192             writeFile(classesdir, RightDouble_name + ".java",
 193                       RightDouble_contents);
 194         final File LeftBoolean_java =
 195             writeFile(classesdir, LeftBoolean_name + ".java",
 196                       LeftBoolean_contents);
 197         final File RightBoolean_java =
 198             writeFile(classesdir, RightBoolean_name + ".java",
 199                       RightBoolean_contents);
 200         assert_compile_fail(LeftObject_java);
 201         assert_compile_fail(RightObject_java);
 202         assert_compile_fail(LeftUncastable_java);
 203         assert_compile_fail(RightUncastable_java);
 204         assert_compile_succeed(LeftNumber_java);
 205         assert_compile_succeed(RightNumber_java);
 206         assert_compile_succeed(LeftCastable_java);
 207         assert_compile_succeed(RightCastable_java);
 208         assert_compile_succeed(LeftInteger_java);
 209         assert_compile_succeed(RightInteger_java);
 210         assert_compile_succeed(LeftDouble_java);
 211         assert_compile_succeed(RightDouble_java);
 212         assert_compile_succeed(LeftBoolean_java);
 213         assert_compile_succeed(RightBoolean_java);
 214         if (errors != 0)
 215             throw new Exception("ObjectZeroCompare test failed with " +
 216                                 errors + " errors.");
 217     }
 218 
 219     int compile(final File file) {
 220         final String filename = file.getPath();
 221         final String[] args = { filename };
 222         final StringWriter sw = new StringWriter();
 223         final PrintWriter pw = new PrintWriter(sw);
 224         final int rc = com.sun.tools.javac.Main.compile(args, pw);
 225         pw.close();
 226         System.err.println("Compiled " + filename + ", output:\n" +
 227                            sw.toString());
 228         return rc;
 229     }
 230 
 231     void assert_compile_fail(final File file) {
 232         final int rc = compile(file);
 233         if (rc == 0) {
 234             System.err.println("Compilation of " + file.getName() +
 235                                " didn't fail as expected.");
 236             errors++;
 237         }
 238     }
 239 
 240     void assert_compile_succeed(final File file) {
 241         final int rc = compile(file);
 242         if (rc != 0) {
 243             System.err.println("Compilation of " + file.getName() +
 244                                " didn't succeed as expected.");
 245             errors++;
 246         }
 247     }
 248 
 249     File writeFile(final File dir,
 250                    final String path,
 251                    final String body) throws IOException {
 252         final File f = new File(dir, path);
 253         f.getParentFile().mkdirs();
 254         final FileWriter out = new FileWriter(f);
 255         out.write(body);
 256         out.close();
 257         return f;
 258     }
 259 
 260 }