src/java.base/share/classes/sun/nio/ch/Reflect.java
author rriggs
Fri, 01 Dec 2017 16:40:08 -0500
changeset 48224 be0df5ab3093
parent 47216 71c04702a3d5
child 57956 e0b8b019d2f5
permissions -rw-r--r--
8080225: FileInput/OutputStream/FileChannel cleanup should be improved Reviewed-by: mchung, plevart, bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22638
29df1779d0de 8033527: Fix raw type lint warning in sun.nio.ch
darcy
parents: 10137
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2057
diff changeset
    23
 * questions.
2
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) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    46
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    47
                public Void run() {
2
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
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    53
    static Constructor<?> lookupConstructor(String className,
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    54
                                            Class<?>[] paramTypes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            setAccessible(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return c;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    61
        } catch (ClassNotFoundException | NoSuchMethodException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    66
    static Object invoke(Constructor<?> c, Object[] args) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return c.newInstance(args);
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    69
        } catch (InstantiationException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    70
                 IllegalAccessException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    71
                 InvocationTargetException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static Method lookupMethod(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                               String methodName,
22638
29df1779d0de 8033527: Fix raw type lint warning in sun.nio.ch
darcy
parents: 10137
diff changeset
    78
                               Class<?>... paramTypes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    81
            Class<?> cl = Class.forName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            Method m = cl.getDeclaredMethod(methodName, paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            setAccessible(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            return m;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    85
        } catch (ClassNotFoundException | NoSuchMethodException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static Object invoke(Method m, Object ob, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return m.invoke(ob, args);
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    93
        } catch (IllegalAccessException | InvocationTargetException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static Object invokeIO(Method m, Object ob, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return m.invoke(ob, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (InvocationTargetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (IOException.class.isInstance(x.getCause()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw (IOException)x.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static Field lookupField(String className, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   114
            Class<?> cl = Class.forName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            Field f = cl.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            setAccessible(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return f;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   118
        } catch (ClassNotFoundException | NoSuchFieldException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static Object get(Object ob, Field f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return f.get(ob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static Object get(Field f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return get(null, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static void set(Object ob, Field f, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            f.set(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static void setInt(Object ob, Field f, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            f.setInt(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static void setBoolean(Object ob, Field f, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            f.setBoolean(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}