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