1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8020968 27 * @summary Sanity test for Function wildcard signature 28 * @run main WalkFunction 29 */ 30 31 import java.lang.StackWalker.StackFrame; 32 import java.util.Optional; 33 import java.util.function.Function; 34 import java.util.stream.Stream; 35 36 public class WalkFunction { 37 private static final StackWalker walker = StackWalker.create(); 38 39 public static void main(String... args) throws Exception { 40 testFunctions(); 41 testWildcards(); 42 walker.walk(counter()); 43 walker.walk(wildCounter()); 44 } 45 46 private static void testFunctions() { 47 walker.walk(Stream::count); 48 49 try { 50 walker.walk(null); 51 throw new RuntimeException("NPE expected"); 52 } catch (NullPointerException e) {} 53 54 Optional<StackFrame> result = walker.walk(WalkFunction::reduce); 55 if (!result.get().getClassName().equals(WalkFunction.class.getName())) { 56 throw new RuntimeException(result.get() + " expected: " + WalkFunction.class.getName()); 57 } 58 } 59 60 static Optional<StackFrame> reduce(Stream<StackFrame> stream) { 61 return stream.reduce((r,f) -> r.getClassName().compareTo(f.getClassName()) > 0 ? f : r); 62 } 63 64 private static void testWildcards() { 65 Function<? super Stream<? extends StackFrame>, Void> f1 = WalkFunction::function; 66 Function<? super Stream<? super StackFrame>, Void> f2 = WalkFunction::function; 67 Function<? super Stream<StackFrame>, Void> f3 = WalkFunction::function; 68 Function<Stream<? extends StackFrame>, Void> f4 = WalkFunction::function; 69 Function<Stream<? super StackFrame>, Void> f5 = WalkFunction::function; 70 Function<Stream<StackFrame>, Void> f6 = WalkFunction::function; 71 walker.walk(f1); 72 walker.walk(f2); 73 walker.walk(f3); 74 walker.walk(f4); 75 walker.walk(f5); 76 walker.walk(f6); 77 } 78 79 private static Void function(Stream<?> s) { 80 return null; 81 } 82 83 private static Function<Stream<?>, Long> wildCounter() { 84 return Stream::count; 85 } 86 private static <T> Function<Stream<T>, Long> counter() { 87 return Stream::count; 88 } 89 }