1013N/A/*
1013N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
1013N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1013N/A *
1013N/A * This code is free software; you can redistribute it and/or modify it
1013N/A * under the terms of the GNU General Public License version 2 only, as
1013N/A * published by the Free Software Foundation.
1013N/A *
1013N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1013N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1013N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1013N/A * version 2 for more details (a copy is included in the LICENSE file that
1013N/A * accompanied this code).
1013N/A *
1013N/A * You should have received a copy of the GNU General Public License version
1013N/A * 2 along with this work; if not, write to the Free Software Foundation,
1013N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1013N/A *
1013N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1013N/A * or visit www.oracle.com if you need additional information or have any
1013N/A * questions.
1013N/A */
1013N/A
1013N/A/**
1013N/A * @test
1013N/A * @bug 7046348
1013N/A * @summary Regression: javac complains of missing classfile for a seemingly unrelated interface
1013N/A */
1013N/A
1013N/Aimport java.io.File;
1013N/Aimport java.net.URI;
1013N/Aimport java.util.Arrays;
1013N/A
1013N/Aimport javax.tools.Diagnostic;
1013N/Aimport javax.tools.DiagnosticListener;
1013N/Aimport javax.tools.JavaCompiler;
1013N/Aimport javax.tools.JavaCompiler.CompilationTask;
1013N/Aimport javax.tools.JavaFileObject;
1013N/Aimport javax.tools.SimpleJavaFileObject;
1013N/Aimport javax.tools.ToolProvider;
1013N/A
1013N/Apublic class EagerInterfaceCompletionTest {
1013N/A
1013N/A JavaCompiler javacTool;
1013N/A File testDir;
1013N/A HierarchyKind hierarchyKind;
1013N/A TestKind testKind;
1013N/A ActionKind actionKind;
1013N/A
1013N/A EagerInterfaceCompletionTest(JavaCompiler javacTool, File testDir,
1013N/A HierarchyKind hierarchyKind, TestKind testKind, ActionKind actionKind) {
1013N/A this.javacTool = javacTool;
1013N/A this.hierarchyKind = hierarchyKind;
1013N/A this.testDir = testDir;
1013N/A this.testKind = testKind;
1013N/A this.actionKind = actionKind;
1013N/A }
1013N/A
1013N/A void test() {
1013N/A testDir.mkdirs();
1013N/A compile(null, hierarchyKind.source);
1013N/A actionKind.doAction(this);
1013N/A DiagnosticChecker dc = new DiagnosticChecker();
1013N/A compile(dc, testKind.source);
1013N/A if (testKind.completionFailure(actionKind, hierarchyKind) != dc.errorFound) {
1013N/A if (dc.errorFound) {
1013N/A error("Unexpected completion failure" +
1013N/A "\nhierarhcyKind " + hierarchyKind +
1013N/A "\ntestKind " + testKind +
1013N/A "\nactionKind " + actionKind);
1013N/A } else {
1013N/A error("Missing completion failure " +
1013N/A "\nhierarhcyKind " + hierarchyKind +
1013N/A "\ntestKind " + testKind +
1013N/A "\nactionKind " + actionKind);
1013N/A }
1013N/A }
1013N/A }
1013N/A
1013N/A void compile(DiagnosticChecker dc, JavaSource... sources) {
1013N/A try {
1013N/A CompilationTask ct = javacTool.getTask(null, null, dc,
1013N/A Arrays.asList("-d", testDir.getAbsolutePath(), "-cp", testDir.getAbsolutePath()),
1013N/A null, Arrays.asList(sources));
1013N/A ct.call();
1013N/A }
1013N/A catch (Exception e) {
1013N/A e.printStackTrace();
1013N/A error("Internal compilation error");
1013N/A }
1013N/A }
1013N/A
1013N/A void removeClass(String classToRemoveStr) {
1013N/A File classToRemove = new File(testDir, classToRemoveStr);
1013N/A if (!classToRemove.exists()) {
1013N/A error("Expected file " + classToRemove + " does not exists in folder " + testDir);
1013N/A }
1013N/A classToRemove.delete();
1013N/A };
1013N/A
1013N/A void error(String msg) {
1013N/A System.err.println(msg);
1013N/A nerrors++;
1013N/A }
1013N/A
1013N/A class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {
1013N/A
1013N/A boolean errorFound = false;
1013N/A
1013N/A public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
1013N/A errorFound = true;
1013N/A }
1013N/A }
1013N/A
1013N/A //global declarations
1013N/A
1013N/A enum HierarchyKind {
1013N/A INTERFACE("interface A { boolean f = false; void m(); }\n" +
1013N/A "class B implements A { public void m() {} }"),
1013N/A CLASS("class A { boolean f; void m() {} }\n" +
1013N/A "class B extends A { void m() {} }"),
1013N/A ABSTRACT_CLASS("abstract class A { boolean f; abstract void m(); }\n" +
1013N/A "class B extends A { void m() {} }");
1013N/A
1013N/A JavaSource source;
1013N/A
1013N/A private HierarchyKind(String code) {
1013N/A this.source = new JavaSource("Test1.java", code);
1013N/A }
1013N/A }
1013N/A
1013N/A enum ActionKind {
1013N/A REMOVE_A("A.class"),
1013N/A REMOVE_B("B.class");
1013N/A
1013N/A String classFile;
1013N/A
1013N/A private ActionKind(String classFile) {
1013N/A this.classFile = classFile;
1013N/A }
1013N/A
1013N/A void doAction(EagerInterfaceCompletionTest test) {
1013N/A test.removeClass(classFile);
1013N/A };
1013N/A }
1013N/A
1013N/A enum TestKind {
1013N/A ACCESS_ONLY("class C { B b; }"),
1013N/A SUPER("class C extends B {}"),
1013N/A METHOD("class C { void test(B b) { b.m(); } }"),
1013N/A FIELD("class C { void test(B b) { boolean b2 = b.f; } }"),
1013N/A CONSTR("class C { void test() { new B(); } }");
1013N/A
1013N/A JavaSource source;
1013N/A
1013N/A private TestKind(final String code) {
1013N/A this.source = new JavaSource("Test2.java", code);
1013N/A }
1013N/A
1013N/A boolean completionFailure(ActionKind ak, HierarchyKind hk) {
1013N/A switch (this) {
1013N/A case ACCESS_ONLY:
1013N/A case CONSTR: return ak == ActionKind.REMOVE_B;
1013N/A case FIELD:
1013N/A case SUPER: return true;
1013N/A case METHOD: return hk != HierarchyKind.INTERFACE || ak == ActionKind.REMOVE_B;
1013N/A default: throw new AssertionError("Unexpected test kind " + this);
1013N/A }
1013N/A }
1013N/A }
1013N/A
1013N/A public static void main(String[] args) throws Exception {
1013N/A String SCRATCH_DIR = System.getProperty("user.dir");
1013N/A JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
1013N/A int n = 0;
1013N/A for (HierarchyKind hierarchyKind : HierarchyKind.values()) {
1013N/A for (TestKind testKind : TestKind.values()) {
1013N/A for (ActionKind actionKind : ActionKind.values()) {
1013N/A File testDir = new File(SCRATCH_DIR, "test"+n);
1013N/A new EagerInterfaceCompletionTest(javacTool, testDir, hierarchyKind, testKind, actionKind).test();
1013N/A n++;
1013N/A }
1013N/A }
1013N/A }
1013N/A if (nerrors > 0) {
1013N/A throw new AssertionError("Some errors have been detected");
1013N/A }
1013N/A }
1013N/A
1013N/A static class JavaSource extends SimpleJavaFileObject {
1013N/A
1013N/A String source;
1013N/A
1013N/A public JavaSource(String filename, String source) {
1013N/A super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
1013N/A this.source = source;
1013N/A }
1013N/A
1013N/A @Override
1013N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
1013N/A return source;
1013N/A }
1013N/A }
1013N/A
1013N/A static int nerrors = 0;
1013N/A}