test/tools/javac/T8180660/MissingLNTEntryForFinalizerTest.java

changeset 3371
7220be8747f0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T8180660/MissingLNTEntryForFinalizerTest.java	Wed Jun 07 00:04:12 2017 -0700
     1.3 @@ -0,0 +1,170 @@
     1.4 +/*
     1.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8180141
    1.30 + * @summary Missing entry in LineNumberTable for break statement that jumps out of try-finally
    1.31 + * @compile -g MissingLNTEntryForFinalizerTest.java
    1.32 + * @run main MissingLNTEntryForFinalizerTest
    1.33 + */
    1.34 +
    1.35 +import java.io.File;
    1.36 +import java.net.URI;
    1.37 +
    1.38 +import javax.tools.JavaFileObject;
    1.39 +import javax.tools.SimpleJavaFileObject;
    1.40 +
    1.41 +import com.sun.tools.classfile.*;
    1.42 +import com.sun.tools.javac.comp.Attr;
    1.43 +import com.sun.tools.javac.comp.AttrContext;
    1.44 +import com.sun.tools.javac.comp.Env;
    1.45 +import com.sun.tools.javac.file.JavacFileManager;
    1.46 +import com.sun.tools.javac.main.JavaCompiler;
    1.47 +import com.sun.tools.javac.tree.JCTree;
    1.48 +import com.sun.tools.javac.tree.JCTree.*;
    1.49 +import com.sun.tools.javac.util.Context;
    1.50 +import com.sun.tools.javac.util.List;
    1.51 +
    1.52 +import static com.sun.tools.javac.util.List.of;
    1.53 +import static com.sun.tools.javac.tree.JCTree.Tag.*;
    1.54 +
    1.55 +public class MissingLNTEntryForFinalizerTest {
    1.56 +    protected ReusableJavaCompiler tool;
    1.57 +    Context context;
    1.58 +
    1.59 +    MissingLNTEntryForFinalizerTest() {
    1.60 +        context = new Context();
    1.61 +        JavacFileManager.preRegister(context);
    1.62 +        MyAttr.preRegister(context);
    1.63 +        tool = new ReusableJavaCompiler(context);
    1.64 +    }
    1.65 +
    1.66 +    public static void main(String... args) throws Throwable {
    1.67 +        new MissingLNTEntryForFinalizerTest().test();
    1.68 +    }
    1.69 +
    1.70 +    void test() throws Throwable {
    1.71 +        JavaSource source = new JavaSource("1");
    1.72 +        tool.clear();
    1.73 +        List<JavaFileObject> inputs = of(source);
    1.74 +        try {
    1.75 +            tool.compile(inputs);
    1.76 +        } catch (Throwable ex) {
    1.77 +            throw new AssertionError(ex);
    1.78 +        }
    1.79 +        File testClasses = new File(".");
    1.80 +        File file = new File(testClasses, "Test1.class");
    1.81 +        ClassFile classFile = ClassFile.read(file);
    1.82 +        for (Method m : classFile.methods) {
    1.83 +            if (classFile.constant_pool.getUTF8Value(m.name_index).equals("foo")) {
    1.84 +                Code_attribute code = (Code_attribute)m.attributes.get(Attribute.Code);
    1.85 +                LineNumberTable_attribute lnt = (LineNumberTable_attribute)code.attributes.get(Attribute.LineNumberTable);
    1.86 +                checkLNT(lnt, MyAttr.lineNumber);
    1.87 +            }
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    void checkLNT(LineNumberTable_attribute lnt, int lineToCheckFor) {
    1.92 +        for (LineNumberTable_attribute.Entry e: lnt.line_number_table) {
    1.93 +            if (e.line_number == lineToCheckFor) {
    1.94 +                return;
    1.95 +            }
    1.96 +        }
    1.97 +        throw new AssertionError("seek line number not found in the LNT for method foo()");
    1.98 +    }
    1.99 +
   1.100 +    class JavaSource extends SimpleJavaFileObject {
   1.101 +        String id;
   1.102 +        String template =
   1.103 +                "import java.util.*;\n" +
   1.104 +                "class Test#Id {\n" +
   1.105 +                "    void foo() {\n" +
   1.106 +                "        List<String> l = null;\n" +
   1.107 +                "        String first = null;\n" +
   1.108 +                "        try {\n" +
   1.109 +                "            first = l.get(0);\n" +
   1.110 +                "        } finally {\n" +
   1.111 +                "            if (first != null) {\n" +
   1.112 +                "                System.out.println(\"finalizer\");\n" +
   1.113 +                "            }\n" +
   1.114 +                "        }\n" +
   1.115 +                "    }\n" +
   1.116 +                "}";
   1.117 +
   1.118 +        JavaSource(String id) {
   1.119 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.120 +            this.id = id;
   1.121 +        }
   1.122 +
   1.123 +        @Override
   1.124 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.125 +            return template.replace("#Id", id);
   1.126 +        }
   1.127 +    }
   1.128 +
   1.129 +    /* this class has been set up to do not depend on a fixed line number, this Attr subclass will
   1.130 +     * look for 'break' or 'continue' statements in order to find the actual line number they occupy.
   1.131 +     * This way the test can find if that line number appears in the LNT generated for a given class.
   1.132 +     */
   1.133 +    static class MyAttr extends Attr {
   1.134 +        static int lineNumber;
   1.135 +
   1.136 +        static void preRegister(Context context) {
   1.137 +            context.put(attrKey, (com.sun.tools.javac.util.Context.Factory<Attr>) c -> new MyAttr(c));
   1.138 +        }
   1.139 +
   1.140 +        MyAttr(Context context) {
   1.141 +            super(context);
   1.142 +        }
   1.143 +
   1.144 +        @Override
   1.145 +        public com.sun.tools.javac.code.Type attribStat(JCTree tree, Env<AttrContext> env) {
   1.146 +            com.sun.tools.javac.code.Type result = super.attribStat(tree, env);
   1.147 +            if (tree.hasTag(TRY)) {
   1.148 +                JCTry tryTree = (JCTry)tree;
   1.149 +                lineNumber = env.toplevel.lineMap.getLineNumber(tryTree.finalizer.endpos);
   1.150 +            }
   1.151 +            return result;
   1.152 +        }
   1.153 +    }
   1.154 +
   1.155 +    static class ReusableJavaCompiler extends JavaCompiler {
   1.156 +        ReusableJavaCompiler(Context context) {
   1.157 +            super(context);
   1.158 +        }
   1.159 +
   1.160 +        protected void checkReusable() {
   1.161 +            // do nothing
   1.162 +        }
   1.163 +
   1.164 +        @Override
   1.165 +        public void close() {
   1.166 +            //do nothing
   1.167 +        }
   1.168 +
   1.169 +        void clear() {
   1.170 +            //do nothing
   1.171 +        }
   1.172 +    }
   1.173 +}

mercurial