Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/ch/Reflect.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, 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
@SuppressWarnings("removal")
47
private static void setAccessible(final AccessibleObject ao) {
48
AccessController.doPrivileged(new PrivilegedAction<Void>() {
49
public Void run() {
50
ao.setAccessible(true);
51
return null;
52
}});
53
}
54
55
static Constructor<?> lookupConstructor(String className,
56
Class<?>[] paramTypes)
57
{
58
try {
59
Class<?> cl = Class.forName(className);
60
Constructor<?> c = cl.getDeclaredConstructor(paramTypes);
61
setAccessible(c);
62
return c;
63
} catch (ClassNotFoundException | NoSuchMethodException x) {
64
throw new ReflectionError(x);
65
}
66
}
67
68
static Object invoke(Constructor<?> c, Object[] args) {
69
try {
70
return c.newInstance(args);
71
} catch (InstantiationException |
72
IllegalAccessException |
73
InvocationTargetException x) {
74
throw new ReflectionError(x);
75
}
76
}
77
78
static Method lookupMethod(String className,
79
String methodName,
80
Class<?>... paramTypes)
81
{
82
try {
83
Class<?> cl = Class.forName(className);
84
Method m = cl.getDeclaredMethod(methodName, paramTypes);
85
setAccessible(m);
86
return m;
87
} catch (ClassNotFoundException | NoSuchMethodException x) {
88
throw new ReflectionError(x);
89
}
90
}
91
92
static Object invoke(Method m, Object ob, Object[] args) {
93
try {
94
return m.invoke(ob, args);
95
} catch (IllegalAccessException | InvocationTargetException x) {
96
throw new ReflectionError(x);
97
}
98
}
99
100
static Object invokeIO(Method m, Object ob, Object[] args)
101
throws IOException
102
{
103
try {
104
return m.invoke(ob, args);
105
} catch (IllegalAccessException x) {
106
throw new ReflectionError(x);
107
} catch (InvocationTargetException x) {
108
if (IOException.class.isInstance(x.getCause()))
109
throw (IOException)x.getCause();
110
throw new ReflectionError(x);
111
}
112
}
113
114
static Field lookupField(String className, String fieldName) {
115
try {
116
Class<?> cl = Class.forName(className);
117
Field f = cl.getDeclaredField(fieldName);
118
setAccessible(f);
119
return f;
120
} catch (ClassNotFoundException | NoSuchFieldException x) {
121
throw new ReflectionError(x);
122
}
123
}
124
125
static Object get(Object ob, Field f) {
126
try {
127
return f.get(ob);
128
} catch (IllegalAccessException x) {
129
throw new ReflectionError(x);
130
}
131
}
132
133
static Object get(Field f) {
134
return get(null, f);
135
}
136
137
static void set(Object ob, Field f, Object val) {
138
try {
139
f.set(ob, val);
140
} catch (IllegalAccessException x) {
141
throw new ReflectionError(x);
142
}
143
}
144
145
static void setInt(Object ob, Field f, int val) {
146
try {
147
f.setInt(ob, val);
148
} catch (IllegalAccessException x) {
149
throw new ReflectionError(x);
150
}
151
}
152
153
static void setBoolean(Object ob, Field f, boolean val) {
154
try {
155
f.setBoolean(ob, val);
156
} catch (IllegalAccessException x) {
157
throw new ReflectionError(x);
158
}
159
}
160
161
}
162
163