src/java.base/share/classes/sun/nio/ch/Reflect.java
author alanb
Sat, 02 Nov 2019 10:02:18 +0000
changeset 58899 5573a7098439
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
8232673: (dc) DatagramChannel socket adaptor issues Reviewed-by: dfuchs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2019, 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 {
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 47216
diff changeset
    39
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        private static final long serialVersionUID = -8659519328078164097L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        ReflectionError(Throwable x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            super(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static void setAccessible(final AccessibleObject ao) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    47
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    48
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    ao.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    54
    static Constructor<?> lookupConstructor(String className,
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    55
                                            Class<?>[] paramTypes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    58
            Class<?> cl = Class.forName(className);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
    59
            Constructor<?> c = cl.getDeclaredConstructor(paramTypes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            setAccessible(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return c;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    62
        } catch (ClassNotFoundException | NoSuchMethodException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    67
    static Object invoke(Constructor<?> c, Object[] args) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return c.newInstance(args);
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    70
        } catch (InstantiationException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    71
                 IllegalAccessException |
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    72
                 InvocationTargetException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static Method lookupMethod(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                               String methodName,
22638
29df1779d0de 8033527: Fix raw type lint warning in sun.nio.ch
darcy
parents: 10137
diff changeset
    79
                               Class<?>... paramTypes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    82
            Class<?> cl = Class.forName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Method m = cl.getDeclaredMethod(methodName, paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            setAccessible(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return m;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    86
        } catch (ClassNotFoundException | NoSuchMethodException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static Object invoke(Method m, Object ob, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return m.invoke(ob, args);
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    94
        } catch (IllegalAccessException | InvocationTargetException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static Object invokeIO(Method m, Object ob, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return m.invoke(ob, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (InvocationTargetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (IOException.class.isInstance(x.getCause()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throw (IOException)x.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static Field lookupField(String className, String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   115
            Class<?> cl = Class.forName(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            Field f = cl.getDeclaredField(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            setAccessible(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return f;
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   119
        } catch (ClassNotFoundException | NoSuchFieldException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static Object get(Object ob, Field f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return f.get(ob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static Object get(Field f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return get(null, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static void set(Object ob, Field f, Object val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            f.set(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static void setInt(Object ob, Field f, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            f.setInt(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static void setBoolean(Object ob, Field f, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            f.setBoolean(ob, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (IllegalAccessException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new ReflectionError(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}