1
+ /*
2
+ * Numismatics
3
+ * Copyright (c) 2024 The Railways Team
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ package dev .ithundxr .createnumismatics .util ;
20
+
21
+ import java .lang .invoke .MethodHandles ;
22
+
23
+ @ SuppressWarnings ("unchecked" )
24
+ public class MethodVarHandleUtils {
25
+ private static final MethodHandles .Lookup lookup = MethodHandles .lookup ();
26
+
27
+ public static <T > T getStaticField (Class <?> clazz , String fieldName , Class <T > type ) throws NoSuchFieldException , IllegalAccessException {
28
+ return (T ) lookup .findStaticVarHandle (clazz , fieldName , type ).get ();
29
+ }
30
+
31
+ public static <T > T getStaticField (Class <?> clazz , String fieldName , Class <T > type , T defaultValue ) {
32
+ T returnValue = defaultValue ;
33
+
34
+ try {
35
+ returnValue = getStaticField (clazz , fieldName , type );
36
+ } catch (NoSuchFieldException | IllegalAccessException ignored ) {}
37
+
38
+ return returnValue ;
39
+ }
40
+ }
0 commit comments