author | alanb |
Sat, 02 Nov 2019 10:02:18 +0000 | |
changeset 58899 | 5573a7098439 |
parent 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 { |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47216
diff
changeset
|
39 |
@java.io.Serial |
2 | 40 |
private static final long serialVersionUID = -8659519328078164097L; |
41 |
ReflectionError(Throwable x) { |
|
42 |
super(x); |
|
43 |
} |
|
44 |
} |
|
45 |
||
46 |
private static void setAccessible(final AccessibleObject ao) { |
|
51 | 47 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
48 |
public Void run() { |
|
2 | 49 |
ao.setAccessible(true); |
50 |
return null; |
|
51 |
}}); |
|
52 |
} |
|
53 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
54 |
static Constructor<?> lookupConstructor(String className, |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
55 |
Class<?>[] paramTypes) |
2 | 56 |
{ |
57 |
try { |
|
51 | 58 |
Class<?> cl = Class.forName(className); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
59 |
Constructor<?> c = cl.getDeclaredConstructor(paramTypes); |
2 | 60 |
setAccessible(c); |
61 |
return c; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
62 |
} catch (ClassNotFoundException | NoSuchMethodException x) { |
2 | 63 |
throw new ReflectionError(x); |
64 |
} |
|
65 |
} |
|
66 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
67 |
static Object invoke(Constructor<?> c, Object[] args) { |
2 | 68 |
try { |
69 |
return c.newInstance(args); |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
70 |
} catch (InstantiationException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
71 |
IllegalAccessException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
72 |
InvocationTargetException x) { |
2 | 73 |
throw new ReflectionError(x); |
74 |
} |
|
75 |
} |
|
76 |
||
77 |
static Method lookupMethod(String className, |
|
78 |
String methodName, |
|
22638 | 79 |
Class<?>... paramTypes) |
2 | 80 |
{ |
81 |
try { |
|
51 | 82 |
Class<?> cl = Class.forName(className); |
2 | 83 |
Method m = cl.getDeclaredMethod(methodName, paramTypes); |
84 |
setAccessible(m); |
|
85 |
return m; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
86 |
} catch (ClassNotFoundException | NoSuchMethodException x) { |
2 | 87 |
throw new ReflectionError(x); |
88 |
} |
|
89 |
} |
|
90 |
||
91 |
static Object invoke(Method m, Object ob, Object[] args) { |
|
92 |
try { |
|
93 |
return m.invoke(ob, args); |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
94 |
} catch (IllegalAccessException | InvocationTargetException x) { |
2 | 95 |
throw new ReflectionError(x); |
96 |
} |
|
97 |
} |
|
98 |
||
99 |
static Object invokeIO(Method m, Object ob, Object[] args) |
|
100 |
throws IOException |
|
101 |
{ |
|
102 |
try { |
|
103 |
return m.invoke(ob, args); |
|
104 |
} catch (IllegalAccessException x) { |
|
105 |
throw new ReflectionError(x); |
|
106 |
} catch (InvocationTargetException x) { |
|
107 |
if (IOException.class.isInstance(x.getCause())) |
|
108 |
throw (IOException)x.getCause(); |
|
109 |
throw new ReflectionError(x); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
static Field lookupField(String className, String fieldName) { |
|
114 |
try { |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
115 |
Class<?> cl = Class.forName(className); |
2 | 116 |
Field f = cl.getDeclaredField(fieldName); |
117 |
setAccessible(f); |
|
118 |
return f; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
119 |
} catch (ClassNotFoundException | NoSuchFieldException x) { |
2 | 120 |
throw new ReflectionError(x); |
121 |
} |
|
122 |
} |
|
123 |
||
124 |
static Object get(Object ob, Field f) { |
|
125 |
try { |
|
126 |
return f.get(ob); |
|
127 |
} catch (IllegalAccessException x) { |
|
128 |
throw new ReflectionError(x); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
static Object get(Field f) { |
|
133 |
return get(null, f); |
|
134 |
} |
|
135 |
||
136 |
static void set(Object ob, Field f, Object val) { |
|
137 |
try { |
|
138 |
f.set(ob, val); |
|
139 |
} catch (IllegalAccessException x) { |
|
140 |
throw new ReflectionError(x); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
static void setInt(Object ob, Field f, int val) { |
|
145 |
try { |
|
146 |
f.setInt(ob, val); |
|
147 |
} catch (IllegalAccessException x) { |
|
148 |
throw new ReflectionError(x); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
static void setBoolean(Object ob, Field f, boolean val) { |
|
153 |
try { |
|
154 |
f.setBoolean(ob, val); |
|
155 |
} catch (IllegalAccessException x) { |
|
156 |
throw new ReflectionError(x); |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
} |