1 /*
   2  * Copyright (c) 1999, 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 4248728
  27  * @summary Test ReferenceType.allLineLocations
  28  * @author Gordon Hirsch
  29  *
  30  * @run build JDIScaffold VMConnection
  31  * @run compile -g RefTypes.java
  32  * @run build AllLineLocations
  33  *
  34  * @run driver AllLineLocations RefTypes
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.List;
  41 
  42 public class AllLineLocations extends JDIScaffold {
  43     final String[] args;
  44 
  45     public static void main(String args[]) throws Exception {
  46         new AllLineLocations(args).startTests();
  47     }
  48 
  49     AllLineLocations(String args[]) {
  50         super();
  51         this.args = args;
  52     }
  53 
  54     protected void runTests() throws Exception {
  55         connect(args);
  56         waitForVMStart();
  57 
  58         /*
  59          * Get to a point where the classes are loaded.
  60          */
  61         BreakpointEvent bp = resumeTo("RefTypes", "loadClasses", "()V");
  62         stepOut(bp.thread());
  63 
  64         /*
  65          * These classes should have no line numbers, except for
  66          * one in the implicit constructor.
  67          */
  68         ReferenceType rt = findReferenceType("AllAbstract");
  69         if (rt == null) {
  70             throw new Exception("AllAbstract: not loaded");
  71         }
  72         List list = rt.allLineLocations();
  73         if (list.size() != 1) {
  74             throw new Exception("AllAbstract: incorrect number of line locations");
  75         }
  76         if (rt.locationsOfLine(5000).size() != 0) {
  77             throw new Exception("AllAbstract: incorrect locationsOfLine");
  78         }
  79         Method method = findMethod(rt, "<init>", "()V");
  80         if (method == null) {
  81             throw new Exception("AllAbstract.<init> not found");
  82         }
  83         List list2 = method.allLineLocations();
  84         if (!list2.equals(list)) {
  85             throw new Exception("AllAbstract: line locations in wrong method");
  86         }
  87         if (method.locationsOfLine(5000).size() != 0) {
  88             throw new Exception("AllAbstract: incorrect locationsOfLine");
  89         }
  90         System.out.println("AllAbstract: passed");
  91 
  92         rt = findReferenceType("AllNative");
  93         if (rt == null) {
  94             throw new Exception("AllNative: not loaded");
  95         }
  96         list = rt.allLineLocations();
  97         if (list.size() != 1) {
  98             throw new Exception("AllNative: incorrect number of line locations");
  99         }
 100         if (rt.locationsOfLine(5000).size() != 0) {
 101             throw new Exception("AllNative: incorrect locationsOfLine");
 102         }
 103         method = findMethod(rt, "<init>", "()V");
 104         if (method == null) {
 105             throw new Exception("AllNative.<init> not found");
 106         }
 107         list2 = method.allLineLocations();
 108         if (!list2.equals(list)) {
 109             throw new Exception("AllNative: line locations in wrong method");
 110         }
 111         if (method.locationsOfLine(5000).size() != 0) {
 112             throw new Exception("AllNative: incorrect locationsOfLine");
 113         }
 114         System.out.println("AllNative: passed");
 115 
 116         rt = findReferenceType("Interface");
 117         if (rt == null) {
 118             throw new Exception("Interface: not loaded");
 119         }
 120         list = rt.allLineLocations();
 121         if (list.size() != 0) {
 122             throw new Exception("Interface: locations reported for abstract methods");
 123         }
 124         System.out.println("Interface: passed");
 125 
 126         /*
 127          * These classes have line numbers in one method and
 128          * in the implicit constructor.
 129          */
 130         rt = findReferenceType("Abstract");
 131         if (rt == null) {
 132             throw new Exception("Abstract: not loaded");
 133         }
 134         list = rt.allLineLocations();
 135         if (list.size() != 5) {
 136             throw new Exception("Abstract: incorrect number of line locations");
 137         }
 138         method = findMethod(rt, "b", "()V");
 139         if (method == null) {
 140             throw new Exception("Abstract.b not found");
 141         }
 142         list2 = method.allLineLocations();
 143         list.removeAll(list2);
 144 
 145         // Remaining location should be in constructor
 146         if ((list.size() != 1) ||
 147             !(((Location)list.get(0)).method().name().equals("<init>"))) {
 148             throw new Exception("Abstract: line locations in wrong method");
 149         }
 150         if (method.locationsOfLine(20).size() != 1) {
 151             throw new Exception("Abstract method: incorrect locationsOfLine");
 152         }
 153         if (method.locationsOfLine(5000).size() != 0) {
 154             throw new Exception("Abstract method: incorrect locationsOfLine");
 155         }
 156         method = findMethod(rt, "a", "()V");
 157         if (method.locationsOfLine(5000).size() != 0) {
 158             throw new Exception("Abstract method: incorrect locationsOfLine");
 159         }
 160         System.out.println("Abstract: passed");
 161 
 162         rt = findReferenceType("Native");
 163         if (rt == null) {
 164             throw new Exception("Native: not loaded");
 165         }
 166         list = rt.allLineLocations();
 167         if (list.size() != 5) {
 168             throw new Exception("Native: incorrect number of line locations");
 169         }
 170         if (rt.locationsOfLine(5000).size() != 0) {
 171             throw new Exception("Native: incorrect locationsOfLine");
 172         }
 173         method = findMethod(rt, "b", "()V");
 174         if (method == null) {
 175             throw new Exception("Native.b not found");
 176         }
 177         list2 = method.allLineLocations();
 178         list.removeAll(list2);
 179 
 180         // Remaining location should be in constructor
 181         if ((list.size() != 1) ||
 182             !(((Location)list.get(0)).method().name().equals("<init>"))) {
 183             throw new Exception("Native: line locations in wrong method");
 184         }
 185         if (method.locationsOfLine(30).size() != 1) {
 186             throw new Exception("Native method: incorrect locationsOfLine");
 187         }
 188         if (method.locationsOfLine(5000).size() != 0) {
 189             throw new Exception("Native method: incorrect locationsOfLine");
 190         }
 191         method = findMethod(rt, "a", "()V");
 192         if (method.locationsOfLine(5000).size() != 0) {
 193             throw new Exception("Native method: incorrect locationsOfLine");
 194         }
 195         System.out.println("Native: passed");
 196 
 197         rt = findReferenceType("AbstractAndNative");
 198         if (rt == null) {
 199             throw new Exception("AbstractAndNative: not loaded");
 200         }
 201         list = rt.allLineLocations();
 202         if (list.size() != 5) {
 203             throw new Exception("AbstractAndNative: incorrect number of line locations");
 204         }
 205         if (rt.locationsOfLine(5000).size() != 0) {
 206             throw new Exception("AbstractAndNative: incorrect locationsOfLine");
 207         }
 208         method = findMethod(rt, "c", "()V");
 209         if (method == null) {
 210             throw new Exception("AbstractAndNative.c not found");
 211         }
 212         list2 = method.allLineLocations();
 213         list.removeAll(list2);
 214 
 215         // Remaining location should be in constructor
 216         if ((list.size() != 1) ||
 217             !(((Location)list.get(0)).method().name().equals("<init>"))) {
 218             throw new Exception("AbstractAndNative: line locations in wrong method");
 219         }
 220         System.out.println("AbstractAndNative: passed");
 221 
 222         // Allow application to complete
 223         resumeToVMDeath();
 224     }
 225 }