< prev index next >

test/java/lang/invoke/JavaDocExamplesTest.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 2013, 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  */


  40 import org.testng.annotations.*;
  41 
  42 /**
  43  * @author jrose
  44  */
  45 public class JavaDocExamplesTest {
  46     /** Wrapper for running the TestNG tests in this module.
  47      *  Put TestNG on the classpath!
  48      */
  49     public static void main(String... ignore) throws Throwable {
  50         new JavaDocExamplesTest().run();
  51     }
  52 
  53     public void run() throws Throwable {
  54         testMisc();
  55         testFindStatic();
  56         testFindConstructor();
  57         testFindVirtual();
  58         testFindSpecial();
  59         testPermuteArguments();

  60         testDropArguments();

  61         testFilterArguments();
  62         testFoldArguments();
  63         testFoldArguments2();
  64         testMethodHandlesSummary();
  65         testAsSpreader();
  66         testAsCollector();
  67         testAsVarargsCollector();
  68         testAsFixedArity();
  69         testAsTypeCornerCases();
  70         testMutableCallSite();
  71     }
  72     // How much output?
  73     static final Class<?> THIS_CLASS = JavaDocExamplesTest.class;
  74     static int verbosity = Integer.getInteger(THIS_CLASS.getSimpleName()+".verbosity", 0);
  75 
  76 
  77 {}
  78 private static final Lookup LOOKUP = lookup();
  79 // static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class,
  80 //     "concat", methodType(String.class, String.class));


 218 MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
 219 assert(twice.type().equals(intfn1));
 220 assert((int)twice.invokeExact(21) == 42);
 221             }}
 222         {{
 223 {} /// JAVADOC
 224 MethodHandle cat = lookup().findVirtual(String.class,
 225   "concat", methodType(String.class, String.class));
 226 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 227 MethodHandle d0 = dropArguments(cat, 0, String.class);
 228 assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
 229 MethodHandle d1 = dropArguments(cat, 1, String.class);
 230 assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
 231 MethodHandle d2 = dropArguments(cat, 2, String.class);
 232 assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
 233 MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
 234 assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
 235             }}
 236     }
 237 











 238     @Test public void testDropArguments() throws Throwable {
 239         {{
 240 {} /// JAVADOC
 241 MethodHandle cat = lookup().findVirtual(String.class,
 242   "concat", methodType(String.class, String.class));
 243 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 244 MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
 245 MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
 246 assertEquals(bigType, d0.type());
 247 assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
 248             }}
 249         {{
 250 {} /// JAVADOC
 251 MethodHandle cat = lookup().findVirtual(String.class,
 252   "concat", methodType(String.class, String.class));
 253 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 254 MethodHandle d0 = dropArguments(cat, 0, String.class);
 255 assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
 256 MethodHandle d1 = dropArguments(cat, 1, String.class);
 257 assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
 258 MethodHandle d2 = dropArguments(cat, 2, String.class);
 259 assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
 260 MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
 261 assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
 262             }}
 263     }
 264 


















 265     @Test public void testFilterArguments() throws Throwable {
 266         {{
 267 {} /// JAVADOC
 268 MethodHandle cat = lookup().findVirtual(String.class,
 269   "concat", methodType(String.class, String.class));
 270 MethodHandle upcase = lookup().findVirtual(String.class,
 271   "toUpperCase", methodType(String.class));
 272 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 273 MethodHandle f0 = filterArguments(cat, 0, upcase);
 274 assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
 275 MethodHandle f1 = filterArguments(cat, 1, upcase);
 276 assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
 277 MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
 278 assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
 279             }}
 280     }
 281 
 282     @Test public void testCollectArguments() throws Throwable {
 283         {{
 284 {} /// JAVADOC


   1 /*
   2  * Copyright (c) 2009, 2016, 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  */


  40 import org.testng.annotations.*;
  41 
  42 /**
  43  * @author jrose
  44  */
  45 public class JavaDocExamplesTest {
  46     /** Wrapper for running the TestNG tests in this module.
  47      *  Put TestNG on the classpath!
  48      */
  49     public static void main(String... ignore) throws Throwable {
  50         new JavaDocExamplesTest().run();
  51     }
  52 
  53     public void run() throws Throwable {
  54         testMisc();
  55         testFindStatic();
  56         testFindConstructor();
  57         testFindVirtual();
  58         testFindSpecial();
  59         testPermuteArguments();
  60         testZero();
  61         testDropArguments();
  62         testDropArgumentsToMatch();
  63         testFilterArguments();
  64         testFoldArguments();
  65         testFoldArguments2();
  66         testMethodHandlesSummary();
  67         testAsSpreader();
  68         testAsCollector();
  69         testAsVarargsCollector();
  70         testAsFixedArity();
  71         testAsTypeCornerCases();
  72         testMutableCallSite();
  73     }
  74     // How much output?
  75     static final Class<?> THIS_CLASS = JavaDocExamplesTest.class;
  76     static int verbosity = Integer.getInteger(THIS_CLASS.getSimpleName()+".verbosity", 0);
  77 
  78 
  79 {}
  80 private static final Lookup LOOKUP = lookup();
  81 // static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class,
  82 //     "concat", methodType(String.class, String.class));


 220 MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
 221 assert(twice.type().equals(intfn1));
 222 assert((int)twice.invokeExact(21) == 42);
 223             }}
 224         {{
 225 {} /// JAVADOC
 226 MethodHandle cat = lookup().findVirtual(String.class,
 227   "concat", methodType(String.class, String.class));
 228 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 229 MethodHandle d0 = dropArguments(cat, 0, String.class);
 230 assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
 231 MethodHandle d1 = dropArguments(cat, 1, String.class);
 232 assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
 233 MethodHandle d2 = dropArguments(cat, 2, String.class);
 234 assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
 235 MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
 236 assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
 237             }}
 238     }
 239 
 240 @Test public void testZero() throws Throwable {
 241         {{
 242 {} /// JAVADOC
 243 Class<?> type = Double.class;
 244 MethodHandle mh1 = MethodHandles.explicitCastArguments(MethodHandles.constant(Object.class, null), methodType(type));
 245 assertEquals("()Double", mh1.type().toString());
 246 MethodHandle mh2 = MethodHandles.empty(methodType(type));
 247 assertEquals("()Double", mh2.type().toString()); 
 248         }}
 249    }
 250 
 251     @Test public void testDropArguments() throws Throwable {
 252         {{
 253 {} /// JAVADOC
 254 MethodHandle cat = lookup().findVirtual(String.class,
 255   "concat", methodType(String.class, String.class));
 256 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 257 MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
 258 MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
 259 assertEquals(bigType, d0.type());
 260 assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
 261             }}
 262         {{
 263 {} /// JAVADOC
 264 MethodHandle cat = lookup().findVirtual(String.class,
 265   "concat", methodType(String.class, String.class));
 266 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 267 MethodHandle d0 = dropArguments(cat, 0, String.class);
 268 assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
 269 MethodHandle d1 = dropArguments(cat, 1, String.class);
 270 assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
 271 MethodHandle d2 = dropArguments(cat, 2, String.class);
 272 assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
 273 MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
 274 assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
 275             }}
 276     }
 277 
 278     @Test public void testDropArgumentsToMatch() throws Throwable {
 279         {{
 280 {} /// JAVADOC
 281 MethodHandle h0= constant(boolean.class, true);
 282 MethodHandle h1 = lookup().findVirtual(String.class, "concat", methodType(String.class, String.class));
 283 MethodType bigType = h1.type().insertParameterTypes(1, String.class, int.class);
 284 MethodHandle h2 = dropArguments(h1, 0, bigType.parameterList());
 285 if (h1.type().parameterCount() < h2.type().parameterCount()) {
 286     h1 = dropArgumentsToMatch(h1, 0, h2.type().parameterList(), 0);  // lengthen h1
 287 }
 288 else {
 289     h2 = dropArgumentsToMatch(h2, 0, h1.type().parameterList(), 0);    // lengthen h2
 290 }
 291 MethodHandle h3 = guardWithTest(h0, h1, h2);
 292 assertEquals("xy", h3.invoke("x", "y", 1, "a", "b", "c"));
 293         }}
 294     }
 295 
 296     @Test public void testFilterArguments() throws Throwable {
 297         {{
 298 {} /// JAVADOC
 299 MethodHandle cat = lookup().findVirtual(String.class,
 300   "concat", methodType(String.class, String.class));
 301 MethodHandle upcase = lookup().findVirtual(String.class,
 302   "toUpperCase", methodType(String.class));
 303 assertEquals("xy", (String) cat.invokeExact("x", "y"));
 304 MethodHandle f0 = filterArguments(cat, 0, upcase);
 305 assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
 306 MethodHandle f1 = filterArguments(cat, 1, upcase);
 307 assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
 308 MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
 309 assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
 310             }}
 311     }
 312 
 313     @Test public void testCollectArguments() throws Throwable {
 314         {{
 315 {} /// JAVADOC


< prev index next >