1 /* 2 * Copyright (c) 2001, 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 4419453 27 * @summary Test that Method.location() returns the right values 28 * @author Robert Field 29 * 30 * @run build TestScaffold VMConnection TargetListener TargetAdapter 31 * @run compile -g LocationTest.java 32 * @run driver LocationTest 33 */ 34 import com.sun.jdi.*; 35 import com.sun.jdi.event.*; 36 import com.sun.jdi.request.*; 37 38 import java.util.*; 39 40 /********** target program **********/ 41 42 abstract class AbstractLocationTarg { 43 abstract void foo(); 44 } 45 46 class LocationTarg extends AbstractLocationTarg { 47 public static void main(String[] args){ 48 System.out.println("Howdy!"); // don't change the following 3 lines 49 } 50 void foo() { 51 System.out.println("Never here!"); // must be 3 lines after "Howdy!" 52 } 53 } 54 55 /********** test program **********/ 56 57 public class LocationTest extends TestScaffold { 58 ReferenceType targetClass; 59 ThreadReference mainThread; 60 61 LocationTest (String args[]) { 62 super(args); 63 } 64 65 public static void main(String[] args) throws Exception { 66 new LocationTest(args).startTests(); 67 } 68 69 /********** test assist **********/ 70 71 Location getLocation(String refName, String methodName) { 72 List refs = vm().classesByName(refName); 73 if (refs.size() != 1) { 74 failure("Test failure: " + refs.size() + 75 " ReferenceTypes named: " + refName); 76 return null; 77 } 78 ReferenceType refType = (ReferenceType)refs.get(0); 79 List meths = refType.methodsByName(methodName); 80 if (meths.size() != 1) { 81 failure("Test failure: " + meths.size() + 82 " methods named: " + methodName); 83 return null; 84 } 85 Method meth = (Method)meths.get(0); 86 return meth.location(); 87 } 88 89 /********** test core **********/ 90 91 protected void runTests() throws Exception { 92 Location loc; 93 94 /* 95 * Get to the top of main() to get everything loaded 96 */ 97 startToMain("LocationTarg"); 98 99 /* 100 * Test the values of location() 101 */ 102 loc = getLocation("AbstractLocationTarg", "foo"); 103 if (loc != null) { 104 failure("location of AbstractLocationTarg.foo() should have " + 105 "been null, but was: " + loc); 106 } 107 108 loc = getLocation("java.util.List", "clear"); 109 if (loc != null) { 110 failure("location of java.util.List.clear() " + 111 "should have been null, but was: " + loc); 112 } 113 114 loc = getLocation("java.lang.Object", "getClass"); 115 if (loc == null) { 116 failure("location of Object.getClass() " + 117 "should have been non-null, but was: " + loc); 118 } else { 119 if (!loc.declaringType().name().equals("java.lang.Object")) { 120 failure("location.declaringType() of Object.getClass() " + 121 "should have been java.lang.Object, but was: " + 122 loc.declaringType()); 123 } 124 if (!loc.method().name().equals("getClass")) { 125 failure("location.method() of Object.getClass() " + 126 "should have been getClass, but was: " + 127 loc.method()); 128 } 129 if (loc.codeIndex() != -1) { 130 failure("location.codeIndex() of Object.getClass() " + 131 "should have been -1, but was: " + 132 loc.codeIndex()); 133 } 134 if (loc.lineNumber() != -1) { 135 failure("location.lineNumber() of Object.getClass() " + 136 "should have been -1, but was: " + 137 loc.lineNumber()); 138 } 139 } 140 Location mainLoc = getLocation("LocationTarg", "main"); 141 loc = getLocation("LocationTarg", "foo"); 142 if (loc == null) { 143 failure("location of LocationTarg.foo() " + 144 "should have been non-null, but was: " + loc); 145 } else { 146 if (!loc.declaringType().name().equals("LocationTarg")) { 147 failure("location.declaringType() of LocationTarg.foo() " + 148 "should have been LocationTarg, but was: " + 149 loc.declaringType()); 150 } 151 if (!loc.method().name().equals("foo")) { 152 failure("location.method() of LocationTarg.foo() " + 153 "should have been foo, but was: " + 154 loc.method()); 155 } 156 if (loc.codeIndex() != 0) { // implementation dependent!!! 157 failure("location.codeIndex() of LocationTarg.foo() " + 158 "should have been 0, but was: " + 159 loc.codeIndex()); 160 } 161 if (loc.lineNumber() != (mainLoc.lineNumber() + 3)) { 162 failure("location.lineNumber() of LocationTarg.foo() " + 163 "should have been " + (mainLoc.lineNumber() + 3) + 164 ", but was: " + loc.lineNumber()); 165 } 166 } 167 168 169 /* 170 * resume until the end 171 */ 172 listenUntilVMDisconnect(); 173 174 /* 175 * deal with results of test 176 * if anything has called failure("foo") testFailed will be true 177 */ 178 if (!testFailed) { 179 println("LocationTest: passed"); 180 } else { 181 throw new Exception("LocationTest: failed"); 182 } 183 } 184 }