NullCheckLineNumberTest.java revision 4181:0426d4e746af
1/* 2 * @test /nodynamiccopyright/ 3 * @bug 8172880 4 * @summary Wrong LineNumberTable for synthetic null checks 5 * @modules jdk.jdeps/com.sun.tools.classfile 6 */ 7 8import com.sun.tools.classfile.ClassFile; 9import com.sun.tools.classfile.ConstantPoolException; 10import com.sun.tools.classfile.Method; 11import com.sun.tools.classfile.Attribute; 12import com.sun.tools.classfile.Code_attribute; 13import com.sun.tools.classfile.LineNumberTable_attribute; 14 15import java.io.IOException; 16import java.util.AbstractMap.SimpleEntry; 17import java.util.Arrays; 18import java.util.List; 19import java.util.Map.Entry; 20import java.util.Objects; 21import java.util.stream.Collectors; 22import java.util.stream.Stream; 23 24public class NullCheckLineNumberTest { 25 26 //test data: 27 static class Test { 28 29 public Test() { 30 String a = "", b = null; 31 32 Stream.of("x") 33 .filter(a::equals) 34 .filter(b::equals) 35 .count(); 36 } 37 38 } 39 40 public static void main(String[] args) throws Exception { 41 List<Entry> actualEntries = findEntries(); 42 List<Entry> expectedEntries = List.of( 43 new SimpleEntry<>(29, 0), 44 new SimpleEntry<>(30, 4), 45 new SimpleEntry<>(32, 9), 46 new SimpleEntry<>(33, 16), 47 new SimpleEntry<>(34, 32), 48 new SimpleEntry<>(35, 46), 49 new SimpleEntry<>(36, 52) 50 ); 51 if (!Objects.equals(actualEntries, expectedEntries)) { 52 error(String.format("Unexpected LineNumberTable: %s", actualEntries.toString())); 53 } 54 55 try { 56 new Test(); 57 } catch (NullPointerException npe) { 58 if (Arrays.stream(npe.getStackTrace()) 59 .noneMatch(se -> se.getFileName().contains("NullCheckLineNumberTest") && 60 se.getLineNumber() == 34)) { 61 throw new AssertionError("Should go through line 34!"); 62 } 63 } 64 } 65 66 static List<Entry> findEntries() throws IOException, ConstantPoolException { 67 ClassFile self = ClassFile.read(NullCheckLineNumberTest.Test.class.getResourceAsStream("NullCheckLineNumberTest$Test.class")); 68 for (Method m : self.methods) { 69 if ("<init>".equals(m.getName(self.constant_pool))) { 70 Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code); 71 for (Attribute at : code_attribute.attributes) { 72 if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) { 73 return Arrays.stream(((LineNumberTable_attribute)at).line_number_table) 74 .map(e -> new SimpleEntry<> (e.line_number, e.start_pc)) 75 .collect(Collectors.toList()); 76 } 77 } 78 } 79 } 80 return null; 81 } 82 83 static void error(String msg) { 84 throw new AssertionError(msg); 85 } 86 87} 88