test/tools/javac/T8022186/DeadCodeGeneratedForEmptyTryTest.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T8022186/DeadCodeGeneratedForEmptyTryTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,166 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @test
    1.31 + * @bug 8022186
    1.32 + * @summary javac generates dead code if a try with an empty body has a finalizer
    1.33 + */
    1.34 +
    1.35 +import com.sun.tools.classfile.Attribute;
    1.36 +import com.sun.tools.classfile.ClassFile;
    1.37 +import com.sun.tools.classfile.Code_attribute;
    1.38 +import com.sun.tools.classfile.ConstantPool;
    1.39 +import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info;
    1.40 +import com.sun.tools.classfile.ConstantPool.CPInfo;
    1.41 +import com.sun.tools.classfile.ConstantPool.InvalidIndex;
    1.42 +import com.sun.tools.classfile.Instruction;
    1.43 +import com.sun.tools.classfile.Instruction.KindVisitor;
    1.44 +import com.sun.tools.classfile.Instruction.TypeKind;
    1.45 +import com.sun.tools.classfile.Method;
    1.46 +import com.sun.tools.javac.util.Assert;
    1.47 +import java.io.BufferedInputStream;
    1.48 +import java.nio.file.Files;
    1.49 +import java.nio.file.Path;
    1.50 +import java.nio.file.Paths;
    1.51 +
    1.52 +public class DeadCodeGeneratedForEmptyTryTest {
    1.53 +
    1.54 +    public static void main(String[] args) throws Exception {
    1.55 +        new DeadCodeGeneratedForEmptyTryTest().run();
    1.56 +    }
    1.57 +
    1.58 +    void run() throws Exception {
    1.59 +        checkClassFile(Paths.get(System.getProperty("test.classes"),
    1.60 +                this.getClass().getName() + "$Test.class"));
    1.61 +    }
    1.62 +
    1.63 +    int utf8Index;
    1.64 +    int numberOfRefToStr = 0;
    1.65 +    ConstantPool constantPool;
    1.66 +
    1.67 +    void checkClassFile(final Path path) throws Exception {
    1.68 +        ClassFile classFile = ClassFile.read(
    1.69 +                new BufferedInputStream(Files.newInputStream(path)));
    1.70 +        constantPool = classFile.constant_pool;
    1.71 +        utf8Index = constantPool.getUTF8Index("STR_TO_LOOK_FOR");
    1.72 +        for (Method method: classFile.methods) {
    1.73 +            if (method.getName(constantPool).equals("methodToLookFor")) {
    1.74 +                Code_attribute codeAtt = (Code_attribute)method.attributes.get(Attribute.Code);
    1.75 +                for (Instruction inst: codeAtt.getInstructions()) {
    1.76 +                    inst.accept(codeVisitor, null);
    1.77 +                }
    1.78 +            }
    1.79 +        }
    1.80 +        Assert.check(numberOfRefToStr == 1,
    1.81 +                "There should only be one reference to a CONSTANT_String_info structure in the generated code");
    1.82 +    }
    1.83 +
    1.84 +    CodeVisitor codeVisitor = new CodeVisitor();
    1.85 +
    1.86 +    class CodeVisitor implements KindVisitor<Void, Void> {
    1.87 +
    1.88 +        void checkIndirectRefToString(int cp_index) {
    1.89 +            try {
    1.90 +                CPInfo cInfo = constantPool.get(cp_index);
    1.91 +                if (cInfo instanceof CONSTANT_String_info) {
    1.92 +                    CONSTANT_String_info strInfo = (CONSTANT_String_info)cInfo;
    1.93 +                    if (strInfo.string_index == utf8Index) {
    1.94 +                        numberOfRefToStr++;
    1.95 +                    }
    1.96 +                }
    1.97 +            } catch (InvalidIndex ex) {
    1.98 +                throw new AssertionError("invalid constant pool index at " + cp_index);
    1.99 +            }
   1.100 +        }
   1.101 +
   1.102 +        @Override
   1.103 +        public Void visitNoOperands(Instruction instr, Void p) {
   1.104 +            return null;
   1.105 +        }
   1.106 +
   1.107 +        @Override
   1.108 +        public Void visitArrayType(Instruction instr, TypeKind kind, Void p) {
   1.109 +            return null;
   1.110 +        }
   1.111 +
   1.112 +        @Override
   1.113 +        public Void visitBranch(Instruction instr, int offset, Void p) {
   1.114 +            return null;
   1.115 +        }
   1.116 +
   1.117 +        @Override
   1.118 +        public Void visitConstantPoolRef(Instruction instr, int index, Void p) {
   1.119 +            checkIndirectRefToString(index);
   1.120 +            return null;
   1.121 +        }
   1.122 +
   1.123 +        @Override
   1.124 +        public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) {
   1.125 +            checkIndirectRefToString(index);
   1.126 +            return null;
   1.127 +        }
   1.128 +
   1.129 +        @Override
   1.130 +        public Void visitLocal(Instruction instr, int index, Void p) {
   1.131 +            return null;
   1.132 +        }
   1.133 +
   1.134 +        @Override
   1.135 +        public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) {
   1.136 +            return null;
   1.137 +        }
   1.138 +
   1.139 +        @Override
   1.140 +        public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, Void p) {
   1.141 +            return null;
   1.142 +        }
   1.143 +
   1.144 +        @Override
   1.145 +        public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, Void p) {
   1.146 +            return null;
   1.147 +        }
   1.148 +
   1.149 +        @Override
   1.150 +        public Void visitValue(Instruction instr, int value, Void p) {
   1.151 +            return null;
   1.152 +        }
   1.153 +
   1.154 +        @Override
   1.155 +        public Void visitUnknown(Instruction instr, Void p) {
   1.156 +            return null;
   1.157 +        }
   1.158 +
   1.159 +    }
   1.160 +
   1.161 +    public class Test {
   1.162 +        void methodToLookFor() {
   1.163 +            try {
   1.164 +            } finally {
   1.165 +                System.out.println("STR_TO_LOOK_FOR");
   1.166 +            }
   1.167 +        }
   1.168 +    }
   1.169 +}

mercurial