AvoidInfiniteReattribution.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2015, 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 8077605
27 * @summary Check that when an exception occurs during Attr.visitLambda, an attempt to attribute
28 *          the lambda again is avoided rather than falling into an infinite recursion.
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.comp
31 *          jdk.compiler/com.sun.tools.javac.tree
32 *          jdk.compiler/com.sun.tools.javac.util
33 */
34
35import java.io.IOException;
36import java.net.URI;
37import java.util.Arrays;
38import java.util.List;
39import javax.tools.JavaFileObject;
40import javax.tools.SimpleJavaFileObject;
41
42import com.sun.tools.javac.api.JavacTaskImpl;
43import com.sun.tools.javac.api.JavacTool;
44import com.sun.tools.javac.comp.Attr;
45import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
46import com.sun.tools.javac.util.Context;
47import com.sun.tools.javac.util.Context.Factory;
48
49public class AvoidInfiniteReattribution {
50
51    public static void main(String... args) throws Exception {
52        new AvoidInfiniteReattribution().run();
53    }
54
55    void run() throws IOException {
56        JavacTool tool = JavacTool.create();
57        JavaSource source = new JavaSource("class Test {" +
58                                           "    I i = STOP -> {};" +
59                                           "    interface I {" +
60                                           "        public void test(int i) {}" +
61                                           "    }" +
62                                           "}");
63        Context context = new Context();
64        CrashingAttr.preRegister(context);
65        List<JavaSource> inputs = Arrays.asList(source);
66        JavacTaskImpl task =
67                (JavacTaskImpl) tool.getTask(null, null, null, null, null, inputs, context);
68        try {
69            task.analyze(null);
70            throw new AssertionError("Expected exception not seen.");
71        } catch (StopException ex) {
72            //ok
73        }
74    }
75
76    static class CrashingAttr extends Attr {
77
78        static void preRegister(Context context) {
79            context.put(attrKey, (Factory<Attr>) c -> new CrashingAttr(c));
80        }
81
82        CrashingAttr(Context context) {
83            super(context);
84        }
85
86        @Override public void visitVarDef(JCVariableDecl tree) {
87            if (tree.name.contentEquals("STOP"))
88                throw new StopException();
89            super.visitVarDef(tree);
90        }
91    }
92
93    static class StopException extends NullPointerException {}
94
95    class JavaSource extends SimpleJavaFileObject {
96
97        String source;
98
99        JavaSource(String source) {
100            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
101            this.source = source;
102        }
103
104        @Override
105        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
106            return source;
107        }
108
109    }
110
111}
112