1 /*
2  * Copyright (c) 2017, 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  */
23 
24 /*
25  * @test
26  * @bug 8176714
27  * @summary javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
28  * @library /tools/javac/lib
29  * @modules jdk.compiler/com.sun.source.util
30  *          jdk.compiler/com.sun.tools.javac.api
31  *          jdk.compiler/com.sun.tools.javac.code
32  *          jdk.compiler/com.sun.tools.javac.file
33  *          jdk.compiler/com.sun.tools.javac.tree
34  *          jdk.compiler/com.sun.tools.javac.util
35  * @build DPrinter
36  * @run main FieldOverloadKindNotAssignedTest
37  */
38 
39 import java.net.URI;
40 import java.util.Arrays;
41 
42 import javax.tools.JavaCompiler;
43 import javax.tools.JavaFileObject;
44 import javax.tools.SimpleJavaFileObject;
45 import javax.tools.ToolProvider;
46 
47 import com.sun.source.tree.CompilationUnitTree;
48 import com.sun.source.util.JavacTask;
49 import com.sun.tools.javac.file.JavacFileManager;
50 import com.sun.tools.javac.tree.JCTree;
51 import com.sun.tools.javac.tree.JCTree.JCMemberReference;
52 import com.sun.tools.javac.tree.TreeScanner;
53 import com.sun.tools.javac.util.Assert;
54 import com.sun.tools.javac.util.Context;
55 
56 public class FieldOverloadKindNotAssignedTest {
main(String... args)57     public static void main(String... args) throws Exception {
58         new FieldOverloadKindNotAssignedTest().run();
59     }
60 
run()61     void run() throws Exception {
62         Context context = new Context();
63         JavacFileManager.preRegister(context);
64         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
65         JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
66         Iterable<? extends CompilationUnitTree> elements = ct.parse();
67         ct.analyze();
68         Assert.check(elements.iterator().hasNext());
69         JCTree topLevel = (JCTree)elements.iterator().next();
70         new TreeScanner() {
71             @Override
72             public void visitReference(JCMemberReference tree) {
73                 Assert.check(tree.getOverloadKind() != null);
74             }
75         }.scan(topLevel);
76     }
77 
78     static class JavaSource extends SimpleJavaFileObject {
79 
80         String source =
81                 "import java.util.function.*;\n" +
82 
83                 "class Test {\n" +
84                 "    void m(Predicate<String> psi) {}\n" +
85                 "    void m(Function<String, String> fss) {}\n" +
86 
87                 "    void foo(boolean b) {\n" +
88                 "        m(b ? s -> false : Test::g);\n" +
89                 "    }\n" +
90 
91                 "    static boolean g(String s) { return false; }\n" +
92                 "    static boolean g(Integer i) { return false; }\n" +
93                 "}";
94 
JavaSource()95         public JavaSource() {
96             super(URI.create("myfo:/Foo.java"), JavaFileObject.Kind.SOURCE);
97         }
98 
99         @Override
getCharContent(boolean ignoreEncodingErrors)100         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
101             return source;
102         }
103     }
104 }
105