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 8175235
27 * @summary type inference regression after JDK-8046685
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 InferenceRegressionTest02
37 */
38
39import java.io.*;
40import java.net.URI;
41import java.util.Arrays;
42
43import javax.tools.JavaCompiler;
44import javax.tools.JavaFileObject;
45import javax.tools.SimpleJavaFileObject;
46import javax.tools.ToolProvider;
47
48import com.sun.source.tree.CompilationUnitTree;
49import com.sun.source.util.JavacTask;
50import com.sun.source.util.Trees;
51import com.sun.tools.javac.api.JavacTrees;
52import com.sun.tools.javac.file.JavacFileManager;
53import com.sun.tools.javac.tree.JCTree;
54import com.sun.tools.javac.util.Assert;
55import com.sun.tools.javac.util.Context;
56
57public class InferenceRegressionTest02 {
58    public static void main(String... args) throws Exception {
59        new InferenceRegressionTest02().run();
60    }
61
62    void run() throws Exception {
63        Context context = new Context();
64        JavacFileManager.preRegister(context);
65        Trees trees = JavacTrees.instance(context);
66        StringWriter strOut = new StringWriter();
67        PrintWriter pw = new PrintWriter(strOut);
68        DPrinter dprinter = new DPrinter(pw, trees);
69        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
70        JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
71        Iterable<? extends CompilationUnitTree> elements = ct.parse();
72        ct.analyze();
73        Assert.check(elements.iterator().hasNext());
74        dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next());
75        String output = strOut.toString();
76        Assert.check(!output.contains("java.lang.Object"), "there shouldn't be any type instantiated to Object");
77    }
78
79    static class JavaSource extends SimpleJavaFileObject {
80
81        String source =
82                "import java.util.function.*;\n" +
83                "import java.util.*;\n" +
84                "import java.util.stream.*;\n" +
85
86                "class Foo {\n" +
87                "    void test(List<Map.Entry<Foo, Foo>> ls) {\n" +
88                "        Map<Foo, Set<Foo>> res = ls.stream()\n" +
89                "                .collect(Collectors.groupingBy(Map.Entry::getKey,\n" +
90                "                        HashMap::new,\n" +
91                "                        Collectors.mapping(Map.Entry::getValue, Collectors.toSet())));\n" +
92                "    }\n" +
93                "}";
94
95        public JavaSource() {
96            super(URI.create("myfo:/Foo.java"), JavaFileObject.Kind.SOURCE);
97        }
98
99        @Override
100        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
101            return source;
102        }
103    }
104}
105