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