jdk/test/java/dyn/JavaDocExamples.java
author jrose
Wed, 08 Sep 2010 18:40:23 -0700
changeset 7052 963a5baf2ba3
permissions -rw-r--r--
6980096: JSR 292 reflective lookup should throw checked exceptions Summary: Make NoAccessException be a checked exception. Also remove JavaMethodHandle. Reviewed-by: twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     1
/*
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     4
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    10
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    15
 * accompanied this code).
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    16
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    20
 *
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    23
 * questions.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    24
 */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    25
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    26
/* @test
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    27
 * @summary example code used in javadoc for java.dyn API
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    28
 * @compile -XDallowTransitionalJSR292=no JavaDocExamples.java
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    29
 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles test.java.dyn.JavaDocExamples
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    30
 */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    31
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    32
/*
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    33
---- To run outside jtreg:
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    34
$ $JAVA7X_HOME/bin/javac -cp $JUNIT4_JAR -d /tmp/Classes \
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    35
   $DAVINCI/sources/jdk/test/java/dyn/JavaDocExamples.java
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    36
$ $JAVA7X_HOME/bin/java   -cp $JUNIT4_JAR:/tmp/Classes \
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    37
   -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles \
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    38
   -Dtest.java.dyn.JavaDocExamples.verbosity=1 \
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    39
     test.java.dyn.JavaDocExamples
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    40
----
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    41
*/
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    42
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    43
package test.java.dyn;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    44
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    45
import java.dyn.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    46
import static java.dyn.MethodHandles.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    47
import static java.dyn.MethodType.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    48
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    49
import java.lang.reflect.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    50
import java.util.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    51
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    52
import org.junit.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    53
import static org.junit.Assert.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    54
import static org.junit.Assume.*;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    55
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    56
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    57
/**
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    58
 * @author jrose
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    59
 */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    60
public class JavaDocExamples {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    61
    /** Wrapper for running the JUnit tests in this module.
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    62
     *  Put JUnit on the classpath!
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    63
     */
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    64
    public static void main(String... ignore) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    65
        org.junit.runner.JUnitCore.runClasses(JavaDocExamples.class);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    66
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    67
    // How much output?
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    68
    static int verbosity = Integer.getInteger("test.java.dyn.JavaDocExamples.verbosity", 0);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    69
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    70
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    71
static final private Lookup LOOKUP = lookup();
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    72
// static final private MethodHandle CONCAT_1 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    73
//     "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    74
// static final private MethodHandle HASHCODE_1 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    75
//     "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    76
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    77
// form required if NoAccessException is intercepted:
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    78
static final private MethodHandle CONCAT_2, HASHCODE_2;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    79
static {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    80
  try {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    81
    CONCAT_2 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    82
      "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    83
    HASHCODE_2 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    84
      "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    85
   } catch (NoAccessException ex) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    86
     throw new RuntimeException(ex);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    87
   }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    88
}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    89
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    90
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    91
    @Test public void testFindVirtual() throws Throwable {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    92
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    93
MethodHandle CONCAT_3 = LOOKUP.findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    94
  "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    95
MethodHandle HASHCODE_3 = LOOKUP.findVirtual(Object.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    96
  "hashCode", methodType(int.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    97
//assertEquals("xy", (String) CONCAT_1.invokeExact("x", "y"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    98
assertEquals("xy", (String) CONCAT_2.<String>invokeExact("x", "y"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
    99
assertEquals("xy", (String) CONCAT_3.<String>invokeExact("x", "y"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   100
//assertEquals("xy".hashCode(), (int) HASHCODE_1.<int>invokeExact((Object)"xy"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   101
assertEquals("xy".hashCode(), (int) HASHCODE_2.<int>invokeExact((Object)"xy"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   102
assertEquals("xy".hashCode(), (int) HASHCODE_3.<int>invokeExact((Object)"xy"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   103
{}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   104
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   105
    @Test public void testDropArguments() throws Throwable {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   106
        {{
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   107
{} /// JAVADOC
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   108
MethodHandle cat = lookup().findVirtual(String.class,
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   109
  "concat", methodType(String.class, String.class));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   110
cat = cat.asType(methodType(Object.class, String.class, String.class)); /*(String)*/
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   111
assertEquals("xy", /*(String)*/ cat.invokeExact("x", "y"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   112
MethodHandle d0 = dropArguments(cat, 0, String.class);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   113
assertEquals("yz", /*(String)*/ d0.invokeExact("x", "y", "z"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   114
MethodHandle d1 = dropArguments(cat, 1, String.class);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   115
assertEquals("xz", /*(String)*/ d1.invokeExact("x", "y", "z"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   116
MethodHandle d2 = dropArguments(cat, 2, String.class);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   117
assertEquals("xy", /*(String)*/ d2.invokeExact("x", "y", "z"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   118
MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   119
assertEquals("xz", /*(String)*/ d12.invokeExact("x", 12, true, "z"));
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   120
            }}
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   121
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   122
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   123
    static void assertEquals(Object exp, Object act) {
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   124
        if (verbosity > 0)
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   125
            System.out.println("result: "+act);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   126
        Assert.assertEquals(exp, act);
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   127
    }
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents:
diff changeset
   128
}