1/*
2 * Copyright (c) 2014, 2015, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8034091
27 * @summary Add LineNumberTable attributes for conditional operator (?:) split across several lines.
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 */
30
31import com.sun.tools.classfile.ClassFile;
32import com.sun.tools.classfile.ConstantPoolException;
33import com.sun.tools.classfile.Method;
34import com.sun.tools.classfile.Attribute;
35import com.sun.tools.classfile.Code_attribute;
36import com.sun.tools.classfile.LineNumberTable_attribute;
37import com.sun.tools.classfile.LineNumberTable_attribute.Entry;
38
39import java.io.File;
40import java.io.IOException;
41
42public class ConditionalLineNumberTest {
43    public static void main(String[] args) throws Exception {
44        // check that we have 5 consecutive entries for method()
45        Entry[] lines = findEntries();
46        if (lines == null || lines.length != 5)
47            throw new Exception("conditional line number table incorrect");
48
49        int current = lines[0].line_number;
50        for (Entry e : lines) {
51            if (e.line_number != current)
52                throw new Exception("conditional line number table incorrect");
53            current++;
54        }
55   }
56
57    static Entry[] findEntries() throws IOException, ConstantPoolException {
58        ClassFile self = ClassFile.read(ConditionalLineNumberTest.class.getResourceAsStream("ConditionalLineNumberTest.class"));
59        for (Method m : self.methods) {
60            if ("method".equals(m.getName(self.constant_pool))) {
61                Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
62                for (Attribute at : code_attribute.attributes) {
63                    if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
64                        return ((LineNumberTable_attribute)at).line_number_table;
65                    }
66                }
67            }
68        }
69        return null;
70    }
71
72    // This method should get one LineNumberTable entry per line
73    // in the method body.
74    public static String method(int field) {
75        String s = field % 2 == 0 ?
76            (field == 0 ? "false"
77             : "true" + field) : //Breakpoint
78            "false" + field; //Breakpoint
79        return s;
80    }
81}
82