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 | |
| 29 | /** |
| 30 | * @author Robert Field |
| 31 | */ |
| 32 | |
| 33 | interface NsII { String m(Integer a, Integer b); } |
| 34 | |
| 35 | interface Nsiii { String m(int a, int b, int c); } |
| 36 | |
| 37 | interface Nsi { String m(int a); } |
| 38 | |
| 39 | interface NsaO { String m(Object[] a); } |
| 40 | |
| 41 | interface Nsai { String m(int[] a); } |
| 42 | |
| 43 | interface Nsvi { String m(int... va); } |
| 44 | |
| 45 | @Test |
| 46 | public class MethodReferenceTestVarArgsThis { |
| 47 | |
| 48 | // These should be processed as var args |
| 49 | |
| 50 | String xvI(Integer... vi) { |
| 51 | StringBuilder sb = new StringBuilder("xvI:"); |
| 52 | for (Integer i : vi) { |
| 53 | sb.append(i); |
| 54 | sb.append("-"); |
| 55 | } |
| 56 | return sb.toString(); |
| 57 | } |
| 58 | |
| 59 | String xIvI(Integer f, Integer... vi) { |
| 60 | StringBuilder sb = new StringBuilder("xIvI:"); |
| 61 | sb.append(f); |
| 62 | for (Integer i : vi) { |
| 63 | sb.append(i); |
| 64 | sb.append("-"); |
| 65 | } |
| 66 | return sb.toString(); |
| 67 | } |
| 68 | |
| 69 | String xvi(int... vi) { |
| 70 | int sum = 0; |
| 71 | for (int i : vi) { |
| 72 | sum += i; |
| 73 | } |
| 74 | return "xvi:" + sum; |
| 75 | } |
| 76 | |
| 77 | String xIvi(Integer f, int... vi) { |
| 78 | int sum = 0; |
| 79 | for (int i : vi) { |
| 80 | sum += i; |
| 81 | } |
| 82 | return "xIvi:(" + f + ")" + sum; |
| 83 | } |
| 84 | |
| 85 | String xvO(Object... vi) { |
| 86 | StringBuilder sb = new StringBuilder("xvO:"); |
| 87 | for (Object i : vi) { |
| 88 | if (i.getClass().isArray()) { |
| 89 | sb.append("["); |
| 90 | int len = Array.getLength(i); |
| 91 | for (int x = 0; x < len; ++x) { |
| 92 | sb.append(Array.get(i, x)); |
| 93 | sb.append(","); |
| 94 | } |
| 95 | sb.append("]"); |
| 96 | |
| 97 | } else { |
| 98 | sb.append(i); |
| 99 | } |
| 100 | sb.append("*"); |
| 101 | } |
| 102 | return sb.toString(); |
| 103 | } |
| 104 | |
| 105 | public void testVarArgsNsSuperclass() { |
| 106 | NsII q; |
| 107 | |
| 108 | q = this::xvO; |
| 109 | assertEquals(q.m(55,66), "xvO:55*66*"); |
| 110 | } |
| 111 | |
| 112 | public void testVarArgsNsArray() { |
| 113 | Nsai q; |
| 114 | |
| 115 | q = this::xvO; |
| 116 | assertEquals(q.m(new int[] { 55,66 } ), "xvO:[55,66,]*"); |
| 117 | } |
| 118 | |
| 119 | public void testVarArgsNsII() { |
| 120 | NsII q; |
| 121 | |
| 122 | q = this::xvI; |
| 123 | assertEquals(q.m(33,7), "xvI:33-7-"); |
| 124 | |
| 125 | q = this::xIvI; |
| 126 | assertEquals(q.m(50,40), "xIvI:5040-"); |
| 127 | |
| 128 | q = this::xvi; |
| 129 | assertEquals(q.m(100,23), "xvi:123"); |
| 130 | |
| 131 | q = this::xIvi; |
| 132 | assertEquals(q.m(9,21), "xIvi:(9)21"); |
| 133 | } |
| 134 | |
| 135 | public void testVarArgsNsiii() { |
| 136 | Nsiii q; |
| 137 | |
| 138 | q = this::xvI; |
| 139 | assertEquals(q.m(3, 2, 1), "xvI:3-2-1-"); |
| 140 | |
| 141 | q = this::xIvI; |
| 142 | assertEquals(q.m(888, 99, 2), "xIvI:88899-2-"); |
| 143 | |
| 144 | q = this::xvi; |
| 145 | assertEquals(q.m(900,80,7), "xvi:987"); |
| 146 | |
| 147 | q = this::xIvi; |
| 148 | assertEquals(q.m(333,27, 72), "xIvi:(333)99"); |
| 149 | } |
| 150 | |
| 151 | public void testVarArgsNsi() { |
| 152 | Nsi q; |
| 153 | |
| 154 | q = this::xvI; |
| 155 | assertEquals(q.m(3), "xvI:3-"); |
| 156 | |
| 157 | q = this::xIvI; |
| 158 | assertEquals(q.m(888), "xIvI:888"); |
| 159 | |
| 160 | q = this::xvi; |
| 161 | assertEquals(q.m(900), "xvi:900"); |
| 162 | |
| 163 | q = this::xIvi; |
| 164 | assertEquals(q.m(333), "xIvi:(333)0"); |
| 165 | } |
| 166 | |
| 167 | // These should NOT be processed as var args |
| 168 | |
| 169 | public void testVarArgsNsaO() { |
| 170 | NsaO q; |
| 171 | |
| 172 | q = this::xvO; |
| 173 | assertEquals(q.m(new String[] { "yo", "there", "dude" }), "xvO:yo*there*dude*"); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | } |