001: /*
002: * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.nio.ch;
027:
028: import java.io.*;
029: import java.lang.reflect.*;
030: import java.security.AccessController;
031: import java.security.PrivilegedAction;
032:
033: class Reflect { // package-private
034:
035: private Reflect() {
036: }
037:
038: private static class ReflectionError extends Error {
039: ReflectionError(Throwable x) {
040: super (x);
041: }
042: }
043:
044: private static void setAccessible(final AccessibleObject ao) {
045: AccessController.doPrivileged(new PrivilegedAction() {
046: public Object run() {
047: ao.setAccessible(true);
048: return null;
049: }
050: });
051: }
052:
053: static Constructor lookupConstructor(String className,
054: Class[] paramTypes) {
055: try {
056: Class cl = Class.forName(className);
057: Constructor c = cl.getDeclaredConstructor(paramTypes);
058: setAccessible(c);
059: return c;
060: } catch (ClassNotFoundException x) {
061: throw new ReflectionError(x);
062: } catch (NoSuchMethodException x) {
063: throw new ReflectionError(x);
064: }
065: }
066:
067: static Object invoke(Constructor c, Object[] args) {
068: try {
069: return c.newInstance(args);
070: } catch (InstantiationException x) {
071: throw new ReflectionError(x);
072: } catch (IllegalAccessException x) {
073: throw new ReflectionError(x);
074: } catch (InvocationTargetException x) {
075: throw new ReflectionError(x);
076: }
077: }
078:
079: static Method lookupMethod(String className, String methodName,
080: Class[] paramTypes) {
081: try {
082: Class cl = Class.forName(className);
083: Method m = cl.getDeclaredMethod(methodName, paramTypes);
084: setAccessible(m);
085: return m;
086: } catch (ClassNotFoundException x) {
087: throw new ReflectionError(x);
088: } catch (NoSuchMethodException x) {
089: throw new ReflectionError(x);
090: }
091: }
092:
093: static Object invoke(Method m, Object ob, Object[] args) {
094: try {
095: return m.invoke(ob, args);
096: } catch (IllegalAccessException x) {
097: throw new ReflectionError(x);
098: } catch (InvocationTargetException x) {
099: throw new ReflectionError(x);
100: }
101: }
102:
103: static Object invokeIO(Method m, Object ob, Object[] args)
104: throws IOException {
105: try {
106: return m.invoke(ob, args);
107: } catch (IllegalAccessException x) {
108: throw new ReflectionError(x);
109: } catch (InvocationTargetException x) {
110: if (IOException.class.isInstance(x.getCause()))
111: throw (IOException) x.getCause();
112: throw new ReflectionError(x);
113: }
114: }
115:
116: static Field lookupField(String className, String fieldName) {
117: try {
118: Class cl = Class.forName(className);
119: Field f = cl.getDeclaredField(fieldName);
120: setAccessible(f);
121: return f;
122: } catch (ClassNotFoundException x) {
123: throw new ReflectionError(x);
124: } catch (NoSuchFieldException x) {
125: throw new ReflectionError(x);
126: }
127: }
128:
129: static Object get(Object ob, Field f) {
130: try {
131: return f.get(ob);
132: } catch (IllegalAccessException x) {
133: throw new ReflectionError(x);
134: }
135: }
136:
137: static Object get(Field f) {
138: return get(null, f);
139: }
140:
141: static void set(Object ob, Field f, Object val) {
142: try {
143: f.set(ob, val);
144: } catch (IllegalAccessException x) {
145: throw new ReflectionError(x);
146: }
147: }
148:
149: static void setInt(Object ob, Field f, int val) {
150: try {
151: f.setInt(ob, val);
152: } catch (IllegalAccessException x) {
153: throw new ReflectionError(x);
154: }
155: }
156:
157: static void setBoolean(Object ob, Field f, boolean val) {
158: try {
159: f.setBoolean(ob, val);
160: } catch (IllegalAccessException x) {
161: throw new ReflectionError(x);
162: }
163: }
164:
165: }
|