JavaDocExamplesTest.java 8.8 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
27
 * @summary example code used in javadoc for java.lang.invoke API
28 29
 * @compile JavaDocExamplesTest.java
 * @run junit/othervm test.java.lang.invoke.JavaDocExamplesTest
30 31 32 33 34
 */

/*
---- To run outside jtreg:
$ $JAVA7X_HOME/bin/javac -cp $JUNIT4_JAR -d /tmp/Classes \
35
   $DAVINCI/sources/jdk/test/java/lang/invoke/JavaDocExamplesTest.java
36
$ $JAVA7X_HOME/bin/java   -cp $JUNIT4_JAR:/tmp/Classes \
37 38
   -Dtest.java.lang.invoke.JavaDocExamplesTest.verbosity=1 \
     test.java.lang.invoke.JavaDocExamplesTest
39 40 41
----
*/

42
package test.java.lang.invoke;
43

44 45 46
import java.lang.invoke.*;
import static java.lang.invoke.MethodHandles.*;
import static java.lang.invoke.MethodType.*;
47 48 49 50 51 52 53 54 55 56 57 58

import java.lang.reflect.*;
import java.util.*;

import org.junit.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;


/**
 * @author jrose
 */
59
public class JavaDocExamplesTest {
60 61 62 63
    /** Wrapper for running the JUnit tests in this module.
     *  Put JUnit on the classpath!
     */
    public static void main(String... ignore) {
64
        org.junit.runner.JUnitCore.runClasses(JavaDocExamplesTest.class);
65 66
    }
    // How much output?
67
    static int verbosity = Integer.getInteger("test.java.lang.invoke.JavaDocExamplesTest.verbosity", 0);
68 69 70 71 72 73 74 75

{}
static final private Lookup LOOKUP = lookup();
// static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class,
//     "concat", methodType(String.class, String.class));
// static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class,
//     "hashCode", methodType(int.class));

76
// form required if ReflectiveOperationException is intercepted:
77 78 79 80 81 82 83
static final private MethodHandle CONCAT_2, HASHCODE_2;
static {
  try {
    CONCAT_2 = LOOKUP.findVirtual(String.class,
      "concat", methodType(String.class, String.class));
    HASHCODE_2 = LOOKUP.findVirtual(Object.class,
      "hashCode", methodType(int.class));
84
   } catch (ReflectiveOperationException ex) {
85 86 87 88 89 90 91 92 93 94 95 96
     throw new RuntimeException(ex);
   }
}
{}

    @Test public void testFindVirtual() throws Throwable {
{}
MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
  "concat", methodType(String.class, String.class));
MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
  "hashCode", methodType(int.class));
//assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y"));
97 98 99 100 101
assertEquals("xy", (String) CONCAT_2.invokeExact("x", "y"));
assertEquals("xy", (String) CONCAT_3.invokeExact("x", "y"));
//assertEquals("xy".hashCode(), (int) HASHCODE_1.invokeExact((Object)"xy"));
assertEquals("xy".hashCode(), (int) HASHCODE_2.invokeExact((Object)"xy"));
assertEquals("xy".hashCode(), (int) HASHCODE_3.invokeExact((Object)"xy"));
102 103 104 105 106 107 108
{}
    }
    @Test public void testDropArguments() throws Throwable {
        {{
{} /// JAVADOC
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
109
assertEquals("xy", (String) cat.invokeExact("x", "y"));
110 111 112 113 114 115 116 117 118 119
MethodType bigType = cat.type().insertParameterTypes(0, int.class, String.class);
MethodHandle d0 = dropArguments(cat, 0, bigType.parameterList().subList(0,2));
assertEquals(bigType, d0.type());
assertEquals("yz", (String) d0.invokeExact(123, "x", "y", "z"));
            }}
        {{
{} /// JAVADOC
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
assertEquals("xy", (String) cat.invokeExact("x", "y"));
120
MethodHandle d0 = dropArguments(cat, 0, String.class);
121
assertEquals("yz", (String) d0.invokeExact("x", "y", "z"));
122
MethodHandle d1 = dropArguments(cat, 1, String.class);
123
assertEquals("xz", (String) d1.invokeExact("x", "y", "z"));
124
MethodHandle d2 = dropArguments(cat, 2, String.class);
125
assertEquals("xy", (String) d2.invokeExact("x", "y", "z"));
126
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
127
assertEquals("xz", (String) d12.invokeExact("x", 12, true, "z"));
128 129 130
            }}
    }

