/* * Copyright (c) 2015, 2020, 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 8140450 8173898 * @summary Basic test for the StackWalker::walk method * @run testng Basic
*/
/** * For a stack of a given depth, it creates a StackWalker with an estimate. * Test walking different number of frames
*/
@Test(dataProvider = "stackDepths") publicstaticvoid test(int[] depth, int[] estimates) {
Basic test = new Basic(depth[0]); for (int estimate : estimates) {
test.walk(estimate);
}
}
if (!f.getMethodType().equals(type)) { thrownew RuntimeException("Expected: " + type
+ " got: " + f.getMethodType());
}
// verify descriptor returned by getDescriptor() before and after // getMethodType() is called if (!descriptor.equals(f.getDescriptor())) { thrownew RuntimeException("Mismatched: " + descriptor
+ " got: " + f.getDescriptor());
}
}
}
}
/* * Setup a stack builder with the expected stack depth * Walk the stack and count the frames.
*/ void walk(int estimate) { int limit = Math.min(depth, 16);
List<StackFrame> frames = new StackBuilder(depth, limit).build();
System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",
depth, estimate, limit, frames.size());
assertEquals(limit, frames.size());
}
staticclass ConstructorNewInstance { staticfinal StackWalker walker =
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
List<String> testFramesOrReflectionFrames; public ConstructorNewInstance() {
testFramesOrReflectionFrames = walker.walk(this::parse);
} public List<String> collectedFrames() { return testFramesOrReflectionFrames;
} publicboolean accept(StackFrame f) { // Frames whose class names don't contain "." // are our own test frames. These are the ones // we expect. // Frames whose class names contain ".reflect." // are reflection frames. None should be present, // since they are supposed to be filtered by // by StackWalker. If we find any, we want to fail. if (!f.getClassName().contains(".")
|| f.getClassName().contains(".reflect.")) {
System.out.println(" " + f); returntrue;
} // Filter out all other frames (in particular // those from the test framework) in order to // have predictable results. returnfalse;
} public String frame(StackFrame f) { return f.getClassName() + "::" + f.getMethodName();
}
List<String> parse(Stream<StackFrame> s) { return s.filter(this::accept)
.map(this::frame)
.collect(Collectors.toList());
} publicstatic ConstructorNewInstance create() throws Exception { return ConstructorNewInstance.class.getConstructor().newInstance();
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.