Path: blob/master/src/java.base/share/classes/sun/nio/ch/Reflect.java
41159 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.*;28import java.lang.reflect.*;29import java.security.AccessController;30import java.security.PrivilegedAction;313233class Reflect { // package-private3435private Reflect() { }3637private static class ReflectionError extends Error {38@java.io.Serial39private static final long serialVersionUID = -8659519328078164097L;40ReflectionError(Throwable x) {41super(x);42}43}4445@SuppressWarnings("removal")46private static void setAccessible(final AccessibleObject ao) {47AccessController.doPrivileged(new PrivilegedAction<Void>() {48public Void run() {49ao.setAccessible(true);50return null;51}});52}5354static Constructor<?> lookupConstructor(String className,55Class<?>[] paramTypes)56{57try {58Class<?> cl = Class.forName(className);59Constructor<?> c = cl.getDeclaredConstructor(paramTypes);60setAccessible(c);61return c;62} catch (ClassNotFoundException | NoSuchMethodException x) {63throw new ReflectionError(x);64}65}6667static Object invoke(Constructor<?> c, Object[] args) {68try {69return c.newInstance(args);70} catch (InstantiationException |71IllegalAccessException |72InvocationTargetException x) {73throw new ReflectionError(x);74}75}7677static Method lookupMethod(String className,78String methodName,79Class<?>... paramTypes)80{81try {82Class<?> cl = Class.forName(className);83Method m = cl.getDeclaredMethod(methodName, paramTypes);84setAccessible(m);85return m;86} catch (ClassNotFoundException | NoSuchMethodException x) {87throw new ReflectionError(x);88}89}9091static Object invoke(Method m, Object ob, Object[] args) {92try {93return m.invoke(ob, args);94} catch (IllegalAccessException | InvocationTargetException x) {95throw new ReflectionError(x);96}97}9899static Object invokeIO(Method m, Object ob, Object[] args)100throws IOException101{102try {103return m.invoke(ob, args);104} catch (IllegalAccessException x) {105throw new ReflectionError(x);106} catch (InvocationTargetException x) {107if (IOException.class.isInstance(x.getCause()))108throw (IOException)x.getCause();109throw new ReflectionError(x);110}111}112113static Field lookupField(String className, String fieldName) {114try {115Class<?> cl = Class.forName(className);116Field f = cl.getDeclaredField(fieldName);117setAccessible(f);118return f;119} catch (ClassNotFoundException | NoSuchFieldException x) {120throw new ReflectionError(x);121}122}123124static Object get(Object ob, Field f) {125try {126return f.get(ob);127} catch (IllegalAccessException x) {128throw new ReflectionError(x);129}130}131132static Object get(Field f) {133return get(null, f);134}135136static void set(Object ob, Field f, Object val) {137try {138f.set(ob, val);139} catch (IllegalAccessException x) {140throw new ReflectionError(x);141}142}143144static void setInt(Object ob, Field f, int val) {145try {146f.setInt(ob, val);147} catch (IllegalAccessException x) {148throw new ReflectionError(x);149}150}151152static void setBoolean(Object ob, Field f, boolean val) {153try {154f.setBoolean(ob, val);155} catch (IllegalAccessException x) {156throw new ReflectionError(x);157}158}159160}161162163