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