1009N/A/*
1009N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
1009N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1009N/A *
1009N/A * This code is free software; you can redistribute it and/or modify it
1009N/A * under the terms of the GNU General Public License version 2 only, as
1009N/A * published by the Free Software Foundation.
1009N/A *
1009N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1009N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1009N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1009N/A * version 2 for more details (a copy is included in the LICENSE file that
1009N/A * accompanied this code).
1009N/A *
1009N/A * You should have received a copy of the GNU General Public License version
1009N/A * 2 along with this work; if not, write to the Free Software Foundation,
1009N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1009N/A *
1009N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1009N/A * or visit www.oracle.com if you need additional information or have any
1009N/A * questions.
1009N/A */
1009N/A
1009N/A/*
1009N/A * @test
1009N/A * @bug 7043922
1009N/A * @summary Regression: internal compiler error for nested anonymous inner class featuring varargs constructor
1009N/A */
1009N/A
1009N/Aimport com.sun.source.util.JavacTask;
1009N/Aimport com.sun.tools.javac.api.JavacTool;
1009N/Aimport com.sun.tools.javac.util.List;
1009N/A
1009N/Aimport java.net.URI;
1009N/Aimport java.util.Arrays;
1009N/Aimport java.util.Locale;
1009N/Aimport javax.tools.Diagnostic;
1009N/Aimport javax.tools.JavaCompiler;
1009N/Aimport javax.tools.JavaFileObject;
1009N/Aimport javax.tools.SimpleJavaFileObject;
1009N/Aimport javax.tools.StandardJavaFileManager;
1009N/Aimport javax.tools.ToolProvider;
1009N/A
1009N/Apublic class T7043922 {
1009N/A
1009N/A ClassKind[] classKinds;
1009N/A ConstructorKind[] constructorKinds;
1009N/A
1009N/A T7043922(ClassKind[] classKinds, ConstructorKind[] constructorKinds) {
1009N/A this.classKinds = classKinds;
1009N/A this.constructorKinds = constructorKinds;
1009N/A }
1009N/A
1009N/A void compileAndCheck() throws Exception {
1009N/A JavaSource source = new JavaSource();
1009N/A ErrorChecker ec = new ErrorChecker();
1009N/A JavacTask ct = (JavacTask)tool.getTask(null, fm, ec,
1009N/A null, null, Arrays.asList(source));
1009N/A ct.analyze();
1009N/A if (ec.errorFound) {
1009N/A throw new Error("invalid diagnostics for source:\n" +
1009N/A source.getCharContent(true) +
1009N/A "\nCompiler diagnostics:\n" + ec.printDiags());
1009N/A }
1009N/A }
1009N/A
1009N/A class JavaSource extends SimpleJavaFileObject {
1009N/A
1009N/A static final String source_template = "#C0 A0 { #K0 }\n" +
1009N/A "#C1 A1 { #K1 }\n" +
1009N/A "#C2 A2 { #K2 }\n" +
1009N/A "class D {\n" +
1009N/A " void test() {\n" +
1009N/A " new A0(#V0) {\n" +
1009N/A " void test() {\n" +
1009N/A " new A1(#V1) {\n" +
1009N/A " void test() {\n" +
1009N/A " new A2(#V2) {};\n" +
1009N/A " }\n" +
1009N/A " };\n" +
1009N/A " }\n" +
1009N/A " };\n" +
1009N/A " }\n" +
1009N/A "}\n";
1009N/A
1009N/A String source;
1009N/A
1009N/A public JavaSource() {
1009N/A super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
1009N/A source = source_template;
1009N/A for (int i = 0; i < 3; i++) {
1009N/A source = source.replaceAll("#C" + i, classKinds[i].classKind).
1009N/A replaceAll("#K" + i, classKinds[i].getConstructor("A" + i, constructorKinds[i])).
1009N/A replaceAll("#V" + i, constructorKinds[i].constrArgs);
1009N/A }
1009N/A }
1009N/A
1009N/A @Override
1009N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
1009N/A return source;
1009N/A }
1009N/A }
1009N/A
1009N/A /** global decls ***/
1009N/A
1009N/A enum ConstructorKind {
1009N/A DEFAULT("", ""),
1009N/A FIXED_ARITY("String s", "\"\""),
1009N/A VARIABLE_ARITY("String... ss", "\"\",\"\"");
1009N/A
1009N/A String constrParam;
1009N/A String constrArgs;
1009N/A
1009N/A private ConstructorKind(String constrParam, String constrArgs) {
1009N/A this.constrParam = constrParam;
1009N/A this.constrArgs = constrArgs;
1009N/A }
1009N/A }
1009N/A
1009N/A enum ClassKind {
1009N/A ABSTRACT("abstract class"),
1009N/A CLASS("class"),
1009N/A INTERFACE("interface");
1009N/A
1009N/A String classKind;
1009N/A
1009N/A private ClassKind(String classKind) {
1009N/A this.classKind = classKind;
1009N/A }
1009N/A
1009N/A boolean isConstructorOk(ConstructorKind ck) {
1009N/A return this != INTERFACE ||
1009N/A ck == ConstructorKind.DEFAULT;
1009N/A }
1009N/A
1009N/A String getConstructor(String className, ConstructorKind ck) {
1009N/A return this == INTERFACE ?
1009N/A "" :
1009N/A (className + "(" + ck.constrParam + ") {}");
1009N/A }
1009N/A }
1009N/A
1009N/A // Create a single file manager and JavaCompiler tool
1009N/A // and reuse them for each compile to save time.
1009N/A static final StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
1009N/A static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
1009N/A
1009N/A public static void main(String... args) throws Exception {
1009N/A for (ClassKind classKind1 : ClassKind.values()) {
1009N/A for (ConstructorKind constrKind1 : ConstructorKind.values()) {
1009N/A if (!classKind1.isConstructorOk(constrKind1)) continue;
1009N/A for (ClassKind classKind2 : ClassKind.values()) {
1009N/A for (ConstructorKind constrKind2 : ConstructorKind.values()) {
1009N/A if (!classKind2.isConstructorOk(constrKind2)) continue;
1009N/A for (ClassKind classKind3 : ClassKind.values()) {
1009N/A for (ConstructorKind constrKind3 : ConstructorKind.values()) {
1009N/A if (!classKind3.isConstructorOk(constrKind3)) continue;
1009N/A new T7043922(new ClassKind[] { classKind1, classKind2, classKind3 },
1009N/A new ConstructorKind[] { constrKind1, constrKind2, constrKind3 }).compileAndCheck();
1009N/A }
1009N/A }
1009N/A }
1009N/A }
1009N/A }
1009N/A }
1009N/A }
1009N/A
1009N/A static class ErrorChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
1009N/A
1009N/A boolean errorFound;
1009N/A List<String> errDiags = List.nil();
1009N/A
1009N/A public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
1009N/A if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
1009N/A errDiags = errDiags.append(diagnostic.getMessage(Locale.getDefault()));
1009N/A errorFound = true;
1009N/A }
1009N/A }
1009N/A
1009N/A String printDiags() {
1009N/A StringBuilder buf = new StringBuilder();
1009N/A for (String s : errDiags) {
1009N/A buf.append(s);
1009N/A buf.append("\n");
1009N/A }
1009N/A return buf.toString();
1009N/A }
1009N/A }
1009N/A}