jdk/src/share/classes/sun/nio/ch/Reflect.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
class Reflect {                                 // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private Reflect() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static class ReflectionError extends Error {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        private static final long serialVersionUID = -8659519328078164097L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        ReflectionError(Throwable x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            super(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void setAccessible(final AccessibleObject ao) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                    ao.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static Constructor lookupConstructor(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                         Class[] paramTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            Class cl = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            Constructor c = cl.getDeclaredConstructor(paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            setAccessible(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static Object invoke(Constructor c, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return c.newInstance(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } catch (InstantiationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (InvocationTargetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static Method lookupMethod(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                               String methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                               Class[] paramTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            Class cl = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Method m = cl.getDeclaredMethod(methodName, paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            setAccessible(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static Object invoke(Method m, Object ob, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return m.invoke(ob, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (InvocationTargetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static Object invokeIO(Method m, Object ob, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return m.invoke(ob, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (InvocationTargetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (IOException.class.isInstance(x.getCause()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw (IOException)x.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static Field lookupField(String className, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            Class cl = Class.forName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Field f = cl.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            setAccessible(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (NoSuchFieldException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static Object get(Object ob, Field f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return f.get(ob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static Object get(Field f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return get(null, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static void set(Object ob, Field f, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            f.set(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static void setInt(Object ob, Field f, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            f.setInt(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static void setBoolean(Object ob, Field f, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            f.setBoolean(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}