|
1 /* |
|
2 * Copyright (c) 2013, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * @test |
|
28 * @bug 8022186 |
|
29 * @summary javac generates dead code if a try with an empty body has a finalizer |
|
30 */ |
|
31 |
|
32 import com.sun.tools.classfile.Attribute; |
|
33 import com.sun.tools.classfile.ClassFile; |
|
34 import com.sun.tools.classfile.Code_attribute; |
|
35 import com.sun.tools.classfile.ConstantPool; |
|
36 import com.sun.tools.classfile.ConstantPool.CONSTANT_String_info; |
|
37 import com.sun.tools.classfile.ConstantPool.CPInfo; |
|
38 import com.sun.tools.classfile.ConstantPool.InvalidIndex; |
|
39 import com.sun.tools.classfile.Instruction; |
|
40 import com.sun.tools.classfile.Instruction.KindVisitor; |
|
41 import com.sun.tools.classfile.Instruction.TypeKind; |
|
42 import com.sun.tools.classfile.Method; |
|
43 import com.sun.tools.javac.util.Assert; |
|
44 import java.io.BufferedInputStream; |
|
45 import java.nio.file.Files; |
|
46 import java.nio.file.Path; |
|
47 import java.nio.file.Paths; |
|
48 |
|
49 public class DeadCodeGeneratedForEmptyTryTest { |
|
50 |
|
51 public static void main(String[] args) throws Exception { |
|
52 new DeadCodeGeneratedForEmptyTryTest().run(); |
|
53 } |
|
54 |
|
55 void run() throws Exception { |
|
56 checkClassFile(Paths.get(System.getProperty("test.classes"), |
|
57 this.getClass().getName() + "$Test.class")); |
|
58 } |
|
59 |
|
60 int utf8Index; |
|
61 int numberOfRefToStr = 0; |
|
62 ConstantPool constantPool; |
|
63 |
|
64 void checkClassFile(final Path path) throws Exception { |
|
65 ClassFile classFile = ClassFile.read( |
|
66 new BufferedInputStream(Files.newInputStream(path))); |
|
67 constantPool = classFile.constant_pool; |
|
68 utf8Index = constantPool.getUTF8Index("STR_TO_LOOK_FOR"); |
|
69 for (Method method: classFile.methods) { |
|
70 if (method.getName(constantPool).equals("methodToLookFor")) { |
|
71 Code_attribute codeAtt = (Code_attribute)method.attributes.get(Attribute.Code); |
|
72 for (Instruction inst: codeAtt.getInstructions()) { |
|
73 inst.accept(codeVisitor, null); |
|
74 } |
|
75 } |
|
76 } |
|
77 Assert.check(numberOfRefToStr == 1, |
|
78 "There should only be one reference to a CONSTANT_String_info structure in the generated code"); |
|
79 } |
|
80 |
|
81 CodeVisitor codeVisitor = new CodeVisitor(); |
|
82 |
|
83 class CodeVisitor implements KindVisitor<Void, Void> { |
|
84 |
|
85 void checkIndirectRefToString(int cp_index) { |
|
86 try { |
|
87 CPInfo cInfo = constantPool.get(cp_index); |
|
88 if (cInfo instanceof CONSTANT_String_info) { |
|
89 CONSTANT_String_info strInfo = (CONSTANT_String_info)cInfo; |
|
90 if (strInfo.string_index == utf8Index) { |
|
91 numberOfRefToStr++; |
|
92 } |
|
93 } |
|
94 } catch (InvalidIndex ex) { |
|
95 throw new AssertionError("invalid constant pool index at " + cp_index); |
|
96 } |
|
97 } |
|
98 |
|
99 @Override |
|
100 public Void visitNoOperands(Instruction instr, Void p) { |
|
101 return null; |
|
102 } |
|
103 |
|
104 @Override |
|
105 public Void visitArrayType(Instruction instr, TypeKind kind, Void p) { |
|
106 return null; |
|
107 } |
|
108 |
|
109 @Override |
|
110 public Void visitBranch(Instruction instr, int offset, Void p) { |
|
111 return null; |
|
112 } |
|
113 |
|
114 @Override |
|
115 public Void visitConstantPoolRef(Instruction instr, int index, Void p) { |
|
116 checkIndirectRefToString(index); |
|
117 return null; |
|
118 } |
|
119 |
|
120 @Override |
|
121 public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) { |
|
122 checkIndirectRefToString(index); |
|
123 return null; |
|
124 } |
|
125 |
|
126 @Override |
|
127 public Void visitLocal(Instruction instr, int index, Void p) { |
|
128 return null; |
|
129 } |
|
130 |
|
131 @Override |
|
132 public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) { |
|
133 return null; |
|
134 } |
|
135 |
|
136 @Override |
|
137 public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, Void p) { |
|
138 return null; |
|
139 } |
|
140 |
|
141 @Override |
|
142 public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, Void p) { |
|
143 return null; |
|
144 } |
|
145 |
|
146 @Override |
|
147 public Void visitValue(Instruction instr, int value, Void p) { |
|
148 return null; |
|
149 } |
|
150 |
|
151 @Override |
|
152 public Void visitUnknown(Instruction instr, Void p) { |
|
153 return null; |
|
154 } |
|
155 |
|
156 } |
|
157 |
|
158 public class Test { |
|
159 void methodToLookFor() { |
|
160 try { |
|
161 } finally { |
|
162 System.out.println("STR_TO_LOOK_FOR"); |
|
163 } |
|
164 } |
|
165 } |
|
166 } |