1 /*
   2  * Copyright (c) 2001, 2002, 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 4408582
  27  *  @summary Test fix for: JDWP: WatchpointEvents outside of class filtered out
  28  *
  29  *  @author Tim Bell
  30  *
  31  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  32  *  @run compile -g FieldWatchpoints.java
  33  *  @run driver FieldWatchpoints
  34  */
  35 import com.sun.jdi.*;
  36 import com.sun.jdi.event.*;
  37 import java.util.*;
  38 
  39 class A extends Object {
  40     public static int aField = 0;
  41 }
  42 
  43 class B extends A {
  44 }
  45 
  46 class FieldWatchpointsDebugee {
  47     public void update (){
  48         /* test direct modify access by other class */
  49         A.aField = 7;
  50         B.aField = 11;
  51     }
  52     public void access (){
  53         /* test direct read access by other class */
  54         System.out.print("aField is: ");
  55         System.out.println(A.aField);
  56     }
  57     public static void main(String[] args){
  58         A testA = new A();
  59         B testB = new B();
  60         FieldWatchpointsDebugee my =
  61             new FieldWatchpointsDebugee();
  62         my.update();
  63         my.access();
  64     }
  65 }
  66 
  67 public class FieldWatchpoints extends TestScaffold {
  68     boolean fieldModifyReported = false;
  69     boolean fieldAccessReported = false;
  70 
  71     FieldWatchpoints (String args[]) {
  72         super(args);
  73     }
  74 
  75     public static void main(String[] args)      throws Exception {
  76         new FieldWatchpoints (args).startTests();
  77     }
  78 
  79     protected void runTests() throws Exception {
  80         startTo("FieldWatchpointsDebugee", "update", "()V");
  81 
  82         try {
  83             /*
  84              * Set a modification watch on aField
  85              */
  86             ReferenceType rt = findReferenceType("A");
  87             String fieldName = "aField";
  88             Field field = rt.fieldByName(fieldName);
  89             if (field == null) {
  90                 throw new Exception ("Field name not found: " + fieldName);
  91             }
  92             com.sun.jdi.request.EventRequest req =
  93                eventRequestManager().createModificationWatchpointRequest(field);
  94             req.setSuspendPolicy(com.sun.jdi.request.EventRequest.SUSPEND_ALL);
  95             req.enable();
  96 
  97             /*
  98              * Set an access watch on aField
  99              */
 100             req =
 101                eventRequestManager().createAccessWatchpointRequest(field);
 102             req.setSuspendPolicy(com.sun.jdi.request.EventRequest.SUSPEND_ALL);
 103             req.enable();
 104 
 105             addListener (new TargetAdapter() {
 106                     EventSet lastSet = null;
 107 
 108                     public void eventSetReceived(EventSet set) {
 109                         lastSet = set;
 110                     }
 111                     public void fieldModified(ModificationWatchpointEvent event) {
 112                         System.out.println("Field modified: " + event);
 113                         fieldModifyReported = true;
 114                         lastSet.resume();
 115                     }
 116                     public void fieldAccessed(AccessWatchpointEvent event) {
 117                         System.out.println("Field accessed: " + event);
 118                         fieldAccessReported = true;
 119                         lastSet.resume();
 120                     }
 121                 });
 122 
 123             vm().resume();
 124 
 125         } catch (Exception ex){
 126             ex.printStackTrace();
 127             testFailed = true;
 128         } finally {
 129             // Allow application to complete and shut down
 130             resumeToVMDisconnect();
 131         }
 132         if (!testFailed && fieldModifyReported && fieldAccessReported) {
 133             System.out.println("FieldWatchpoints: passed");
 134         } else {
 135             throw new Exception("FieldWatchpoints: failed");
 136         }
 137     }
 138 }