test/tools/javac/varargs/7043922/T7043922.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 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 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7043922
aoqi@0 27 * @summary Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import com.sun.source.util.JavacTask;
aoqi@0 31 import com.sun.tools.javac.api.JavacTool;
aoqi@0 32 import com.sun.tools.javac.util.List;
aoqi@0 33
aoqi@0 34 import java.net.URI;
aoqi@0 35 import java.util.Arrays;
aoqi@0 36 import java.util.Locale;
aoqi@0 37 import javax.tools.Diagnostic;
aoqi@0 38 import javax.tools.JavaCompiler;
aoqi@0 39 import javax.tools.JavaFileObject;
aoqi@0 40 import javax.tools.SimpleJavaFileObject;
aoqi@0 41 import javax.tools.StandardJavaFileManager;
aoqi@0 42 import javax.tools.ToolProvider;
aoqi@0 43
aoqi@0 44 public class T7043922 {
aoqi@0 45
aoqi@0 46 ClassKind[] classKinds;
aoqi@0 47 ConstructorKind[] constructorKinds;
aoqi@0 48
aoqi@0 49 T7043922(ClassKind[] classKinds, ConstructorKind[] constructorKinds) {
aoqi@0 50 this.classKinds = classKinds;
aoqi@0 51 this.constructorKinds = constructorKinds;
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 void compileAndCheck() throws Exception {
aoqi@0 55 JavaSource source = new JavaSource();
aoqi@0 56 ErrorChecker ec = new ErrorChecker();
aoqi@0 57 JavacTask ct = (JavacTask)tool.getTask(null, fm, ec,
aoqi@0 58 null, null, Arrays.asList(source));
aoqi@0 59 ct.analyze();
aoqi@0 60 if (ec.errorFound) {
aoqi@0 61 throw new Error("invalid diagnostics for source:\n" +
aoqi@0 62 source.getCharContent(true) +
aoqi@0 63 "\nCompiler diagnostics:\n" + ec.printDiags());
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 class JavaSource extends SimpleJavaFileObject {
aoqi@0 68
aoqi@0 69 static final String source_template = "#C0 A0 { #K0 }\n" +
aoqi@0 70 "#C1 A1 { #K1 }\n" +
aoqi@0 71 "#C2 A2 { #K2 }\n" +
aoqi@0 72 "class D {\n" +
aoqi@0 73 " void test() {\n" +
aoqi@0 74 " new A0(#V0) {\n" +
aoqi@0 75 " void test() {\n" +
aoqi@0 76 " new A1(#V1) {\n" +
aoqi@0 77 " void test() {\n" +
aoqi@0 78 " new A2(#V2) {};\n" +
aoqi@0 79 " }\n" +
aoqi@0 80 " };\n" +
aoqi@0 81 " }\n" +
aoqi@0 82 " };\n" +
aoqi@0 83 " }\n" +
aoqi@0 84 "}\n";
aoqi@0 85
aoqi@0 86 String source;
aoqi@0 87
aoqi@0 88 public JavaSource() {
aoqi@0 89 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 90 source = source_template;
aoqi@0 91 for (int i = 0; i < 3; i++) {
aoqi@0 92 source = source.replaceAll("#C" + i, classKinds[i].classKind).
aoqi@0 93 replaceAll("#K" + i, classKinds[i].getConstructor("A" + i, constructorKinds[i])).
aoqi@0 94 replaceAll("#V" + i, constructorKinds[i].constrArgs);
aoqi@0 95 }
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 @Override
aoqi@0 99 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 100 return source;
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 /** global decls ***/
aoqi@0 105
aoqi@0 106 enum ConstructorKind {
aoqi@0 107 DEFAULT("", ""),
aoqi@0 108 FIXED_ARITY("String s", "\"\""),
aoqi@0 109 VARIABLE_ARITY("String... ss", "\"\",\"\"");
aoqi@0 110
aoqi@0 111 String constrParam;
aoqi@0 112 String constrArgs;
aoqi@0 113
aoqi@0 114 private ConstructorKind(String constrParam, String constrArgs) {
aoqi@0 115 this.constrParam = constrParam;
aoqi@0 116 this.constrArgs = constrArgs;
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 enum ClassKind {
aoqi@0 121 ABSTRACT("abstract class"),
aoqi@0 122 CLASS("class"),
aoqi@0 123 INTERFACE("interface");
aoqi@0 124
aoqi@0 125 String classKind;
aoqi@0 126
aoqi@0 127 private ClassKind(String classKind) {
aoqi@0 128 this.classKind = classKind;
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 boolean isConstructorOk(ConstructorKind ck) {
aoqi@0 132 return this != INTERFACE ||
aoqi@0 133 ck == ConstructorKind.DEFAULT;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 String getConstructor(String className, ConstructorKind ck) {
aoqi@0 137 return this == INTERFACE ?
aoqi@0 138 "" :
aoqi@0 139 (className + "(" + ck.constrParam + ") {}");
aoqi@0 140 }
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 // Create a single file manager and JavaCompiler tool
aoqi@0 144 // and reuse them for each compile to save time.
aoqi@0 145 static final StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
aoqi@0 146 static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
aoqi@0 147
aoqi@0 148 public static void main(String... args) throws Exception {
aoqi@0 149 for (ClassKind classKind1 : ClassKind.values()) {
aoqi@0 150 for (ConstructorKind constrKind1 : ConstructorKind.values()) {
aoqi@0 151 if (!classKind1.isConstructorOk(constrKind1)) continue;
aoqi@0 152 for (ClassKind classKind2 : ClassKind.values()) {
aoqi@0 153 for (ConstructorKind constrKind2 : ConstructorKind.values()) {
aoqi@0 154 if (!classKind2.isConstructorOk(constrKind2)) continue;
aoqi@0 155 for (ClassKind classKind3 : ClassKind.values()) {
aoqi@0 156 for (ConstructorKind constrKind3 : ConstructorKind.values()) {
aoqi@0 157 if (!classKind3.isConstructorOk(constrKind3)) continue;
aoqi@0 158 new T7043922(new ClassKind[] { classKind1, classKind2, classKind3 },
aoqi@0 159 new ConstructorKind[] { constrKind1, constrKind2, constrKind3 }).compileAndCheck();
aoqi@0 160 }
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 static class ErrorChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 169
aoqi@0 170 boolean errorFound;
aoqi@0 171 List<String> errDiags = List.nil();
aoqi@0 172
aoqi@0 173 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 174 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 175 errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault()));
aoqi@0 176 errorFound = true;
aoqi@0 177 }
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 String printDiags() {
aoqi@0 181 StringBuilder buf = new StringBuilder();
aoqi@0 182 for (String s : errDiags) {
aoqi@0 183 buf.append(s);
aoqi@0 184 buf.append("\n");
aoqi@0 185 }
aoqi@0 186 return buf.toString();
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189 }

mercurial