Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/Reflect.java
38918 views
/*1* Copyright (c) 2000, 2011, 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 {38private static final long serialVersionUID = -8659519328078164097L;39ReflectionError(Throwable x) {40super(x);41}42}4344private static void setAccessible(final AccessibleObject ao) {45AccessController.doPrivileged(new PrivilegedAction<Void>() {46public Void run() {47ao.setAccessible(true);48return null;49}});50}5152static Constructor<?> lookupConstructor(String className,53Class<?>[] paramTypes)54{55try {56Class<?> cl = Class.forName(className);57Constructor<?> c = cl.getDeclaredConstructor(paramTypes);58setAccessible(c);59return c;60} catch (ClassNotFoundException | NoSuchMethodException x) {61throw new ReflectionError(x);62}63}6465static Object invoke(Constructor<?> c, Object[] args) {66try {67return c.newInstance(args);68} catch (InstantiationException |69IllegalAccessException |70InvocationTargetException x) {71throw new ReflectionError(x);72}73}7475static Method lookupMethod(String className,76String methodName,77Class... paramTypes)78{79try {80Class<?> cl = Class.forName(className);81Method m = cl.getDeclaredMethod(methodName, paramTypes);82setAccessible(m);83return m;84} catch (ClassNotFoundException | NoSuchMethodException x) {85throw new ReflectionError(x);86}87}8889static Object invoke(Method m, Object ob, Object[] args) {90try {91return m.invoke(ob, args);92} catch (IllegalAccessException | InvocationTargetException x) {93throw new ReflectionError(x);94}95}9697static Object invokeIO(Method m, Object ob, Object[] args)98throws IOException99{100try {101return m.invoke(ob, args);102} catch (IllegalAccessException x) {103throw new ReflectionError(x);104} catch (InvocationTargetException x) {105if (IOException.class.isInstance(x.getCause()))106throw (IOException)x.getCause();107throw new ReflectionError(x);108}109}110111static Field lookupField(String className, String fieldName) {112try {113Class<?> cl = Class.forName(className);114Field f = cl.getDeclaredField(fieldName);115setAccessible(f);116return f;117} catch (ClassNotFoundException | NoSuchFieldException x) {118throw new ReflectionError(x);119}120}121122static Object get(Object ob, Field f) {123try {124return f.get(ob);125} catch (IllegalAccessException x) {126throw new ReflectionError(x);127}128}129130static Object get(Field f) {131return get(null, f);132}133134static void set(Object ob, Field f, Object val) {135try {136f.set(ob, val);137} catch (IllegalAccessException x) {138throw new ReflectionError(x);139}140}141142static void setInt(Object ob, Field f, int val) {143try {144f.setInt(ob, val);145} catch (IllegalAccessException x) {146throw new ReflectionError(x);147}148}149150static void setBoolean(Object ob, Field f, boolean val) {151try {152f.setBoolean(ob, val);153} catch (IllegalAccessException x) {154throw new ReflectionError(x);155}156}157158}159160161