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