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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2260
fb870c70e774
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial