1 /*
2  * Copyright (c) 2003, 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 4642611
27  * @summary Test that method.allLineLocations() should
28  *          throw AbsentInformationException exception
29  * @author Serguei Spitsyn
30  *
31  * @run build TestScaffold VMConnection TargetListener TargetAdapter
32  * @run compile -g:none NoLocInfoTest.java
33  * @run driver NoLocInfoTest
34  */
35 import com.sun.jdi.*;
36 import com.sun.jdi.event.*;
37 import com.sun.jdi.request.*;
38 
39 import java.util.*;
40 
41     /********** target program **********/
42 
43 interface InterfaceNoLocInfoTarg {
instanceMeth()44     int instanceMeth();
instanceMeth1()45     int instanceMeth1();
46 }
47 
48 abstract class AbstractNoLocInfoTarg implements InterfaceNoLocInfoTarg {
49     protected int fld;
50 
51     // Constructor
AbstractNoLocInfoTarg()52     AbstractNoLocInfoTarg() {
53         fld = 1000;
54     }
55 
instanceMeth()56     public abstract int instanceMeth();
57 
instanceMeth1()58     public int instanceMeth1() {
59         fld = 999;
60         fld = instanceMeth();
61         return 0;
62     }
63 }
64 
65 class NoLocInfoTarg extends AbstractNoLocInfoTarg {
66 
67     // Class has a default constructor
68 
main(String[] args)69     public static void main(String[] args){
70         System.out.println("A number is: " + new NoLocInfoTarg().instanceMeth());
71     }
72 
staticMeth()73     public static int staticMeth() {
74         int i = 2;
75         return i;
76     }
77 
instanceMeth()78     public int instanceMeth() {
79         int i = 0;
80         i++;
81         return i + staticMeth();
82     }
83 
voidInstanceMeth()84     private void voidInstanceMeth() {}
staticNativeMeth()85     static native int staticNativeMeth();
instanceNativeMeth()86     native boolean instanceNativeMeth();
87 }
88 
89     /********** test program **********/
90 
91 public class NoLocInfoTest extends TestScaffold {
92     final String[] args;
93 
NoLocInfoTest(String args[])94     NoLocInfoTest (String args[]) {
95         super(args);
96         this.args = args;
97     }
98 
main(String[] args)99     public static void main(String[] args)      throws Exception {
100         new NoLocInfoTest(args).startTests();
101     }
102 
103     /********** test assist **********/
104 
getMethod(String className, String methodName)105     Method getMethod(String className, String methodName) {
106         List refs = vm().classesByName(className);
107         if (refs.size() != 1) {
108             failure("Failure: " + refs.size() +
109                     " ReferenceTypes named: " + className);
110             return null;
111         }
112         ReferenceType refType = (ReferenceType)refs.get(0);
113         List meths = refType.methodsByName(methodName);
114         if (meths.size() != 1) {
115             failure("Failure: " + meths.size() +
116                     " methods named: " + methodName);
117             return null;
118         }
119         return (Method)meths.get(0);
120     }
121 
checkLineNumberTable(String className, String methodName)122     void checkLineNumberTable(String className, String methodName) {
123         println("GetLineNumberTable for method: " + className + "." + methodName);
124         Method method = getMethod(className, methodName);
125 
126         try {
127             List locations = method.allLineLocations();
128             failure("Failure: com.sun.jdi.AbsentInformationException was expected; " +
129                     "LineNumberTable.size() = " + locations.size());
130         }
131         catch (com.sun.jdi.AbsentInformationException ex) {
132             println("Success: com.sun.jdi.AbsentInformationException thrown as expected");
133         }
134         println("");
135     }
136 
checkEmptyLineNumberTable(String className, String methodName)137     void checkEmptyLineNumberTable(String className, String methodName) {
138         println("GetLineNumberTable for abstract/native method: " +
139                  className + "." + methodName);
140         Method method = getMethod(className, methodName);
141 
142         try {
143             int size = method.allLineLocations().size();
144             if (size == 0) {
145                println("Succes: LineNumberTable.size() == " + size + " as expected");
146             } else {
147                failure("Failure: LineNumberTable.size()==" + size + ", but ZERO was expected");
148             }
149         }
150         catch (com.sun.jdi.AbsentInformationException ex) {
151             failure("Failure: com.sun.jdi.AbsentInformationException was not expected; ");
152         }
153         println("");
154     }
155 
156     /********** test core **********/
157 
runTests()158     protected void runTests() throws Exception {
159 
160         /*
161          * Get to the top of main() to get everything loaded
162          */
163         startToMain("NoLocInfoTarg");
164 
165         println("\n Abstract Methods:");
166         // For abtsract methods allLineLocations() always returns empty List
167         checkEmptyLineNumberTable("InterfaceNoLocInfoTarg", "instanceMeth");
168         checkEmptyLineNumberTable("InterfaceNoLocInfoTarg", "instanceMeth1");
169         checkEmptyLineNumberTable("AbstractNoLocInfoTarg",  "instanceMeth");
170 
171         println("\n Native Methods:");
172         // For native methods allLineLocations() always returns empty List
173         checkEmptyLineNumberTable("NoLocInfoTarg", "staticNativeMeth");
174         checkEmptyLineNumberTable("NoLocInfoTarg", "instanceNativeMeth");
175 
176         println("\n Non-Abstract Methods of Abstract class:");
177         checkLineNumberTable("AbstractNoLocInfoTarg", "<init>");
178         checkLineNumberTable("AbstractNoLocInfoTarg", "instanceMeth1");
179 
180         println("\n Methods of Non-Abstract class:");
181         checkLineNumberTable("NoLocInfoTarg", "<init>"); // default constructor
182         checkLineNumberTable("NoLocInfoTarg", "main");
183         checkLineNumberTable("NoLocInfoTarg", "instanceMeth");
184         checkLineNumberTable("NoLocInfoTarg", "instanceMeth1"); // inherited
185         checkLineNumberTable("NoLocInfoTarg", "voidInstanceMeth");
186 
187         /*
188          * resume until the end
189          */
190         listenUntilVMDisconnect();
191 
192         /*
193          * deal with results of test
194          * if anything has called failure("foo") testFailed will be true
195          */
196         if (!testFailed) {
197             println("NoLocInfoTest: passed");
198         } else {
199             throw new Exception("NoLocInfoTest: failed");
200         }
201     }
202 }
203