1 /*
   2  * Copyright (c) 2005, 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 4836939 6646613
  27  *  @summary JDI add addSourceNameFilter to ClassPrepareRequest
  28  *
  29  *  @author jjh
  30  *
  31  *  @modules jdk.jdi
  32  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  *  @run compile -g SourceNameFilterTest.java
  34  *  @run driver SourceNameFilterTest
  35  *  @run compile -g:none SourceNameFilterTest.java
  36  *  @run driver SourceNameFilterTest
  37  */
  38 // The compile -g:none suppresses the lineNumber table to trigger bug 6646613.
  39 
  40 import com.sun.jdi.*;
  41 import com.sun.jdi.event.*;
  42 import com.sun.jdi.request.*;
  43 
  44 import java.util.*;
  45 
  46     /********** target program **********/
  47 
  48 class SourceNameFilterTarg {
  49     static  void bkpt() {
  50     }
  51 
  52     public static void main(String[] args){
  53         System.out.println("Howdy!");
  54 
  55         LoadedLater1.doit();
  56         bkpt();
  57 
  58         LoadedLater2.doit();
  59         bkpt();
  60 
  61         LoadedLater3.doit();
  62         bkpt();
  63         System.out.println("Goodbye from SourceNameFilterTarg!");
  64     }
  65 }
  66 class LoadedLater1 {
  67     public static void doit() {
  68         System.out.println("didit1");
  69     }
  70 }
  71 
  72 class LoadedLater2 {
  73     public static void doit() {
  74         System.out.println("didit2");
  75     }
  76 }
  77 
  78 class LoadedLater3 {
  79     public static void doit() {
  80         System.out.println("didit3");
  81     }
  82 }
  83 
  84     /********** test program **********/
  85 
  86 public class SourceNameFilterTest extends TestScaffold {
  87     ReferenceType targetClass;
  88     ThreadReference mainThread;
  89     boolean gotEvent1 = false;
  90     boolean gotEvent2 = false;
  91     boolean gotEvent3 = false;
  92     ClassPrepareRequest cpReq;
  93     boolean shouldResume = false;
  94     SourceNameFilterTest (String args[]) {
  95         super(args);
  96     }
  97 
  98     public static void main(String[] args)      throws Exception {
  99         new SourceNameFilterTest(args).startTests();
 100     }
 101     public void eventSetComplete(EventSet set) {
 102         //System.out.println("jj: resuming, set = " + set);
 103         if (shouldResume) {
 104             set.resume();
 105             shouldResume = false;
 106         }
 107     }
 108 
 109     public void classPrepared(ClassPrepareEvent event) {
 110         shouldResume = true;
 111 
 112         ReferenceType rt = event.referenceType();
 113         String rtname = rt.name();
 114 
 115         if (rtname.equals("LoadedLater1")) {
 116             gotEvent1 = true;
 117         }
 118 
 119         if (rtname.equals("LoadedLater2")) {
 120             gotEvent2 = true;
 121         }
 122 
 123         if (rtname.equals("LoadedLater3")) {
 124             gotEvent3 = true;
 125         }
 126 
 127         // debug code
 128         if (false) {
 129             println("Got ClassPrepareEvent for : " + rtname);
 130             try {
 131                 println("    sourceName = " + rt.sourceName());
 132             } catch (AbsentInformationException ee) {
 133                 failure("failure: absent info on sourceName(): " + ee);
 134             }
 135 
 136             String stratum = rt.defaultStratum();
 137             println("    defaultStratum = " + stratum);
 138 
 139             try {
 140                 println("    sourceNames = " + rt.sourceNames(stratum));
 141             } catch (AbsentInformationException ee) {
 142                 failure("failure: absent info on sourceNames(): " + ee);
 143             }
 144             println("\nAvailable strata:  " + rt.availableStrata());
 145         }
 146     }
 147 
 148 
 149     /********** test core **********/
 150 
 151     protected void runTests() throws Exception {
 152         /*
 153          * Get to the top of main()
 154          * to determine targetClass and mainThread
 155          */
 156         BreakpointEvent bpe = startToMain("SourceNameFilterTarg");
 157         targetClass = bpe.location().declaringType();
 158         boolean noSourceName = false;
 159         try {
 160             targetClass.sourceName();
 161         } catch (AbsentInformationException ee) {
 162             noSourceName = true;
 163         }
 164         if (noSourceName) {
 165             println("-- Running with no source names");
 166         } else {
 167             println("-- Running with source names");
 168         }
 169 
 170         mainThread = bpe.thread();
 171         EventRequestManager erm = vm().eventRequestManager();
 172         addListener(this);
 173 
 174         /*
 175          * Resume the target listening for events
 176          * This should cause a class prepare event for LoadedLater1
 177          */
 178         cpReq = erm.createClassPrepareRequest();
 179         cpReq.enable();
 180         resumeTo("SourceNameFilterTarg", "bkpt", "()V");
 181 
 182         /*
 183          * This should cause us to not get a class prepared for
 184          * LoadedLater2 since it doesn't come from "jj"
 185          */
 186         cpReq.disable();
 187         cpReq.addSourceNameFilter("jj");
 188         cpReq.enable();
 189         resumeTo("SourceNameFilterTarg", "bkpt", "()V");
 190         cpReq.disable();
 191 
 192         /*
 193          * This should cause us to get a class prepare event for
 194          * LoadedLater3 except in the case where -g:none
 195          * was used to compile so that there is no LineNumberTable
 196          * and therefore, no source name for the class.
 197          */
 198         cpReq = erm.createClassPrepareRequest();
 199         cpReq.addSourceNameFilter("SourceNameFilterTest.java");
 200         cpReq.enable();
 201         resumeTo("SourceNameFilterTarg", "bkpt", "()V");
 202 
 203         listenUntilVMDisconnect();
 204 
 205         if (!gotEvent1) {
 206             failure("failure: Did not get a class prepare request " +
 207                     "for LoadedLater1");
 208         }
 209 
 210         if (gotEvent2) {
 211             failure("failure: Did get a class prepare request " +
 212                     "for LoadedLater2");
 213         }
 214 
 215         if (gotEvent3 && noSourceName) {
 216             failure("failure: Did get a class prepare request " +
 217                     "for LoadedLater3");
 218         }
 219         else if (!gotEvent3 && !noSourceName) {
 220             failure("failure: Did not get a class prepare request " +
 221                     "for LoadedLater3");
 222         }
 223 
 224         /*
 225          * deal with results of test
 226          * if anything has called failure("foo") testFailed will be true
 227          */
 228         if (!testFailed) {
 229             println("SourceNameFilterTest: passed");
 230         } else {
 231             throw new Exception("SourceNameFilterTest: failed");
 232         }
 233     }
 234 }