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