author | rriggs |
Fri, 01 Dec 2017 16:40:08 -0500 | |
changeset 48224 | be0df5ab3093 |
parent 47216 | 71c04702a3d5 |
child 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22638 | 2 |
* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.lang.reflect.*; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.security.PrivilegedAction; |
|
32 |
||
33 |
||
34 |
class Reflect { // package-private |
|
35 |
||
36 |
private Reflect() { } |
|
37 |
||
38 |
private static class ReflectionError extends Error { |
|
39 |
private static final long serialVersionUID = -8659519328078164097L; |
|
40 |
ReflectionError(Throwable x) { |
|
41 |
super(x); |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
private static void setAccessible(final AccessibleObject ao) { |
|
51 | 46 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
47 |
public Void run() { |
|
2 | 48 |
ao.setAccessible(true); |
49 |
return null; |
|
50 |
}}); |
|
51 |
} |
|
52 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
53 |
static Constructor<?> lookupConstructor(String className, |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
54 |
Class<?>[] paramTypes) |
2 | 55 |
{ |
56 |
try { |
|
51 | 57 |
Class<?> cl = Class.forName(className); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
58 |
Constructor<?> c = cl.getDeclaredConstructor(paramTypes); |
2 | 59 |
setAccessible(c); |
60 |
return c; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
61 |
} catch (ClassNotFoundException | NoSuchMethodException x) { |
2 | 62 |
throw new ReflectionError(x); |
63 |
} |
|
64 |
} |
|
65 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
66 |
static Object invoke(Constructor<?> c, Object[] args) { |
2 | 67 |
try { |
68 |
return c.newInstance(args); |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
69 |
} catch (InstantiationException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
70 |
IllegalAccessException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
71 |
InvocationTargetException x) { |
2 | 72 |
throw new ReflectionError(x); |
73 |
} |
|
74 |
} |
|
75 |
||
76 |
static Method lookupMethod(String className, |
|
77 |
String methodName, |
|
22638 | 78 |
Class<?>... paramTypes) |
2 | 79 |
{ |
80 |
try { |
|
51 | 81 |
Class<?> cl = Class.forName(className); |
2 | 82 |
Method m = cl.getDeclaredMethod(methodName, paramTypes); |
83 |
setAccessible(m); |
|
84 |
return m; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
85 |
} catch (ClassNotFoundException | NoSuchMethodException x) { |
2 | 86 |
throw new ReflectionError(x); |
87 |
} |
|
88 |
} |
|
89 |
||
90 |
static Object invoke(Method m, Object ob, Object[] args) { |
|
91 |
try { |
|
92 |
return m.invoke(ob, args); |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
93 |
} catch (IllegalAccessException | InvocationTargetException x) { |
2 | 94 |
throw new ReflectionError(x); |
95 |
} |
|
96 |
} |
|
97 |
||
98 |
static Object invokeIO(Method m, Object ob, Object[] args) |
|
99 |
throws IOException |
|
100 |
{ |
|
101 |
try { |
|
102 |
return m.invoke(ob, args); |
|
103 |
} catch (IllegalAccessException x) { |
|
104 |
throw new ReflectionError(x); |
|
105 |
} catch (InvocationTargetException x) { |
|
106 |
if (IOException.class.isInstance(x.getCause())) |
|
107 |
throw (IOException)x.getCause(); |
|
108 |
throw new ReflectionError(x); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
static Field lookupField(String className, String fieldName) { |
|
113 |
try { |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
114 |
Class<?> cl = Class.forName(className); |
2 | 115 |
Field f = cl.getDeclaredField(fieldName); |
116 |
setAccessible(f); |
|
117 |
return f; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
118 |
} catch (ClassNotFoundException | NoSuchFieldException x) { |
2 | 119 |
throw new ReflectionError(x); |
120 |
} |
|
121 |
} |
|
122 |
||
123 |
static Object get(Object ob, Field f) { |
|
124 |
try { |
|
125 |
return f.get(ob); |
|
126 |
} catch (IllegalAccessException x) { |
|
127 |
throw new ReflectionError(x); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
static Object get(Field f) { |
|
132 |
return get(null, f); |
|
133 |
} |
|
134 |
||
135 |
static void set(Object ob, Field f, Object val) { |
|
136 |
try { |
|
137 |
f.set(ob, val); |
|
138 |
} catch (IllegalAccessException x) { |
|
139 |
throw new ReflectionError(x); |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
static void setInt(Object ob, Field f, int val) { |
|
144 |
try { |
|
145 |
f.setInt(ob, val); |
|
146 |
} catch (IllegalAccessException x) { |
|
147 |
throw new ReflectionError(x); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
static void setBoolean(Object ob, Field f, boolean val) { |
|
152 |
try { |
|
153 |
f.setBoolean(ob, val); |
|
154 |
} catch (IllegalAccessException x) { |
|
155 |
throw new ReflectionError(x); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
} |