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