author | jrose |
Thu, 26 May 2011 17:37:36 -0700 | |
changeset 9752 | 88ab34b6da6d |
child 10078 | 944d876457df |
permissions | -rw-r--r-- |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
1 |
/* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
2 |
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
4 |
* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
10 |
* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
15 |
* accompanied this code). |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
16 |
* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
20 |
* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
23 |
* questions. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
24 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
25 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
26 |
package java.lang.invoke; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
27 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
28 |
import java.lang.reflect.*; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
29 |
import sun.invoke.WrapperInstance; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
30 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
31 |
/** |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
32 |
* This class consists exclusively of static methods that help adapt |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
33 |
* method handles to other JVM types, such as interfaces. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
34 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
35 |
public class MethodHandleProxies { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
36 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
37 |
private MethodHandleProxies() { } // do not instantiate |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
38 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
39 |
/** |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
40 |
* Produces an instance of the given single-method interface which redirects |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
41 |
* its calls to the given method handle. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
42 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
43 |
* A single-method interface is an interface which declares a uniquely named method. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
44 |
* When determining the uniquely named method of a single-method interface, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
45 |
* the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode}) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
46 |
* are disregarded. For example, {@link java.util.Comparator} is a single-method interface, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
47 |
* even though it re-declares the {@code Object.equals} method. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
48 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
49 |
* The interface must be public. No additional access checks are performed. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
50 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
51 |
* The resulting instance of the required type will respond to |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
52 |
* invocation of the type's uniquely named method by calling |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
53 |
* the given target on the incoming arguments, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
54 |
* and returning or throwing whatever the target |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
55 |
* returns or throws. The invocation will be as if by |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
56 |
* {@code target.invoke}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
57 |
* The target's type will be checked before the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
58 |
* instance is created, as if by a call to {@code asType}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
59 |
* which may result in a {@code WrongMethodTypeException}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
60 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
61 |
* The uniquely named method is allowed to be multiply declared, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
62 |
* with distinct type descriptors. (E.g., it can be overloaded, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
63 |
* or can possess bridge methods.) All such declarations are |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
64 |
* connected directly to the target method handle. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
65 |
* Argument and return types are adjusted by {@code asType} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
66 |
* for each individual declaration. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
67 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
68 |
* The wrapper instance will implement the requested interface |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
69 |
* and its super-types, but no other single-method interfaces. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
70 |
* This means that the instance will not unexpectedly |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
71 |
* pass an {@code instanceof} test for any unrequested type. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
72 |
* <p style="font-size:smaller;"> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
73 |
* <em>Implementation Note:</em> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
74 |
* Therefore, each instance must implement a unique single-method interface. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
75 |
* Implementations may not bundle together |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
76 |
* multiple single-method interfaces onto single implementation classes |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
77 |
* in the style of {@link java.awt.AWTEventMulticaster}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
78 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
79 |
* The method handle may throw an <em>undeclared exception</em>, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
80 |
* which means any checked exception (or other checked throwable) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
81 |
* not declared by the requested type's single abstract method. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
82 |
* If this happens, the throwable will be wrapped in an instance of |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
83 |
* {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
84 |
* and thrown in that wrapped form. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
85 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
86 |
* Like {@link java.lang.Integer#valueOf Integer.valueOf}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
87 |
* {@code asInterfaceInstance} is a factory method whose results are defined |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
88 |
* by their behavior. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
89 |
* It is not guaranteed to return a new instance for every call. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
90 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
91 |
* Because of the possibility of {@linkplain java.lang.reflect.Method#isBridge bridge methods} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
92 |
* and other corner cases, the interface may also have several abstract methods |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
93 |
* with the same name but having distinct descriptors (types of returns and parameters). |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
94 |
* In this case, all the methods are bound in common to the one given target. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
95 |
* The type check and effective {@code asType} conversion is applied to each |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
96 |
* method type descriptor, and all abstract methods are bound to the target in common. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
97 |
* Beyond this type check, no further checks are made to determine that the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
98 |
* abstract methods are related in any way. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
99 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
100 |
* Future versions of this API may accept additional types, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
101 |
* such as abstract classes with single abstract methods. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
102 |
* Future versions of this API may also equip wrapper instances |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
103 |
* with one or more additional public "marker" interfaces. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
104 |
* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
105 |
* @param target the method handle to invoke from the wrapper |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
106 |
* @param intfc the desired type of the wrapper, a single-method interface |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
107 |
* @return a correctly-typed wrapper for the given target |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
108 |
* @throws NullPointerException if either argument is null |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
109 |
* @throws IllegalArgumentException if the {@code intfc} is not a |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
110 |
* valid argument to this method |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
111 |
* @throws WrongMethodTypeException if the target cannot |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
112 |
* be converted to the type required by the requested interface |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
113 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
114 |
// Other notes to implementors: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
115 |
// <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
116 |
// No stable mapping is promised between the single-method interface and |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
117 |
// the implementation class C. Over time, several implementation |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
118 |
// classes might be used for the same type. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
119 |
// <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
120 |
// If the implementation is able |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
121 |
// to prove that a wrapper of the required type |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
122 |
// has already been created for a given |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
123 |
// method handle, or for another method handle with the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
124 |
// same behavior, the implementation may return that wrapper in place of |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
125 |
// a new wrapper. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
126 |
// <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
127 |
// This method is designed to apply to common use cases |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
128 |
// where a single method handle must interoperate with |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
129 |
// an interface that implements a function-like |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
130 |
// API. Additional variations, such as single-abstract-method classes with |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
131 |
// private constructors, or interfaces with multiple but related |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
132 |
// entry points, must be covered by hand-written or automatically |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
133 |
// generated adapter classes. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
134 |
// |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
135 |
public static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
136 |
<T> T asInterfaceInstance(final Class<T> intfc, final MethodHandle target) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
137 |
// POC implementation only; violates the above contract several ways |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
138 |
final Method sm = getSingleMethod(intfc); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
139 |
if (sm == null) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
140 |
throw new IllegalArgumentException("not a single-method interface: "+intfc.getName()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
141 |
MethodType smMT = MethodType.methodType(sm.getReturnType(), sm.getParameterTypes()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
142 |
MethodHandle checkTarget = target.asType(smMT); // make throw WMT |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
143 |
checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
144 |
final MethodHandle vaTarget = checkTarget.asSpreader(Object[].class, smMT.parameterCount()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
145 |
return intfc.cast(Proxy.newProxyInstance( |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
146 |
intfc.getClassLoader(), |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
147 |
new Class[]{ intfc, WrapperInstance.class }, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
148 |
new InvocationHandler() { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
149 |
private Object getArg(String name) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
150 |
if ((Object)name == "getWrapperInstanceTarget") return target; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
151 |
if ((Object)name == "getWrapperInstanceType") return intfc; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
152 |
throw new AssertionError(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
153 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
154 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
155 |
if (method.getDeclaringClass() == WrapperInstance.class) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
156 |
return getArg(method.getName()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
157 |
if (method.equals(sm)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
158 |
return vaTarget.invokeExact(args); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
159 |
if (isObjectMethod(method)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
160 |
return callObjectMethod(this, method, args); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
161 |
throw new InternalError(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
162 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
163 |
})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
164 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
165 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
166 |
/** |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
167 |
* Determines if the given object was produced by a call to {@link #asInterfaceInstance asInterfaceInstance}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
168 |
* @param x any reference |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
169 |
* @return true if the reference is not null and points to an object produced by {@code asInterfaceInstance} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
170 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
171 |
public static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
172 |
boolean isWrapperInstance(Object x) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
173 |
return x instanceof WrapperInstance; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
174 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
175 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
176 |
private static WrapperInstance asWrapperInstance(Object x) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
177 |
try { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
178 |
if (x != null) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
179 |
return (WrapperInstance) x; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
180 |
} catch (ClassCastException ex) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
181 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
182 |
throw new IllegalArgumentException("not a wrapper instance"); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
183 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
184 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
185 |
/** |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
186 |
* Produces or recovers a target method handle which is behaviorally |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
187 |
* equivalent to the unique method of this wrapper instance. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
188 |
* The object {@code x} must have been produced by a call to {@link #asInterfaceInstance asInterfaceInstance}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
189 |
* This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
190 |
* @param x any reference |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
191 |
* @return a method handle implementing the unique method |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
192 |
* @throws IllegalArgumentException if the reference x is not to a wrapper instance |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
193 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
194 |
public static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
195 |
MethodHandle wrapperInstanceTarget(Object x) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
196 |
return asWrapperInstance(x).getWrapperInstanceTarget(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
197 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
198 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
199 |
/** |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
200 |
* Recovers the unique single-method interface type for which this wrapper instance was created. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
201 |
* The object {@code x} must have been produced by a call to {@link #asInterfaceInstance asInterfaceInstance}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
202 |
* This requirement may be tested via {@link #isWrapperInstance isWrapperInstance}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
203 |
* @param x any reference |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
204 |
* @return the single-method interface type for which the wrapper was created |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
205 |
* @throws IllegalArgumentException if the reference x is not to a wrapper instance |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
206 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
207 |
public static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
208 |
Class<?> wrapperInstanceType(Object x) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
209 |
return asWrapperInstance(x).getWrapperInstanceType(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
210 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
211 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
212 |
private static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
213 |
boolean isObjectMethod(Method m) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
214 |
switch (m.getName()) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
215 |
case "toString": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
216 |
return (m.getReturnType() == String.class |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
217 |
&& m.getParameterTypes().length == 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
218 |
case "hashCode": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
219 |
return (m.getReturnType() == int.class |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
220 |
&& m.getParameterTypes().length == 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
221 |
case "equals": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
222 |
return (m.getReturnType() == boolean.class |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
223 |
&& m.getParameterTypes().length == 1 |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
224 |
&& m.getParameterTypes()[0] == Object.class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
225 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
226 |
return false; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
227 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
228 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
229 |
private static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
230 |
Object callObjectMethod(Object self, Method m, Object[] args) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
231 |
assert(isObjectMethod(m)) : m; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
232 |
switch (m.getName()) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
233 |
case "toString": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
234 |
return self.getClass().getName() + "@" + Integer.toHexString(self.hashCode()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
235 |
case "hashCode": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
236 |
return System.identityHashCode(self); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
237 |
case "equals": |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
238 |
return (self == args[0]); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
239 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
240 |
return null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
241 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
242 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
243 |
private static |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
244 |
Method getSingleMethod(Class<?> intfc) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
245 |
if (!intfc.isInterface()) return null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
246 |
Method sm = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
247 |
for (Method m : intfc.getMethods()) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
248 |
int mod = m.getModifiers(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
249 |
if (Modifier.isAbstract(mod)) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
250 |
if (sm != null && !isObjectMethod(sm)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
251 |
return null; // too many abstract methods |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
252 |
sm = m; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
253 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
254 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
255 |
return sm; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
256 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
diff
changeset
|
257 |
} |