Wed, 07 Jun 2017 00:04:12 -0700
8180660: missing LNT entry for finally block
Reviewed-by: mcimadamore, vromero
Contributed-by: maurizio.cimadamore@oracle.com, vicente.romero@oracle.com
shshahma@3371 | 1 | /* |
shshahma@3371 | 2 | * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
shshahma@3371 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
shshahma@3371 | 4 | * |
shshahma@3371 | 5 | * This code is free software; you can redistribute it and/or modify it |
shshahma@3371 | 6 | * under the terms of the GNU General Public License version 2 only, as |
shshahma@3371 | 7 | * published by the Free Software Foundation. |
shshahma@3371 | 8 | * |
shshahma@3371 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
shshahma@3371 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
shshahma@3371 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
shshahma@3371 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
shshahma@3371 | 13 | * accompanied this code). |
shshahma@3371 | 14 | * |
shshahma@3371 | 15 | * You should have received a copy of the GNU General Public License version |
shshahma@3371 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
shshahma@3371 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
shshahma@3371 | 18 | * |
shshahma@3371 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
shshahma@3371 | 20 | * or visit www.oracle.com if you need additional information or have any |
shshahma@3371 | 21 | * questions. |
shshahma@3371 | 22 | */ |
shshahma@3371 | 23 | |
shshahma@3371 | 24 | /* |
shshahma@3371 | 25 | * @test |
shshahma@3371 | 26 | * @bug 8180141 |
shshahma@3371 | 27 | * @summary Missing entry in LineNumberTable for break statement that jumps out of try-finally |
shshahma@3371 | 28 | * @compile -g MissingLNTEntryForFinalizerTest.java |
shshahma@3371 | 29 | * @run main MissingLNTEntryForFinalizerTest |
shshahma@3371 | 30 | */ |
shshahma@3371 | 31 | |
shshahma@3371 | 32 | import java.io.File; |
shshahma@3371 | 33 | import java.net.URI; |
shshahma@3371 | 34 | |
shshahma@3371 | 35 | import javax.tools.JavaFileObject; |
shshahma@3371 | 36 | import javax.tools.SimpleJavaFileObject; |
shshahma@3371 | 37 | |
shshahma@3371 | 38 | import com.sun.tools.classfile.*; |
shshahma@3371 | 39 | import com.sun.tools.javac.comp.Attr; |
shshahma@3371 | 40 | import com.sun.tools.javac.comp.AttrContext; |
shshahma@3371 | 41 | import com.sun.tools.javac.comp.Env; |
shshahma@3371 | 42 | import com.sun.tools.javac.file.JavacFileManager; |
shshahma@3371 | 43 | import com.sun.tools.javac.main.JavaCompiler; |
shshahma@3371 | 44 | import com.sun.tools.javac.tree.JCTree; |
shshahma@3371 | 45 | import com.sun.tools.javac.tree.JCTree.*; |
shshahma@3371 | 46 | import com.sun.tools.javac.util.Context; |
shshahma@3371 | 47 | import com.sun.tools.javac.util.List; |
shshahma@3371 | 48 | |
shshahma@3371 | 49 | import static com.sun.tools.javac.util.List.of; |
shshahma@3371 | 50 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
shshahma@3371 | 51 | |
shshahma@3371 | 52 | public class MissingLNTEntryForFinalizerTest { |
shshahma@3371 | 53 | protected ReusableJavaCompiler tool; |
shshahma@3371 | 54 | Context context; |
shshahma@3371 | 55 | |
shshahma@3371 | 56 | MissingLNTEntryForFinalizerTest() { |
shshahma@3371 | 57 | context = new Context(); |
shshahma@3371 | 58 | JavacFileManager.preRegister(context); |
shshahma@3371 | 59 | MyAttr.preRegister(context); |
shshahma@3371 | 60 | tool = new ReusableJavaCompiler(context); |
shshahma@3371 | 61 | } |
shshahma@3371 | 62 | |
shshahma@3371 | 63 | public static void main(String... args) throws Throwable { |
shshahma@3371 | 64 | new MissingLNTEntryForFinalizerTest().test(); |
shshahma@3371 | 65 | } |
shshahma@3371 | 66 | |
shshahma@3371 | 67 | void test() throws Throwable { |
shshahma@3371 | 68 | JavaSource source = new JavaSource("1"); |
shshahma@3371 | 69 | tool.clear(); |
shshahma@3371 | 70 | List<JavaFileObject> inputs = of(source); |
shshahma@3371 | 71 | try { |
shshahma@3371 | 72 | tool.compile(inputs); |
shshahma@3371 | 73 | } catch (Throwable ex) { |
shshahma@3371 | 74 | throw new AssertionError(ex); |
shshahma@3371 | 75 | } |
shshahma@3371 | 76 | File testClasses = new File("."); |
shshahma@3371 | 77 | File file = new File(testClasses, "Test1.class"); |
shshahma@3371 | 78 | ClassFile classFile = ClassFile.read(file); |
shshahma@3371 | 79 | for (Method m : classFile.methods) { |
shshahma@3371 | 80 | if (classFile.constant_pool.getUTF8Value(m.name_index).equals("foo")) { |
shshahma@3371 | 81 | Code_attribute code = (Code_attribute)m.attributes.get(Attribute.Code); |
shshahma@3371 | 82 | LineNumberTable_attribute lnt = (LineNumberTable_attribute)code.attributes.get(Attribute.LineNumberTable); |
shshahma@3371 | 83 | checkLNT(lnt, MyAttr.lineNumber); |
shshahma@3371 | 84 | } |
shshahma@3371 | 85 | } |
shshahma@3371 | 86 | } |
shshahma@3371 | 87 | |
shshahma@3371 | 88 | void checkLNT(LineNumberTable_attribute lnt, int lineToCheckFor) { |
shshahma@3371 | 89 | for (LineNumberTable_attribute.Entry e: lnt.line_number_table) { |
shshahma@3371 | 90 | if (e.line_number == lineToCheckFor) { |
shshahma@3371 | 91 | return; |
shshahma@3371 | 92 | } |
shshahma@3371 | 93 | } |
shshahma@3371 | 94 | throw new AssertionError("seek line number not found in the LNT for method foo()"); |
shshahma@3371 | 95 | } |
shshahma@3371 | 96 | |
shshahma@3371 | 97 | class JavaSource extends SimpleJavaFileObject { |
shshahma@3371 | 98 | String id; |
shshahma@3371 | 99 | String template = |
shshahma@3371 | 100 | "import java.util.*;\n" + |
shshahma@3371 | 101 | "class Test#Id {\n" + |
shshahma@3371 | 102 | " void foo() {\n" + |
shshahma@3371 | 103 | " List<String> l = null;\n" + |
shshahma@3371 | 104 | " String first = null;\n" + |
shshahma@3371 | 105 | " try {\n" + |
shshahma@3371 | 106 | " first = l.get(0);\n" + |
shshahma@3371 | 107 | " } finally {\n" + |
shshahma@3371 | 108 | " if (first != null) {\n" + |
shshahma@3371 | 109 | " System.out.println(\"finalizer\");\n" + |
shshahma@3371 | 110 | " }\n" + |
shshahma@3371 | 111 | " }\n" + |
shshahma@3371 | 112 | " }\n" + |
shshahma@3371 | 113 | "}"; |
shshahma@3371 | 114 | |
shshahma@3371 | 115 | JavaSource(String id) { |
shshahma@3371 | 116 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
shshahma@3371 | 117 | this.id = id; |
shshahma@3371 | 118 | } |
shshahma@3371 | 119 | |
shshahma@3371 | 120 | @Override |
shshahma@3371 | 121 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
shshahma@3371 | 122 | return template.replace("#Id", id); |
shshahma@3371 | 123 | } |
shshahma@3371 | 124 | } |
shshahma@3371 | 125 | |
shshahma@3371 | 126 | /* this class has been set up to do not depend on a fixed line number, this Attr subclass will |
shshahma@3371 | 127 | * look for 'break' or 'continue' statements in order to find the actual line number they occupy. |
shshahma@3371 | 128 | * This way the test can find if that line number appears in the LNT generated for a given class. |
shshahma@3371 | 129 | */ |
shshahma@3371 | 130 | static class MyAttr extends Attr { |
shshahma@3371 | 131 | static int lineNumber; |
shshahma@3371 | 132 | |
shshahma@3371 | 133 | static void preRegister(Context context) { |
shshahma@3371 | 134 | context.put(attrKey, (com.sun.tools.javac.util.Context.Factory<Attr>) c -> new MyAttr(c)); |
shshahma@3371 | 135 | } |
shshahma@3371 | 136 | |
shshahma@3371 | 137 | MyAttr(Context context) { |
shshahma@3371 | 138 | super(context); |
shshahma@3371 | 139 | } |
shshahma@3371 | 140 | |
shshahma@3371 | 141 | @Override |
shshahma@3371 | 142 | public com.sun.tools.javac.code.Type attribStat(JCTree tree, Env<AttrContext> env) { |
shshahma@3371 | 143 | com.sun.tools.javac.code.Type result = super.attribStat(tree, env); |
shshahma@3371 | 144 | if (tree.hasTag(TRY)) { |
shshahma@3371 | 145 | JCTry tryTree = (JCTry)tree; |
shshahma@3371 | 146 | lineNumber = env.toplevel.lineMap.getLineNumber(tryTree.finalizer.endpos); |
shshahma@3371 | 147 | } |
shshahma@3371 | 148 | return result; |
shshahma@3371 | 149 | } |
shshahma@3371 | 150 | } |
shshahma@3371 | 151 | |
shshahma@3371 | 152 | static class ReusableJavaCompiler extends JavaCompiler { |
shshahma@3371 | 153 | ReusableJavaCompiler(Context context) { |
shshahma@3371 | 154 | super(context); |
shshahma@3371 | 155 | } |
shshahma@3371 | 156 | |
shshahma@3371 | 157 | protected void checkReusable() { |
shshahma@3371 | 158 | // do nothing |
shshahma@3371 | 159 | } |
shshahma@3371 | 160 | |
shshahma@3371 | 161 | @Override |
shshahma@3371 | 162 | public void close() { |
shshahma@3371 | 163 | //do nothing |
shshahma@3371 | 164 | } |
shshahma@3371 | 165 | |
shshahma@3371 | 166 | void clear() { |
shshahma@3371 | 167 | //do nothing |
shshahma@3371 | 168 | } |
shshahma@3371 | 169 | } |
shshahma@3371 | 170 | } |