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 8180141
27 * @summary Missing entry in LineNumberTable for break statement that jumps out of try-finally
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 *          jdk.compiler/com.sun.tools.javac.code
30 *          jdk.compiler/com.sun.tools.javac.comp
31 *          jdk.compiler/com.sun.tools.javac.file
32 *          jdk.compiler/com.sun.tools.javac.main
33 *          jdk.compiler/com.sun.tools.javac.tree
34 *          jdk.compiler/com.sun.tools.javac.util
35 * @compile -g MissingLNTEntryForBreakContinueTest.java
36 * @run main MissingLNTEntryForBreakContinueTest
37 */
38
39import java.io.File;
40import java.net.URI;
41
42import javax.tools.JavaFileObject;
43import javax.tools.SimpleJavaFileObject;
44
45import com.sun.tools.classfile.*;
46import com.sun.tools.javac.comp.Attr;
47import com.sun.tools.javac.comp.AttrContext;
48import com.sun.tools.javac.comp.Env;
49import com.sun.tools.javac.comp.Modules;
50import com.sun.tools.javac.file.JavacFileManager;
51import com.sun.tools.javac.main.JavaCompiler;
52import com.sun.tools.javac.tree.JCTree;
53import com.sun.tools.javac.util.Context;
54import com.sun.tools.javac.util.List;
55
56import static com.sun.tools.javac.util.List.of;
57import static com.sun.tools.javac.tree.JCTree.Tag.*;
58
59public class MissingLNTEntryForBreakContinueTest {
60    protected ReusableJavaCompiler tool;
61    Context context;
62
63    MissingLNTEntryForBreakContinueTest() {
64        context = new Context();
65        JavacFileManager.preRegister(context);
66        MyAttr.preRegister(context);
67        tool = new ReusableJavaCompiler(context);
68    }
69
70    public static void main(String... args) throws Throwable {
71        new MissingLNTEntryForBreakContinueTest().test();
72    }
73
74    void test() throws Throwable {
75        testFor("1", "break");
76        testFor("2", "continue");
77    }
78
79    void testFor(String id, String statement) throws Throwable {
80        JavaSource source = new JavaSource(id, statement);
81        tool.clear();
82        List<JavaFileObject> inputs = of(source);
83        try {
84            tool.compile(inputs);
85        } catch (Throwable ex) {
86            throw new AssertionError(ex);
87        }
88        File testClasses = new File(".");
89        File file = new File(testClasses, "Test" + id + ".class");
90        ClassFile classFile = ClassFile.read(file);
91        for (Method m : classFile.methods) {
92            if (classFile.constant_pool.getUTF8Value(m.name_index).equals("foo")) {
93                Code_attribute code = (Code_attribute)m.attributes.get(Attribute.Code);
94                LineNumberTable_attribute lnt = (LineNumberTable_attribute)code.attributes.get(Attribute.LineNumberTable);
95                checkLNT(lnt, MyAttr.lineNumber);
96            }
97        }
98    }
99
100    void checkLNT(LineNumberTable_attribute lnt, int lineToCheckFor) {
101        for (LineNumberTable_attribute.Entry e: lnt.line_number_table) {
102            if (e.line_number == lineToCheckFor) {
103                return;
104            }
105        }
106        throw new AssertionError("seek line number not found in the LNT for method foo()");
107    }
108
109    class JavaSource extends SimpleJavaFileObject {
110        String statement;
111        String id;
112        String template =
113                "class Test#Id {\n" +
114                "    void foo(boolean condition) {\n" +
115                "        while (true) {\n" +
116                "            try {\n" +
117                "                if (condition) {\n" +
118                "                    #STM;\n" +
119                "                }\n" +
120                "            } finally {\n" +
121                "                System.out.println(\"finalizer\");\n" +
122                "            }\n" +
123                "        }\n" +
124                "    }" +
125                "}";
126
127        JavaSource(String id, String statement) {
128            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
129            this.statement = statement;
130            this.id = id;
131        }
132
133        @Override
134        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
135            return template.replace("#Id", id).replace("#STM", statement);
136        }
137    }
138
139    /* this class has been set up to do not depend on a fixed line number, this Attr subclass will
140     * look for 'break' or 'continue' statements in order to find the actual line number they occupy.
141     * This way the test can find if that line number appears in the LNT generated for a given class.
142     */
143    static class MyAttr extends Attr {
144        static int lineNumber;
145
146        static void preRegister(Context context) {
147            context.put(attrKey, (com.sun.tools.javac.util.Context.Factory<Attr>) c -> new MyAttr(c));
148        }
149
150        MyAttr(Context context) {
151            super(context);
152        }
153
154        @Override
155        public com.sun.tools.javac.code.Type attribStat(JCTree tree, Env<AttrContext> env) {
156            com.sun.tools.javac.code.Type result = super.attribStat(tree, env);
157            if (tree.hasTag(BREAK) || tree.hasTag(CONTINUE)) {
158                lineNumber = env.toplevel.lineMap.getLineNumber(tree.pos);
159            }
160            return result;
161        }
162    }
163
164    static class ReusableJavaCompiler extends JavaCompiler {
165        ReusableJavaCompiler(Context context) {
166            super(context);
167        }
168
169        @Override
170        protected void checkReusable() {
171            // do nothing
172        }
173
174        @Override
175        public void close() {
176            //do nothing
177        }
178
179        void clear() {
180            newRound();
181            Modules.instance(context).newRound();
182        }
183    }
184}
185