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