1 /* |
1 /* |
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
34 import com.sun.tools.classfile.Attribute; |
34 import com.sun.tools.classfile.Attribute; |
35 import com.sun.tools.classfile.Code_attribute; |
35 import com.sun.tools.classfile.Code_attribute; |
36 import com.sun.tools.classfile.LineNumberTable_attribute; |
36 import com.sun.tools.classfile.LineNumberTable_attribute; |
37 import com.sun.tools.classfile.LineNumberTable_attribute.Entry; |
37 import com.sun.tools.classfile.LineNumberTable_attribute.Entry; |
38 |
38 |
39 import java.io.File; |
|
40 import java.io.IOException; |
39 import java.io.IOException; |
41 |
40 |
42 public class FinallyLineNumberTest { |
41 public class FinallyLineNumberTest { |
43 public static void main(String[] args) throws Exception { |
42 public static void main(String[] args) throws Exception { |
44 // check that we have 5 consecutive entries for method() |
43 // check that we have 5 consecutive entries for method() |
45 Entry[] lines = findEntries(); |
44 Entry[] lines = findEntries(); |
46 if (lines == null) { |
45 if (lines == null) { |
47 throw new Exception("finally line number table could not be loaded"); |
46 throw new Exception("finally line number table could not be loaded"); |
48 } |
47 } |
49 if (lines.length != 4) { |
48 if (lines.length != 5) { |
50 // Help debug |
49 // Help debug |
51 System.err.println("LineTable error, got lines:"); |
50 System.err.println("LineTable error, got lines:"); |
52 for (Entry e : lines) { |
51 for (Entry e : lines) { |
53 System.err.println(e.line_number); |
52 System.err.println(e.line_number); |
54 } |
53 } |
55 throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=4"); |
54 throw new Exception("finally line number table incorrect: length=" + lines.length + " expected length=5"); |
56 } |
55 } |
57 |
56 |
58 // return null line, for the load null operation |
57 // return null line, for the load null operation |
59 int current = lines[0].line_number; |
58 int current = lines[0].line_number; |
60 int first = current; |
59 int first = current; |
69 current = lines[2].line_number; |
68 current = lines[2].line_number; |
70 if (current != first) { |
69 if (current != first) { |
71 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first); |
70 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + first); |
72 } |
71 } |
73 |
72 |
74 // finally line, for when exception is thrown |
73 // for when exception is thrown |
75 current = lines[3].line_number; |
74 current = lines[3].line_number; |
76 if (current != first + 2) { |
75 if (current != first + 2) { |
77 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2)); |
76 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 2)); |
|
77 } |
|
78 |
|
79 // the '}' closing the finally block |
|
80 current = lines[4].line_number; |
|
81 if (current != first + 3) { |
|
82 throw new Exception("finally line number table incorrect: got=" + current + " expected=" + (first + 3)); |
78 } |
83 } |
79 } |
84 } |
80 |
85 |
81 static Entry[] findEntries() throws IOException, ConstantPoolException { |
86 static Entry[] findEntries() throws IOException, ConstantPoolException { |
82 ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class")); |
87 ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class")); |