Showing the changes to java/pkg/sun/nio/ch/Reflect.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,169 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved. | |
| 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. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 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. | |
| 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) { | |
| 46 | + AccessController.doPrivileged(new PrivilegedAction<Void>() { | |
| 47 | + public Void run() { | |
| 48 | + ao.setAccessible(true); | |
| 49 | + return null; | |
| 50 | + }}); | |
| 51 | + } | |
| 52 | + | |
| 53 | + static Constructor lookupConstructor(String className, | |
| 54 | + Class[] paramTypes) | |
| 55 | + { | |
| 56 | + try { | |
| 57 | + Class<?> cl = Class.forName(className); | |
| 58 | + Constructor<?> c = cl.getDeclaredConstructor(paramTypes); | |
| 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, | |
| 82 | + Class... paramTypes) | |
| 83 | + { | |
| 84 | + try { | |
| 85 | + Class<?> cl = Class.forName(className); | |
| 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 | +} | |