test/tools/javac/limits/NumArgsTest.java

Thu, 27 Jun 2013 00:37:13 -0400

author
emc
date
Thu, 27 Jun 2013 00:37:13 -0400
changeset 1860
c674b396827c
parent 0
959103a6100f
permissions
-rw-r--r--

8014230: Compilation incorrectly succeeds with inner class constructor with 254 parameters
Summary: The compiler does not account fr extra parameters due to inner this parameters
Reviewed-by: jjg

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import com.sun.tools.javac.util.*;
aoqi@0 25 import com.sun.tools.javac.api.*;
aoqi@0 26 import com.sun.tools.javac.file.*;
aoqi@0 27 import java.io.*;
aoqi@0 28 import java.util.*;
aoqi@0 29 import javax.tools.*;
aoqi@0 30
aoqi@0 31 // More general parameter limit testing framework, and designed so
aoqi@0 32 // that it could be expanded into a general limits-testing framework
aoqi@0 33 // in the future.
aoqi@0 34 public class NumArgsTest {
aoqi@0 35
aoqi@0 36 private static final NumArgsTest.NestingDef[] NO_NESTING = {};
aoqi@0 37
aoqi@0 38 // threshold is named as such because "threshold" args is expected
aoqi@0 39 // to pass, and "threshold" + 1 args is expected to fail.
aoqi@0 40 private final int threshold;
aoqi@0 41 private final boolean isStaticMethod;
aoqi@0 42 private final String result;
aoqi@0 43 private final String testName;
aoqi@0 44 private final String methodName;
aoqi@0 45 private final NestingDef[] nesting;
aoqi@0 46 private final File testdir;
aoqi@0 47 private final JavacTool tool = JavacTool.create();
aoqi@0 48 private final JavacFileManager fm =
aoqi@0 49 tool.getStandardFileManager(null, null, null);
aoqi@0 50 private int errors = 0;
aoqi@0 51
aoqi@0 52 public NumArgsTest(final int threshold,
aoqi@0 53 final boolean isStaticMethod,
aoqi@0 54 final String result,
aoqi@0 55 final String methodName,
aoqi@0 56 final String testName,
aoqi@0 57 final NestingDef[] nesting) {
aoqi@0 58 this.threshold = threshold;
aoqi@0 59 this.isStaticMethod = isStaticMethod;
aoqi@0 60 this.result = result;
aoqi@0 61 this.methodName = methodName;
aoqi@0 62 this.testName = testName;
aoqi@0 63 this.nesting = nesting;
aoqi@0 64 testdir = new File(testName);
aoqi@0 65 testdir.mkdir();
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 public NumArgsTest(final int threshold,
aoqi@0 69 final boolean isStaticMethod,
aoqi@0 70 final String result,
aoqi@0 71 final String methodName,
aoqi@0 72 final String testName) {
aoqi@0 73 this(threshold, isStaticMethod, result, methodName,
aoqi@0 74 testName, NO_NESTING);
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 public NumArgsTest(final int threshold,
aoqi@0 78 final String result,
aoqi@0 79 final String methodName,
aoqi@0 80 final String testName,
aoqi@0 81 final NestingDef[] nesting) {
aoqi@0 82 this(threshold, false, result, methodName, testName, nesting);
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 public NumArgsTest(final int threshold,
aoqi@0 86 final String result,
aoqi@0 87 final String methodName,
aoqi@0 88 final String testName) {
aoqi@0 89 this(threshold, false, result, methodName, testName, NO_NESTING);
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 public NumArgsTest(final int threshold,
aoqi@0 93 final String testName,
aoqi@0 94 final NestingDef[] nesting) {
aoqi@0 95 this(threshold, null, null, testName, nesting);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 public NumArgsTest(final int threshold,
aoqi@0 99 final String testName) {
aoqi@0 100 this(threshold, null, null, testName, NO_NESTING);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public NumArgsTest(final int threshold,
aoqi@0 104 final String testName,
aoqi@0 105 final String constructorName,
aoqi@0 106 final NestingDef[] nesting) {
aoqi@0 107 this(threshold, null, constructorName, testName, nesting);
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 protected void writeArgs(final int num, final PrintWriter stream)
aoqi@0 111 throws IOException {
aoqi@0 112 stream.print("int x1");
aoqi@0 113 for(int i = 1; i < num; i++)
aoqi@0 114 stream.print(", int x" + (i + 1));
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 protected void writeMethod(final int num,
aoqi@0 118 final String name,
aoqi@0 119 final PrintWriter stream)
aoqi@0 120 throws IOException {
aoqi@0 121 stream.write("public ");
aoqi@0 122 if (isStaticMethod) stream.write("static ");
aoqi@0 123 if (result == null)
aoqi@0 124 stream.write("");
aoqi@0 125 else {
aoqi@0 126 stream.write(result);
aoqi@0 127 stream.write(" ");
aoqi@0 128 }
aoqi@0 129 stream.write(name);
aoqi@0 130 stream.write("(");
aoqi@0 131 writeArgs(num, stream);
aoqi@0 132 stream.write(") {}\n");
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 protected void writeJavaFile(final int num,
aoqi@0 136 final boolean pass,
aoqi@0 137 final PrintWriter stream)
aoqi@0 138 throws IOException {
aoqi@0 139 final String fullName = testName + (pass ? "Pass" : "Fail");
aoqi@0 140 stream.write("public class ");
aoqi@0 141 stream.write(fullName);
aoqi@0 142 stream.write(" {\n");
aoqi@0 143 for(int i = 0; i < nesting.length; i++)
aoqi@0 144 nesting[i].writeBefore(stream);
aoqi@0 145 if (null == methodName)
aoqi@0 146 writeMethod(num, fullName, stream);
aoqi@0 147 else
aoqi@0 148 writeMethod(num, methodName, stream);
aoqi@0 149 for(int i = nesting.length - 1; i >= 0; i--)
aoqi@0 150 nesting[i].writeAfter(stream);
aoqi@0 151 stream.write("}\n");
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 public void runTest() throws Exception {
aoqi@0 155 // Run the pass test
aoqi@0 156 final String passTestName = testName + "Pass.java";
aoqi@0 157 final StringWriter passBody = new StringWriter();
aoqi@0 158 final PrintWriter passStream = new PrintWriter(passBody);
aoqi@0 159 final File passFile = new File(testdir, passTestName);
aoqi@0 160 final FileWriter passWriter = new FileWriter(passFile);
aoqi@0 161
aoqi@0 162 writeJavaFile(threshold, true, passStream);
aoqi@0 163 passStream.close();
aoqi@0 164 passWriter.write(passBody.toString());
aoqi@0 165 passWriter.close();
aoqi@0 166
aoqi@0 167 final StringWriter passSW = new StringWriter();
aoqi@0 168 final String[] passArgs = { passFile.toString() };
aoqi@0 169 final Iterable<? extends JavaFileObject> passFiles =
aoqi@0 170 fm.getJavaFileObjectsFromFiles(Arrays.asList(passFile));
aoqi@0 171 final JavaCompiler.CompilationTask passTask =
aoqi@0 172 tool.getTask(passSW, fm, null, null, null, passFiles);
aoqi@0 173
aoqi@0 174 if (!passTask.call()) {
aoqi@0 175 errors++;
aoqi@0 176 System.err.println("Compilation unexpectedly failed. Body:\n" +
aoqi@0 177 passBody);
aoqi@0 178 System.err.println("Output:\n" + passSW.toString());
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 // Run the fail test
aoqi@0 182 final String failTestName = testName + "Fail.java";
aoqi@0 183 final StringWriter failBody = new StringWriter();
aoqi@0 184 final PrintWriter failStream = new PrintWriter(failBody);
aoqi@0 185 final File failFile = new File(testdir, failTestName);
aoqi@0 186 final FileWriter failWriter = new FileWriter(failFile);
aoqi@0 187
aoqi@0 188 writeJavaFile(threshold + 1, false, failStream);
aoqi@0 189 failStream.close();
aoqi@0 190 failWriter.write(failBody.toString());
aoqi@0 191 failWriter.close();
aoqi@0 192
aoqi@0 193 final StringWriter failSW = new StringWriter();
aoqi@0 194 final TestDiagnosticHandler failDiag =
aoqi@0 195 new TestDiagnosticHandler("compiler.err.limit.parameters");
aoqi@0 196 final Iterable<? extends JavaFileObject> failFiles =
aoqi@0 197 fm.getJavaFileObjectsFromFiles(Arrays.asList(failFile));
aoqi@0 198 final JavaCompiler.CompilationTask failTask =
aoqi@0 199 tool.getTask(failSW,
aoqi@0 200 tool.getStandardFileManager(null, null, null),
aoqi@0 201 failDiag,
aoqi@0 202 null,
aoqi@0 203 null,
aoqi@0 204 failFiles);
aoqi@0 205
aoqi@0 206 if (failTask.call()) {
aoqi@0 207 errors++;
aoqi@0 208 System.err.println("Compilation unexpectedly succeeded.");
aoqi@0 209 System.err.println("Input:\n" + failBody);
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 if (!failDiag.sawError) {
aoqi@0 213 errors++;
aoqi@0 214 System.err.println("Did not see expected compile error.");
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 if (errors != 0)
aoqi@0 218 throw new RuntimeException("Test failed with " +
aoqi@0 219 errors + " errors");
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 public static NestingDef classNesting(final String name) {
aoqi@0 223 return new NestedClassBuilder(name, false);
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 public static NestingDef classNesting(final String name,
aoqi@0 227 final boolean isStatic) {
aoqi@0 228 return new NestedClassBuilder(name, isStatic);
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 protected interface NestingDef {
aoqi@0 232 public abstract void writeBefore(final PrintWriter stream);
aoqi@0 233 public abstract void writeAfter(final PrintWriter stream);
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 private static class NestedClassBuilder implements NestingDef {
aoqi@0 237 private final String name;
aoqi@0 238 private final boolean isStatic;
aoqi@0 239 public NestedClassBuilder(final String name, final boolean isStatic) {
aoqi@0 240 this.name = name;
aoqi@0 241 this.isStatic = isStatic;
aoqi@0 242 }
aoqi@0 243 public void writeBefore(final PrintWriter stream) {
aoqi@0 244 stream.write("public ");
aoqi@0 245 if (isStatic) stream.write("static");
aoqi@0 246 stream.write(" class ");
aoqi@0 247 stream.write(name);
aoqi@0 248 stream.write(" {\n");
aoqi@0 249 }
aoqi@0 250 public void writeAfter(final PrintWriter stream) {
aoqi@0 251 stream.write("}\n");
aoqi@0 252 }
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 public class TestDiagnosticHandler<T> implements DiagnosticListener<T> {
aoqi@0 256 public boolean sawError;
aoqi@0 257 public final String target;
aoqi@0 258
aoqi@0 259 public TestDiagnosticHandler(final String target) {
aoqi@0 260 this.target = target;
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 public void report(final Diagnostic<? extends T> diag) {
aoqi@0 264 if (diag.getCode().equals(target))
aoqi@0 265 sawError = true;
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 }

mercurial