2
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27
* @summary Missing entry in LineNumberTable for break statement that jumps out of try-finally
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
43
import javax.tools.JavaFileObject;
44
import javax.tools.SimpleJavaFileObject;
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.*;
59
import static com.sun.tools.javac.util.List.of;
60
import static com.sun.tools.javac.tree.JCTree.Tag.*;
62
public class MissingLNTEntryForFinalizerTest {
63
protected ReusableJavaCompiler tool;
66
MissingLNTEntryForFinalizerTest() {
67
context = new Context();
68
JavacFileManager.preRegister(context);
69
MyAttr.preRegister(context);
70
tool = new ReusableJavaCompiler(context);
73
public static void main(String... args) throws Throwable {
74
new MissingLNTEntryForFinalizerTest().test();
77
void test() throws Throwable {
78
JavaSource source = new JavaSource("1");
80
List<JavaFileObject> inputs = of(source);
83
} catch (Throwable ex) {
84
throw new AssertionError(ex);
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);
98
void checkLNT(LineNumberTableAttribute lnt, int lineToCheckFor) {
99
for (LineNumberInfo e: lnt.lineNumbers()) {
100
if (e.lineNumber() == lineToCheckFor) {
104
throw new AssertionError("seek line number not found in the LNT for method foo()");
107
class JavaSource extends SimpleJavaFileObject {
110
"import java.util.*;\n" +
111
"class Test#Id {\n" +
113
" List<String> l = null;\n" +
114
" String first = null;\n" +
116
" first = l.get(0);\n" +
118
" if (first != null) {\n" +
119
" System.out.println(\"finalizer\");\n" +
125
JavaSource(String id) {
126
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
131
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
132
return template.replace("#Id", id);
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.
140
static class MyAttr extends Attr {
141
static int lineNumber;
143
static void preRegister(Context context) {
144
context.put(attrKey, (com.sun.tools.javac.util.Context.Factory<Attr>) c -> new MyAttr(c));
147
MyAttr(Context context) {
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);
162
static class ReusableJavaCompiler extends JavaCompiler {
163
ReusableJavaCompiler(Context context) {
168
protected void checkReusable() {
173
public void close() {
179
Modules.instance(context).newRound();