test/tools/javac/scope/7046348/EagerInterfaceCompletionTest.java

Thu, 21 Feb 2013 15:26:46 +0000

author
mcimadamore
date
Thu, 21 Feb 2013 15:26:46 +0000
changeset 1599
9f0ec00514b6
parent 1393
d7d932236fee
child 2260
fb870c70e774
permissions
-rw-r--r--

8007461: Regression: bad overload resolution when inner class and outer class have method with same name
Summary: Fix regression in varargs method resolution introduced by bad refactoring
Reviewed-by: jjg

mcimadamore@1015 1 /*
mcimadamore@1015 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1015 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1015 4 *
mcimadamore@1015 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1015 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1015 7 * published by the Free Software Foundation.
mcimadamore@1015 8 *
mcimadamore@1015 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1015 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1015 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1015 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1015 13 * accompanied this code).
mcimadamore@1015 14 *
mcimadamore@1015 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1015 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1015 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1015 18 *
mcimadamore@1015 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1015 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1015 21 * questions.
mcimadamore@1015 22 */
mcimadamore@1015 23
mcimadamore@1015 24 /**
mcimadamore@1015 25 * @test
mcimadamore@1015 26 * @bug 7046348
mcimadamore@1015 27 * @summary Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore@1015 28 */
mcimadamore@1015 29
mcimadamore@1015 30 import java.io.File;
mcimadamore@1015 31 import java.net.URI;
mcimadamore@1015 32 import java.util.Arrays;
mcimadamore@1393 33 import java.util.List;
mcimadamore@1015 34
mcimadamore@1015 35 import javax.tools.Diagnostic;
mcimadamore@1015 36 import javax.tools.DiagnosticListener;
mcimadamore@1015 37 import javax.tools.JavaCompiler;
mcimadamore@1015 38 import javax.tools.JavaCompiler.CompilationTask;
mcimadamore@1015 39 import javax.tools.JavaFileObject;
mcimadamore@1015 40 import javax.tools.SimpleJavaFileObject;
mcimadamore@1015 41 import javax.tools.ToolProvider;
mcimadamore@1015 42
mcimadamore@1015 43 public class EagerInterfaceCompletionTest {
mcimadamore@1015 44
mcimadamore@1015 45 JavaCompiler javacTool;
mcimadamore@1015 46 File testDir;
mcimadamore@1393 47 VersionKind versionKind;
mcimadamore@1015 48 HierarchyKind hierarchyKind;
mcimadamore@1015 49 TestKind testKind;
mcimadamore@1015 50 ActionKind actionKind;
mcimadamore@1015 51
mcimadamore@1393 52 EagerInterfaceCompletionTest(JavaCompiler javacTool, File testDir, VersionKind versionKind,
mcimadamore@1015 53 HierarchyKind hierarchyKind, TestKind testKind, ActionKind actionKind) {
mcimadamore@1015 54 this.javacTool = javacTool;
mcimadamore@1393 55 this.versionKind = versionKind;
mcimadamore@1015 56 this.hierarchyKind = hierarchyKind;
mcimadamore@1015 57 this.testDir = testDir;
mcimadamore@1015 58 this.testKind = testKind;
mcimadamore@1015 59 this.actionKind = actionKind;
mcimadamore@1015 60 }
mcimadamore@1015 61
mcimadamore@1015 62 void test() {
mcimadamore@1015 63 testDir.mkdirs();
mcimadamore@1015 64 compile(null, hierarchyKind.source);
mcimadamore@1015 65 actionKind.doAction(this);
mcimadamore@1015 66 DiagnosticChecker dc = new DiagnosticChecker();
mcimadamore@1015 67 compile(dc, testKind.source);
mcimadamore@1393 68 if (testKind.completionFailure(versionKind, actionKind, hierarchyKind) != dc.errorFound) {
mcimadamore@1015 69 if (dc.errorFound) {
mcimadamore@1015 70 error("Unexpected completion failure" +
mcimadamore@1015 71 "\nhierarhcyKind " + hierarchyKind +
mcimadamore@1015 72 "\ntestKind " + testKind +
mcimadamore@1015 73 "\nactionKind " + actionKind);
mcimadamore@1015 74 } else {
mcimadamore@1015 75 error("Missing completion failure " +
mcimadamore@1015 76 "\nhierarhcyKind " + hierarchyKind +
mcimadamore@1015 77 "\ntestKind " + testKind +
mcimadamore@1015 78 "\nactionKind " + actionKind);
mcimadamore@1015 79 }
mcimadamore@1015 80 }
mcimadamore@1015 81 }
mcimadamore@1015 82
mcimadamore@1015 83 void compile(DiagnosticChecker dc, JavaSource... sources) {
mcimadamore@1015 84 try {
mcimadamore@1015 85 CompilationTask ct = javacTool.getTask(null, null, dc,
mcimadamore@1393 86 Arrays.asList("-d", testDir.getAbsolutePath(), "-cp",
mcimadamore@1393 87 testDir.getAbsolutePath(), versionKind.optsArr[0], versionKind.optsArr[1]),
mcimadamore@1015 88 null, Arrays.asList(sources));
mcimadamore@1015 89 ct.call();
mcimadamore@1015 90 }
mcimadamore@1015 91 catch (Exception e) {
mcimadamore@1015 92 e.printStackTrace();
mcimadamore@1015 93 error("Internal compilation error");
mcimadamore@1015 94 }
mcimadamore@1015 95 }
mcimadamore@1015 96
mcimadamore@1015 97 void removeClass(String classToRemoveStr) {
mcimadamore@1015 98 File classToRemove = new File(testDir, classToRemoveStr);
mcimadamore@1015 99 if (!classToRemove.exists()) {
mcimadamore@1015 100 error("Expected file " + classToRemove + " does not exists in folder " + testDir);
mcimadamore@1015 101 }
mcimadamore@1015 102 classToRemove.delete();
mcimadamore@1015 103 };
mcimadamore@1015 104
mcimadamore@1015 105 void error(String msg) {
mcimadamore@1015 106 System.err.println(msg);
mcimadamore@1015 107 nerrors++;
mcimadamore@1015 108 }
mcimadamore@1015 109
mcimadamore@1015 110 class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {
mcimadamore@1015 111
mcimadamore@1015 112 boolean errorFound = false;
mcimadamore@1015 113
mcimadamore@1015 114 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1393 115 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1393 116 errorFound = true;
mcimadamore@1393 117 }
mcimadamore@1015 118 }
mcimadamore@1015 119 }
mcimadamore@1015 120
mcimadamore@1015 121 //global declarations
mcimadamore@1015 122
mcimadamore@1393 123 enum VersionKind {
mcimadamore@1393 124 PRE_LAMBDA("-source", "7"),
mcimadamore@1393 125 LAMBDA("-source", "8");
mcimadamore@1393 126
mcimadamore@1393 127 String[] optsArr;
mcimadamore@1393 128
mcimadamore@1393 129 VersionKind(String... optsArr) {
mcimadamore@1393 130 this.optsArr = optsArr;
mcimadamore@1393 131 }
mcimadamore@1393 132 }
mcimadamore@1393 133
mcimadamore@1015 134 enum HierarchyKind {
mcimadamore@1015 135 INTERFACE("interface A { boolean f = false; void m(); }\n" +
mcimadamore@1015 136 "class B implements A { public void m() {} }"),
mcimadamore@1015 137 CLASS("class A { boolean f; void m() {} }\n" +
mcimadamore@1015 138 "class B extends A { void m() {} }"),
mcimadamore@1015 139 ABSTRACT_CLASS("abstract class A { boolean f; abstract void m(); }\n" +
mcimadamore@1015 140 "class B extends A { void m() {} }");
mcimadamore@1015 141
mcimadamore@1015 142 JavaSource source;
mcimadamore@1015 143
mcimadamore@1015 144 private HierarchyKind(String code) {
mcimadamore@1015 145 this.source = new JavaSource("Test1.java", code);
mcimadamore@1015 146 }
mcimadamore@1015 147 }
mcimadamore@1015 148
mcimadamore@1015 149 enum ActionKind {
mcimadamore@1015 150 REMOVE_A("A.class"),
mcimadamore@1015 151 REMOVE_B("B.class");
mcimadamore@1015 152
mcimadamore@1015 153 String classFile;
mcimadamore@1015 154
mcimadamore@1015 155 private ActionKind(String classFile) {
mcimadamore@1015 156 this.classFile = classFile;
mcimadamore@1015 157 }
mcimadamore@1015 158
mcimadamore@1015 159 void doAction(EagerInterfaceCompletionTest test) {
mcimadamore@1015 160 test.removeClass(classFile);
mcimadamore@1015 161 };
mcimadamore@1015 162 }
mcimadamore@1015 163
mcimadamore@1015 164 enum TestKind {
mcimadamore@1015 165 ACCESS_ONLY("class C { B b; }"),
mcimadamore@1015 166 SUPER("class C extends B {}"),
mcimadamore@1015 167 METHOD("class C { void test(B b) { b.m(); } }"),
mcimadamore@1015 168 FIELD("class C { void test(B b) { boolean b2 = b.f; } }"),
mcimadamore@1015 169 CONSTR("class C { void test() { new B(); } }");
mcimadamore@1015 170
mcimadamore@1015 171 JavaSource source;
mcimadamore@1015 172
mcimadamore@1015 173 private TestKind(final String code) {
mcimadamore@1015 174 this.source = new JavaSource("Test2.java", code);
mcimadamore@1015 175 }
mcimadamore@1015 176
mcimadamore@1393 177 boolean completionFailure(VersionKind vk, ActionKind ak, HierarchyKind hk) {
mcimadamore@1015 178 switch (this) {
mcimadamore@1015 179 case ACCESS_ONLY:
mcimadamore@1015 180 case CONSTR: return ak == ActionKind.REMOVE_B;
mcimadamore@1015 181 case FIELD:
mcimadamore@1015 182 case SUPER: return true;
mcimadamore@1393 183 case METHOD: return hk != HierarchyKind.INTERFACE || ak == ActionKind.REMOVE_B ||
mcimadamore@1393 184 (hk == HierarchyKind.INTERFACE && ak == ActionKind.REMOVE_A && vk == VersionKind.LAMBDA);
mcimadamore@1015 185 default: throw new AssertionError("Unexpected test kind " + this);
mcimadamore@1015 186 }
mcimadamore@1015 187 }
mcimadamore@1015 188 }
mcimadamore@1015 189
mcimadamore@1015 190 public static void main(String[] args) throws Exception {
mcimadamore@1015 191 String SCRATCH_DIR = System.getProperty("user.dir");
mcimadamore@1015 192 JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
mcimadamore@1015 193 int n = 0;
mcimadamore@1393 194 for (VersionKind versionKind : VersionKind.values()) {
mcimadamore@1393 195 for (HierarchyKind hierarchyKind : HierarchyKind.values()) {
mcimadamore@1393 196 for (TestKind testKind : TestKind.values()) {
mcimadamore@1393 197 for (ActionKind actionKind : ActionKind.values()) {
mcimadamore@1393 198 File testDir = new File(SCRATCH_DIR, "test"+n);
mcimadamore@1393 199 new EagerInterfaceCompletionTest(javacTool, testDir, versionKind,
mcimadamore@1393 200 hierarchyKind, testKind, actionKind).test();
mcimadamore@1393 201 n++;
mcimadamore@1393 202 }
mcimadamore@1015 203 }
mcimadamore@1015 204 }
mcimadamore@1015 205 }
mcimadamore@1015 206 if (nerrors > 0) {
mcimadamore@1015 207 throw new AssertionError("Some errors have been detected");
mcimadamore@1015 208 }
mcimadamore@1015 209 }
mcimadamore@1015 210
mcimadamore@1015 211 static class JavaSource extends SimpleJavaFileObject {
mcimadamore@1015 212
mcimadamore@1015 213 String source;
mcimadamore@1015 214
mcimadamore@1015 215 public JavaSource(String filename, String source) {
mcimadamore@1015 216 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
mcimadamore@1015 217 this.source = source;
mcimadamore@1015 218 }
mcimadamore@1015 219
mcimadamore@1015 220 @Override
mcimadamore@1015 221 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1015 222 return source;
mcimadamore@1015 223 }
mcimadamore@1015 224 }
mcimadamore@1015 225
mcimadamore@1015 226 static int nerrors = 0;
mcimadamore@1015 227 }

mercurial