InferenceRegressionTest02.java revision 4039:a60be0cc160b
11573Srgrimes/*
21573Srgrimes * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41573Srgrimes *
51573Srgrimes * This code is free software; you can redistribute it and/or modify it
61573Srgrimes * under the terms of the GNU General Public License version 2 only, as
71573Srgrimes * published by the Free Software Foundation.
81573Srgrimes *
91573Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101573Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111573Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121573Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131573Srgrimes * accompanied this code).
141573Srgrimes *
151573Srgrimes * You should have received a copy of the GNU General Public License version
161573Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171573Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181573Srgrimes *
191573Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201573Srgrimes * or visit www.oracle.com if you need additional information or have any
211573Srgrimes * questions.
221573Srgrimes */
231573Srgrimes
241573Srgrimes/*
251573Srgrimes * @test
261573Srgrimes * @bug 8175235
271573Srgrimes * @summary type inference regression after JDK-8046685
281573Srgrimes * @library /tools/javac/lib
291573Srgrimes * @modules jdk.compiler/com.sun.source.util
301573Srgrimes *          jdk.compiler/com.sun.tools.javac.api
311573Srgrimes *          jdk.compiler/com.sun.tools.javac.code
321573Srgrimes *          jdk.compiler/com.sun.tools.javac.file
331573Srgrimes *          jdk.compiler/com.sun.tools.javac.tree
341573Srgrimes *          jdk.compiler/com.sun.tools.javac.util
3523668Speter * @build DPrinter
361573Srgrimes * @run main InferenceRegressionTest02
371573Srgrimes */
381573Srgrimes
3923768Sbdeimport java.io.*;
407978Sbdeimport java.net.URI;
411573Srgrimesimport java.util.Arrays;
421573Srgrimes
437978Sbdeimport javax.tools.JavaCompiler;
441573Srgrimesimport javax.tools.JavaFileObject;
451573Srgrimesimport javax.tools.SimpleJavaFileObject;
461573Srgrimesimport javax.tools.ToolProvider;
471573Srgrimes
481573Srgrimesimport com.sun.source.tree.CompilationUnitTree;
4923668Speterimport com.sun.source.util.JavacTask;
501573Srgrimesimport com.sun.source.util.Trees;
511573Srgrimesimport com.sun.tools.javac.api.JavacTrees;
521573Srgrimesimport com.sun.tools.javac.file.JavacFileManager;
531573Srgrimesimport com.sun.tools.javac.tree.JCTree;
541573Srgrimesimport com.sun.tools.javac.util.Assert;
5523768Sbdeimport com.sun.tools.javac.util.Context;
5623668Speter
5723668Speterpublic class InferenceRegressionTest02 {
581573Srgrimes    public static void main(String... args) throws Exception {
5923668Speter        new InferenceRegressionTest02().run();
6023668Speter    }
6123668Speter
6223668Speter    void run() throws Exception {
6323668Speter        Context context = new Context();
6423668Speter        JavacFileManager.preRegister(context);
6523668Speter        Trees trees = JavacTrees.instance(context);
6623668Speter        StringWriter strOut = new StringWriter();
6723768Sbde        PrintWriter pw = new PrintWriter(strOut);
6823668Speter        DPrinter dprinter = new DPrinter(pw, trees);
6923668Speter        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
7023668Speter        JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, Arrays.asList(new JavaSource()));
7114910Sbde        Iterable<? extends CompilationUnitTree> elements = ct.parse();
7214910Sbde        ct.analyze();
7314910Sbde        Assert.check(elements.iterator().hasNext());
7414910Sbde        dprinter.treeTypes(true).printTree("", (JCTree)elements.iterator().next());
7523668Speter        String output = strOut.toString();
7623768Sbde        Assert.check(!output.contains("java.lang.Object"), "there shouldn't be any type instantiated to Object");
7723668Speter    }
7814910Sbde
7923768Sbde    static class JavaSource extends SimpleJavaFileObject {
8014910Sbde
817978Sbde        String source =
8223668Speter                "import java.util.function.*;\n" +
8323768Sbde                "import java.util.*;\n" +
8423768Sbde                "import java.util.stream.*;\n" +
8523768Sbde
8623768Sbde                "class Foo {\n" +
877978Sbde                "    void test(List<Map.Entry<Foo, Foo>> ls) {\n" +
8823768Sbde                "        Map<Foo, Set<Foo>> res = ls.stream()\n" +
897978Sbde                "                .collect(Collectors.groupingBy(Map.Entry::getKey,\n" +
901573Srgrimes                "                        HashMap::new,\n" +
9123768Sbde                "                        Collectors.mapping(Map.Entry::getValue, Collectors.toSet())));\n" +
9223768Sbde                "    }\n" +
9323668Speter                "}";
941573Srgrimes
9523768Sbde        public JavaSource() {
9623668Speter            super(URI.create("myfo:/Foo.java"), JavaFileObject.Kind.SOURCE);
9723668Speter        }
981573Srgrimes
9923668Speter        @Override
10023668Speter        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
10123668Speter            return source;
10223668Speter        }
10323668Speter    }
10423668Speter}
10523668Speter