Path: blob/master/test/jdk/java/lang/StackWalker/WalkFunction.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 802096826* @summary Sanity test for Function wildcard signature27* @run main WalkFunction28*/2930import java.lang.StackWalker.StackFrame;31import java.util.Optional;32import java.util.function.Function;33import java.util.stream.Stream;3435public class WalkFunction {36private static final StackWalker walker = StackWalker.getInstance();3738public static void main(String... args) throws Exception {39testFunctions();40testWildcards();41walker.walk(counter());42walker.walk(wildCounter());43}4445private static void testFunctions() {46walker.walk(Stream::count);4748try {49walker.walk(null);50throw new RuntimeException("NPE expected");51} catch (NullPointerException e) {}5253Optional<StackFrame> result = walker.walk(WalkFunction::reduce);54if (!result.get().getClassName().equals(WalkFunction.class.getName())) {55throw new RuntimeException(result.get() + " expected: " + WalkFunction.class.getName());56}57}5859static Optional<StackFrame> reduce(Stream<StackFrame> stream) {60return stream.reduce((r,f) -> r.getClassName().compareTo(f.getClassName()) > 0 ? f : r);61}6263private static void testWildcards() {64Function<? super Stream<? extends StackFrame>, Void> f1 = WalkFunction::function;65Function<? super Stream<? super StackFrame>, Void> f2 = WalkFunction::function;66Function<? super Stream<StackFrame>, Void> f3 = WalkFunction::function;67Function<Stream<? extends StackFrame>, Void> f4 = WalkFunction::function;68Function<Stream<? super StackFrame>, Void> f5 = WalkFunction::function;69Function<Stream<StackFrame>, Void> f6 = WalkFunction::function;70walker.walk(f1);71walker.walk(f2);72walker.walk(f3);73walker.walk(f4);74walker.walk(f5);75walker.walk(f6);76}7778private static Void function(Stream<?> s) {79return null;80}8182private static Function<Stream<?>, Long> wildCounter() {83return Stream::count;84}85private static <T> Function<Stream<T>, Long> counter() {86return Stream::count;87}88}899091