test/tools/javac/T8180660/MissingLNTEntryForFinalizerTest.java

Sat, 04 Aug 2018 12:55:17 +0100

author
aefimov
date
Sat, 04 Aug 2018 12:55:17 +0100
changeset 3736
7b508cdd33ae
parent 3371
7220be8747f0
permissions
-rw-r--r--

Merge

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

mercurial