|
Java example source code file (ReferrersTest.java)
The ReferrersTest.java Java example source code
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 5089849
* @summary Add support for backtracking reference graph.
* @author jjh
*
* @run build TestScaffold VMConnection TargetListener TargetAdapter
* @run compile -g ReferrersTest.java
* @run main ReferrersTest
*/
/*
* To run this test do this:
* runregress -no ReferrersTest <cmd line options>
*
* where <cmd line options> are the options to be used to
* launch the debuggee, with the classname prefixed with @@.
* For example, this would run java2d demo as the debuggee:
* runregress -no ReferrersTest -classpath
* $jdkDir/demo/jfc/Java2D/Java2Demo.jar \
* -client @@java2d.Java2Demo
*
* In this mode, the specified debuggee is launched in debug mode,
* and the debugger waits for a keystroke before connecting to the debuggee.
*
* If <cmd line options> is not specified, then the ReferrersTarg class below
* is run as the debuggee.
*/
import com.sun.jdi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;
import java.util.*;
class ReferrersFiller {
// This many instances of this are created.
static int FILLER_COUNT = 20000;
static ReferrersFiller[] lotsAndLots = new ReferrersFiller[
ReferrersFiller.FILLER_COUNT];
int xx;
ReferrersFiller(int p1) {
xx = p1;
}
}
class ReferrersTarg {
// This many instances + 1 of this class are created.
static int TARG_COUNT = 10;
static ReferrersTarg theReferrersTarg;
static ReferrersTarg[] allReferrersTargs;
// Each instance will point to the theReferrersTarg
ReferrersTarg oneReferrersTarg;
public static void bkpt() {
}
public static void main(String[] args) {
System.out.println("Howdy!");
for (int ii = 0; ii < ReferrersFiller.lotsAndLots.length; ii++) {
ReferrersFiller.lotsAndLots[ii] = new ReferrersFiller(ii);
}
theReferrersTarg = new ReferrersTarg();
allReferrersTargs = new ReferrersTarg[ReferrersTarg.TARG_COUNT];
for (int ii = 0; ii < ReferrersTarg.TARG_COUNT; ii++) {
allReferrersTargs[ii] = new ReferrersTarg();
allReferrersTargs[ii].oneReferrersTarg = theReferrersTarg;
}
bkpt();
System.out.println("Goodbye from ReferrersTarg!");
}
}
/********** test program **********/
public class ReferrersTest extends TestScaffold {
static String targetName = "ReferrersTarg";
ReferenceType targetClass;
ThreadReference mainThread;
ReferrersTest(String args[]) {
super(args);
}
public static void main(String[] args) throws Exception {
/*
* If args contains @@xxxx, then that is the
* name of the class we are to run.
*/
for (int ii = 0; ii < args.length; ii ++) {
if (args[ii].startsWith("@@")) {
targetName = args[ii] = args[ii].substring(2);
break;
}
}
new ReferrersTest(args).startTests();
}
/*
* Used to sort a list of ReferenceTypes by
* instance count.
*/
class ToSort implements Comparable<ToSort> {
long count;
ReferenceType rt;
public ToSort(long count, ReferenceType rt) {
this.count = count;
this.rt = rt;
}
public int compareTo(ToSort obj) {
if (count < obj.count) return -1;
if (count == obj.count) return 0;
return 1;
}
}
protected void runTests() throws Exception {
/*
* Get to the top of main()
* to determine targetClass and mainThread
*/
int CUT_OFF = 1000;
BreakpointEvent bpe;
bpe = startToMain(targetName);
targetClass = bpe.location().declaringType();
mainThread = bpe.thread();
if (targetName.equals("ReferrersTarg")) {
resumeTo("ReferrersTarg", "bkpt", "()V");
} else {
// Let debuggee run for awhile to get classes loaded
vm().resume();
try {
System.err.println("Press <enter> to continue");
System.in.read();
System.err.println("running...");
} catch(Exception e) {
}
vm().suspend();
}
// Get all classes.
long start = System.currentTimeMillis();
List<ReferenceType> allClasses = vm().allClasses();
long end = System.currentTimeMillis();
System.out.println( allClasses.size() +
" classes from vm.allClasses() took " +
(end - start) + " ms");
long[] counts;
// Test for NPE
{
boolean pass = false;
try {
counts = vm().instanceCounts(null);
} catch (NullPointerException ee) {
pass = true;
}
if (!pass) {
failure("failure: NullPointerException not thrown on instanceCounts(null)");
}
}
// Test for 0 length array
{
List<ReferenceType>someClasses = new ArrayList(2);
counts = vm().instanceCounts(someClasses);
if (counts.length != 0) {
failure("failure: instanceCounts with a zero length array fails: " +
counts.length);
}
}
// Test various values of maxInstances
if (targetClass.name().equals("ReferrersTarg")) {
List<ObjectReference> noInstances = targetClass.instances(0);
if (noInstances.size() != ReferrersTarg.TARG_COUNT + 1) {
failure("failure: instances(0): " + noInstances.size() + ", for " + targetClass);
}
noInstances = targetClass.instances(1);
if (noInstances.size() != 1) {
failure("failure: instances(1): " + noInstances.size() + ", for " + targetClass);
}
boolean pass = false;
try {
noInstances = targetClass.instances(-1);
} catch (IllegalArgumentException ee) {
pass = true;
}
if (!pass) {
failure("failure: instances(-1) did not get an exception");
}
}
// Instance counts for all classes
start = System.currentTimeMillis();
counts = vm().instanceCounts(allClasses);
end = System.currentTimeMillis();
if (counts.length == 0) {
System.out.println("failure: No instances found");
throw new Exception("ReferrersTest: failed");
}
// Create a list of ReferenceTypes sorted by instance count
int size = 0;
List<ToSort> sorted = new ArrayList(allClasses.size());
for (int ii = 0; ii < allClasses.size(); ii++) {
size += counts[ii];
ToSort tos = new ToSort(counts[ii], allClasses.get(ii));
sorted.add(tos);
}
System.out.println("instance counts for " + counts.length +
" classes got " + size + " instances and took " +
(end - start) + " ms");
boolean gotReferrersFiller = false;
boolean gotReferrersTarg = false;
Collections.sort(sorted);
for (int ii = sorted.size() - 1; ii >= 0 ; ii--) {
ToSort xxx = sorted.get(ii);
if (xxx.rt.name().equals("ReferrersFiller") &&
xxx.count == ReferrersFiller.FILLER_COUNT) {
gotReferrersFiller = true;
}
if (xxx.rt.name().equals("ReferrersTarg") &&
xxx.count == ReferrersTarg.TARG_COUNT + 1) {
gotReferrersTarg = true;
}
}
if (!gotReferrersFiller) {
failure("failure: Expected " + ReferrersFiller.FILLER_COUNT +
" instances of ReferrersFiller");
}
if (!gotReferrersTarg) {
failure("failure: Expected " + (ReferrersTarg.TARG_COUNT + 1) +
" instances of ReferrersTarg");
}
List<List
Other Java examples (source code examples)Here is a short list of links related to this Java ReferrersTest.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.