0N/A/*
2362N/A * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4312961
0N/A * @summary Verify that an instance filter on a MethodEntryRequest works
0N/A * properly.
0N/A *
0N/A * @author Robert Field/Jim Holmlund
0N/A *
0N/A * @run build TestScaffold VMConnection TargetAdapter TargetListener
0N/A * @run compile -g InstanceFilter.java
0N/A * @run main/othervm InstanceFilter
0N/A */
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport com.sun.jdi.request.*;
0N/A
0N/Aclass InstanceFilterTarg {
0N/A static InstanceFilterTarg first = new InstanceFilterTarg();
0N/A static InstanceFilterTarg second = new InstanceFilterTarg();
0N/A static InstanceFilterTarg third = new InstanceFilterTarg();
0N/A
0N/A public static void main(String args[]) {
0N/A start();
0N/A }
0N/A
0N/A static void start() {
0N/A first.go();
0N/A second.go();
0N/A third.go();
0N/A }
0N/A
0N/A void go() {
0N/A one();
0N/A two();
0N/A three();
0N/A }
0N/A
0N/A void one() {
0N/A }
0N/A
0N/A void two() {
0N/A }
0N/A
0N/A void three() {
0N/A }
0N/A}
0N/A
0N/Apublic class InstanceFilter extends TestScaffold {
0N/A ReferenceType targetClass;
0N/A
0N/A ObjectReference theInstance;
0N/A MethodEntryRequest methodEntryRequest;
0N/A int methodCount = 0;
0N/A // These are the methods for which we expect to get MethodEntryEvents.
0N/A String[] expectedMethods = new String[] { "go", "one", "two", "three"};
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A new InstanceFilter(args).startTests();
0N/A }
0N/A
0N/A InstanceFilter(String args[]) throws Exception {
0N/A super(args);
0N/A }
0N/A
0N/A /**
0N/A * Override TestScaffold.methodEntered. This should get called
0N/A * once for each method named in 'expectedMethods', and for
0N/A * the instance that we select to filter on.
0N/A */
0N/A public void methodEntered(MethodEntryEvent event) {
0N/A if (testFailed) {
0N/A return;
0N/A }
0N/A // Find the instance and verify that it is
0N/A // the one we want.
0N/A ObjectReference theThis;
0N/A try {
0N/A theThis = event.thread().frame(0).thisObject();
0N/A } catch (IncompatibleThreadStateException ee) {
0N/A failure("FAILED: Exception occured in methodEntered: " + ee);
0N/A return;
0N/A }
0N/A if (theThis == null) {
0N/A // This happens when the thread has exited.
0N/A methodEntryRequest.disable();
0N/A return;
0N/A }
0N/A
0N/A if (!theThis.equals(theInstance)) {
0N/A failure("FAILED: Got a hit on a non-selected instance");
0N/A }
0N/A
0N/A // fail if we don't get called for each of the expected methods
0N/A {
0N/A String methodStr = event.location().method().name();
0N/A
0N/A if (methodCount >= expectedMethods.length) {
0N/A failure("FAILED: Got too many methodEntryEvents");
0N/A } else if (methodStr.indexOf(expectedMethods[methodCount]) == -1) {
0N/A failure("FAILED: Expected method: " + expectedMethods[methodCount]);
0N/A }
0N/A methodCount++;
0N/A println("Method: " + methodStr);
0N/A }
0N/A }
0N/A
0N/A protected void runTests() throws Exception {
0N/A
0N/A BreakpointEvent bpe = startTo("InstanceFilterTarg", "go", "()V");
0N/A targetClass = bpe.location().declaringType();
0N/A
0N/A Field field = targetClass.fieldByName("second");
0N/A theInstance = (ObjectReference)(targetClass.getValue(field));
0N/A
0N/A EventRequestManager mgr = vm().eventRequestManager();
0N/A methodEntryRequest = mgr.createMethodEntryRequest();
0N/A methodEntryRequest.addInstanceFilter(theInstance);
0N/A methodEntryRequest.enable();
0N/A
0N/A listenUntilVMDisconnect();
0N/A
0N/A if (!testFailed && methodCount < expectedMethods.length) {
0N/A failure("FAILED: Expected " + expectedMethods.length + " events, only got "
0N/A + methodCount);
0N/A }
0N/A if (!testFailed) {
0N/A println("InstanceFilter: passed");
0N/A } else {
0N/A throw new Exception("InstanceFilter: failed");
0N/A }
0N/A }
0N/A}