jdk

Форк
0
/
MissingLNTEntryForFinalizerTest.java 
182 строки · 6.3 Кб
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
 * @enablePreview
29
 * @modules java.base/jdk.internal.classfile.impl
30
 *          jdk.compiler/com.sun.tools.javac.code
31
 *          jdk.compiler/com.sun.tools.javac.comp
32
 *          jdk.compiler/com.sun.tools.javac.file
33
 *          jdk.compiler/com.sun.tools.javac.main
34
 *          jdk.compiler/com.sun.tools.javac.tree
35
 *          jdk.compiler/com.sun.tools.javac.util
36
 * @compile -g MissingLNTEntryForFinalizerTest.java
37
 * @run main MissingLNTEntryForFinalizerTest
38
 */
39

40
import java.io.File;
41
import java.net.URI;
42

43
import javax.tools.JavaFileObject;
44
import javax.tools.SimpleJavaFileObject;
45

46
import com.sun.tools.javac.comp.Attr;
47
import com.sun.tools.javac.comp.AttrContext;
48
import com.sun.tools.javac.comp.Env;
49
import com.sun.tools.javac.comp.Modules;
50
import com.sun.tools.javac.file.JavacFileManager;
51
import com.sun.tools.javac.main.JavaCompiler;
52
import com.sun.tools.javac.tree.JCTree;
53
import com.sun.tools.javac.tree.JCTree.*;
54
import com.sun.tools.javac.util.Context;
55
import com.sun.tools.javac.util.List;
56
import java.lang.classfile.*;
57
import java.lang.classfile.attribute.*;
58

59
import static com.sun.tools.javac.util.List.of;
60
import static com.sun.tools.javac.tree.JCTree.Tag.*;
61

62
public class MissingLNTEntryForFinalizerTest {
63
    protected ReusableJavaCompiler tool;
64
    Context context;
65

66
    MissingLNTEntryForFinalizerTest() {
67
        context = new Context();
68
        JavacFileManager.preRegister(context);
69
        MyAttr.preRegister(context);
70
        tool = new ReusableJavaCompiler(context);
71
    }
72

73
    public static void main(String... args) throws Throwable {
74
        new MissingLNTEntryForFinalizerTest().test();
75
    }
76

77
    void test() throws Throwable {
78
        JavaSource source = new JavaSource("1");
79
        tool.clear();
80
        List<JavaFileObject> inputs = of(source);
81
        try {
82
            tool.compile(inputs);
83
        } catch (Throwable ex) {
84
            throw new AssertionError(ex);
85
        }
86
        File testClasses = new File(".");
87
        File file = new File(testClasses, "Test1.class");
88
        ClassModel classFile = ClassFile.of().parse(file.toPath());
89
        for (MethodModel m : classFile.methods()) {
90
            if (m.methodName().equalsString("foo")) {
91
                CodeAttribute code = m.findAttribute(Attributes.code()).orElseThrow();
92
                LineNumberTableAttribute lnt = code.findAttribute(Attributes.lineNumberTable()).orElseThrow();
93
                checkLNT(lnt, MyAttr.lineNumber);
94
            }
95
        }
96
    }
97

98
    void checkLNT(LineNumberTableAttribute lnt, int lineToCheckFor) {
99
        for (LineNumberInfo e: lnt.lineNumbers()) {
100
            if (e.lineNumber() == lineToCheckFor) {
101
                return;
102
            }
103
        }
104
        throw new AssertionError("seek line number not found in the LNT for method foo()");
105
    }
106

107
    class JavaSource extends SimpleJavaFileObject {
108
        String id;
109
        String template =
110
                "import java.util.*;\n" +
111
                "class Test#Id {\n" +
112
                "    void foo() {\n" +
113
                "        List<String> l = null;\n" +
114
                "        String first = null;\n" +
115
                "        try {\n" +
116
                "            first = l.get(0);\n" +
117
                "        } finally {\n" +
118
                "            if (first != null) {\n" +
119
                "                System.out.println(\"finalizer\");\n" +
120
                "            }\n" +
121
                "        }\n" +
122
                "    }\n" +
123
                "}";
124

125
        JavaSource(String id) {
126
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
127
            this.id = id;
128
        }
129

130
        @Override
131
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
132
            return template.replace("#Id", id);
133
        }
134
    }
135

136
    /* this class has been set up to do not depend on a fixed line number, this Attr subclass will
137
     * look for 'break' or 'continue' statements in order to find the actual line number they occupy.
138
     * This way the test can find if that line number appears in the LNT generated for a given class.
139
     */
140
    static class MyAttr extends Attr {
141
        static int lineNumber;
142

143
        static void preRegister(Context context) {
144
            context.put(attrKey, (com.sun.tools.javac.util.Context.Factory<Attr>) c -> new MyAttr(c));
145
        }
146

147
        MyAttr(Context context) {
148
            super(context);
149
        }
150

151
        @Override
152
        public com.sun.tools.javac.code.Type attribStat(JCTree tree, Env<AttrContext> env) {
153
            com.sun.tools.javac.code.Type result = super.attribStat(tree, env);
154
            if (tree.hasTag(TRY)) {
155
                JCTry tryTree = (JCTry)tree;
156
                lineNumber = env.toplevel.lineMap.getLineNumber(tryTree.finalizer.endpos);
157
            }
158
            return result;
159
        }
160
    }
161

162
    static class ReusableJavaCompiler extends JavaCompiler {
163
        ReusableJavaCompiler(Context context) {
164
            super(context);
165
        }
166

167
        @Override
168
        protected void checkReusable() {
169
            // do nothing
170
        }
171

172
        @Override
173
        public void close() {
174
            //do nothing
175
        }
176

177
        void clear() {
178
            newRound();
179
            Modules.instance(context).newRound();
180
        }
181
    }
182
}
183

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.