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