author | alanb |
Sun, 15 Feb 2009 12:25:54 +0000 | |
changeset 2057 | 3acf8e5e2ca0 |
parent 715 | f16baef3a20e |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
2 |
* Copyright 2000-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
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 |
||
53 |
static Constructor lookupConstructor(String className, |
|
54 |
Class[] paramTypes) |
|
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; |
|
61 |
} catch (ClassNotFoundException x) { |
|
62 |
throw new ReflectionError(x); |
|
63 |
} catch (NoSuchMethodException x) { |
|
64 |
throw new ReflectionError(x); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
static Object invoke(Constructor c, Object[] args) { |
|
69 |
try { |
|
70 |
return c.newInstance(args); |
|
71 |
} catch (InstantiationException x) { |
|
72 |
throw new ReflectionError(x); |
|
73 |
} catch (IllegalAccessException x) { |
|
74 |
throw new ReflectionError(x); |
|
75 |
} catch (InvocationTargetException x) { |
|
76 |
throw new ReflectionError(x); |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
static Method lookupMethod(String className, |
|
81 |
String methodName, |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
82 |
Class... paramTypes) |
2 | 83 |
{ |
84 |
try { |
|
51 | 85 |
Class<?> cl = Class.forName(className); |
2 | 86 |
Method m = cl.getDeclaredMethod(methodName, paramTypes); |
87 |
setAccessible(m); |
|
88 |
return m; |
|
89 |
} catch (ClassNotFoundException x) { |
|
90 |
throw new ReflectionError(x); |
|
91 |
} catch (NoSuchMethodException x) { |
|
92 |
throw new ReflectionError(x); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
static Object invoke(Method m, Object ob, Object[] args) { |
|
97 |
try { |
|
98 |
return m.invoke(ob, args); |
|
99 |
} catch (IllegalAccessException x) { |
|
100 |
throw new ReflectionError(x); |
|
101 |
} catch (InvocationTargetException x) { |
|
102 |
throw new ReflectionError(x); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
static Object invokeIO(Method m, Object ob, Object[] args) |
|
107 |
throws IOException |
|
108 |
{ |
|
109 |
try { |
|
110 |
return m.invoke(ob, args); |
|
111 |
} catch (IllegalAccessException x) { |
|
112 |
throw new ReflectionError(x); |
|
113 |
} catch (InvocationTargetException x) { |
|
114 |
if (IOException.class.isInstance(x.getCause())) |
|
115 |
throw (IOException)x.getCause(); |
|
116 |
throw new ReflectionError(x); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
static Field lookupField(String className, String fieldName) { |
|
121 |
try { |
|
122 |
Class cl = Class.forName(className); |
|
123 |
Field f = cl.getDeclaredField(fieldName); |
|
124 |
setAccessible(f); |
|
125 |
return f; |
|
126 |
} catch (ClassNotFoundException x) { |
|
127 |
throw new ReflectionError(x); |
|
128 |
} catch (NoSuchFieldException x) { |
|
129 |
throw new ReflectionError(x); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
static Object get(Object ob, Field f) { |
|
134 |
try { |
|
135 |
return f.get(ob); |
|
136 |
} catch (IllegalAccessException x) { |
|
137 |
throw new ReflectionError(x); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
static Object get(Field f) { |
|
142 |
return get(null, f); |
|
143 |
} |
|
144 |
||
145 |
static void set(Object ob, Field f, Object val) { |
|
146 |
try { |
|
147 |
f.set(ob, val); |
|
148 |
} catch (IllegalAccessException x) { |
|
149 |
throw new ReflectionError(x); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
static void setInt(Object ob, Field f, int val) { |
|
154 |
try { |
|
155 |
f.setInt(ob, val); |
|
156 |
} catch (IllegalAccessException x) { |
|
157 |
throw new ReflectionError(x); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
static void setBoolean(Object ob, Field f, boolean val) { |
|
162 |
try { |
|
163 |
f.setBoolean(ob, val); |
|
164 |
} catch (IllegalAccessException x) { |
|
165 |
throw new ReflectionError(x); |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
} |