briangoetz | ef42f9a | 2013-05-06 11:43:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, 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 | import org.testng.annotations.Test; |
| 25 | import java.lang.reflect.Array; |
| 26 | |
| 27 | import static org.testng.Assert.assertEquals; |
| 28 | import static org.testng.Assert.assertTrue; |
| 29 | import static org.testng.Assert.fail; |
| 30 | |
| 31 | /** |
| 32 | * Method references and raw types. |
| 33 | * @author Robert Field |
| 34 | */ |
| 35 | |
| 36 | @Test |
| 37 | @SuppressWarnings({"rawtypes", "unchecked"}) |
| 38 | public class MethodReferenceTestFDCCE { |
| 39 | |
| 40 | static void assertCCE(Throwable t) { |
| 41 | assertEquals(t.getClass().getName(), "java.lang.ClassCastException"); |
| 42 | } |
| 43 | |
| 44 | interface Pred<T> { boolean accept(T x); } |
| 45 | |
| 46 | interface Ps { boolean accept(short x); } |
| 47 | |
| 48 | interface Oo { Object too(int x); } |
| 49 | |
| 50 | interface Reto<T> { T m(); } |
| 51 | |
| 52 | class A {} |
| 53 | class B extends A {} |
| 54 | |
| 55 | static boolean isMinor(int x) { |
| 56 | return x < 18; |
| 57 | } |
| 58 | |
| 59 | static boolean tst(A x) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | static Object otst(Object x) { |
| 64 | return x; |
| 65 | } |
| 66 | |
| 67 | static boolean stst(Short x) { |
| 68 | return x < 18; |
| 69 | } |
| 70 | |
| 71 | static short ritst() { |
| 72 | return 123; |
| 73 | } |
| 74 | |
| 75 | public void testMethodReferenceFDPrim1() { |
| 76 | Pred<Byte> p = MethodReferenceTestFDCCE::isMinor; |
| 77 | Pred p2 = p; |
| 78 | assertTrue(p2.accept((Byte)(byte)15)); |
| 79 | } |
| 80 | |
| 81 | public void testMethodReferenceFDPrim2() { |
| 82 | Pred<Byte> p = MethodReferenceTestFDCCE::isMinor; |
| 83 | Pred p2 = p; |
| 84 | assertTrue(p2.accept((byte)15)); |
| 85 | } |
| 86 | |
| 87 | public void testMethodReferenceFDPrimICCE() { |
| 88 | Pred<Byte> p = MethodReferenceTestFDCCE::isMinor; |
| 89 | Pred p2 = p; |
| 90 | try { |
| 91 | p2.accept(15); // should throw CCE |
| 92 | fail("Exception should have been thrown"); |
| 93 | } catch (Throwable t) { |
| 94 | assertCCE(t); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void testMethodReferenceFDPrimOCCE() { |
| 99 | Pred<Byte> p = MethodReferenceTestFDCCE::isMinor; |
| 100 | Pred p2 = p; |
| 101 | try { |
| 102 | p2.accept(new Object()); // should throw CCE |
| 103 | fail("Exception should have been thrown"); |
| 104 | } catch (Throwable t) { |
| 105 | assertCCE(t); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public void testMethodReferenceFDRef() { |
| 110 | Pred<B> p = MethodReferenceTestFDCCE::tst; |
| 111 | Pred p2 = p; |
| 112 | assertTrue(p2.accept(new B())); |
| 113 | } |
| 114 | |
| 115 | public void testMethodReferenceFDRefCCE() { |
| 116 | Pred<B> p = MethodReferenceTestFDCCE::tst; |
| 117 | Pred p2 = p; |
| 118 | try { |
| 119 | p2.accept(new A()); // should throw CCE |
| 120 | fail("Exception should have been thrown"); |
| 121 | } catch (Throwable t) { |
| 122 | assertCCE(t); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public void testMethodReferenceFDPrimPrim() { |
| 127 | Ps p = MethodReferenceTestFDCCE::isMinor; |
| 128 | assertTrue(p.accept((byte)15)); |
| 129 | } |
| 130 | |
| 131 | public void testMethodReferenceFDPrimBoxed() { |
| 132 | Ps p = MethodReferenceTestFDCCE::stst; |
| 133 | assertTrue(p.accept((byte)15)); |
| 134 | } |
| 135 | |
| 136 | public void testMethodReferenceFDPrimRef() { |
| 137 | Oo p = MethodReferenceTestFDCCE::otst; |
| 138 | assertEquals(p.too(15).getClass().getName(), "java.lang.Integer"); |
| 139 | } |
| 140 | |
| 141 | public void testMethodReferenceFDRet1() { |
| 142 | Reto<Short> p = MethodReferenceTestFDCCE::ritst; |
| 143 | assertEquals(p.m(), (Short)(short)123); |
| 144 | } |
| 145 | |
| 146 | } |