131 132 133 134 135 136 137
    @Test public void testFilterArguments() throws Throwable {
        {{
{} /// JAVADOC
MethodHandle cat = lookup().findVirtual(String.class,
  "concat", methodType(String.class, String.class));
MethodHandle upcase = lookup().findVirtual(String.class,
  "toUpperCase", methodType(String.class));
138
assertEquals("xy", (String) cat.invokeExact("x", "y"));
139
MethodHandle f0 = filterArguments(cat, 0, upcase);
140
assertEquals("Xy", (String) f0.invokeExact("x", "y")); // Xy
141
MethodHandle f1 = filterArguments(cat, 1, upcase);
142
assertEquals("xY", (String) f1.invokeExact("x", "y")); // xY
143
MethodHandle f2 = filterArguments(cat, 0, upcase, upcase);
144
assertEquals("XY", (String) f2.invokeExact("x", "y")); // XY
145 146 147
            }}
    }

148 149 150 151 152
    static void assertEquals(Object exp, Object act) {
        if (verbosity > 0)
            System.out.println("result: "+act);
        Assert.assertEquals(exp, act);
    }
153

154
    @Test public void testMethodHandlesSummary() throws Throwable {
155 156
        {{
{} /// JAVADOC
157 158 159 160 161 162 163
Object x, y; String s; int i;
MethodType mt; MethodHandle mh;
MethodHandles.Lookup lookup = MethodHandles.lookup();
// mt is (char,char)String
mt = MethodType.methodType(String.class, char.class, char.class);
mh = lookup.findVirtual(String.class, "replace", mt);
s = (String) mh.invokeExact("daddy",'d','n');
164
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String;
165 166 167 168 169 170 171 172
assert(s.equals("nanny"));
// weakly typed invocation (using MHs.invoke)
s = (String) mh.invokeWithArguments("sappy", 'p', 'v');
assert(s.equals("savvy"));
// mt is (Object[])List
mt = MethodType.methodType(java.util.List.class, Object[].class);
mh = lookup.findStatic(java.util.Arrays.class, "asList", mt);
assert(mh.isVarargsCollector());
173 174
x = mh.invoke("one", "two");
// invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
175 176 177
assert(x.equals(java.util.Arrays.asList("one","two")));
// mt is (Object,Object,Object)Object
mt = MethodType.genericMethodType(3);
178
mh = mh.asType(mt);
179
x = mh.invokeExact((Object)1, (Object)2, (Object)3);
180
// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
181 182 183 184 185
assert(x.equals(java.util.Arrays.asList(1,2,3)));
// mt is { => int}
mt = MethodType.methodType(int.class);
mh = lookup.findVirtual(java.util.List.class, "size", mt);
i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3));
186
// invokeExact(Ljava/util/List;)I
187 188 189 190
assert(i == 3);
mt = MethodType.methodType(void.class, String.class);
mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt);
mh.invokeExact(System.out, "Hello, world.");
191
// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V
192
{}
193 194 195
            }}
    }

196 197 198 199 200 201
    @Test public void testAsVarargsCollector() throws Throwable {
        {{
{} /// JAVADOC
MethodHandle asList = publicLookup()
  .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
  .asVarargsCollector(Object[].class);
202 203 204
assertEquals("[]", asList.invoke().toString());
assertEquals("[1]", asList.invoke(1).toString());
assertEquals("[two, too]", asList.invoke("two", "too").toString());
205
Object[] argv = { "three", "thee", "tee" };
206 207
assertEquals("[three, thee, tee]", asList.invoke(argv).toString());
List ls = (List) asList.invoke((Object)argv);
208 209 210 211
assertEquals(1, ls.size());
assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0)));
            }}
    }
212

213 214 215 216 217 218
    @Test public void testVarargsCollectorSuppression() throws Throwable {
        {{
{} /// JAVADOC
MethodHandle vamh = publicLookup()
  .findStatic(Arrays.class, "asList", methodType(List.class, Object[].class))
  .asVarargsCollector(Object[].class);
219
MethodHandle mh = MethodHandles.exactInvoker(vamh.type()).bindTo(vamh);
220
assert(vamh.type().equals(mh.type()));
221
assertEquals("[1, 2, 3]", vamh.invoke(1,2,3).toString());
222
boolean failed = false;
223
try { mh.invoke(1,2,3); }
224 225 226 227 228
catch (WrongMethodTypeException ex) { failed = true; }
assert(failed);
{}
            }}
    }
229
}