18
18
19
19
package org .apache .parquet .hadoop .util .wrapped .io ;
20
20
21
+ import static org .apache .parquet .Preconditions .checkState ;
22
+
23
+ import java .io .IOException ;
24
+ import java .io .UncheckedIOException ;
25
+ import java .util .function .Supplier ;
21
26
import org .apache .parquet .util .DynMethods ;
22
27
import org .slf4j .Logger ;
23
28
import org .slf4j .LoggerFactory ;
@@ -31,6 +36,35 @@ final class BindingUtils {
31
36
32
37
private BindingUtils () {}
33
38
39
+ /**
40
+ * Load a class by name.
41
+ * @param cl classloader to use.
42
+ * @param className classname
43
+ * @return the class or null if it could not be loaded.
44
+ */
45
+ public static Class <?> loadClass (ClassLoader cl , String className ) {
46
+ try {
47
+ return cl .loadClass (className );
48
+ } catch (ClassNotFoundException e ) {
49
+ LOG .debug ("No class {}" , className , e );
50
+ return null ;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Load a class by name.
56
+ * @param className classname
57
+ * @return the class or null if it could not be loaded.
58
+ */
59
+ public static Class <?> loadClass (String className ) {
60
+ try {
61
+ return Class .forName (className );
62
+ } catch (ClassNotFoundException e ) {
63
+ LOG .debug ("No class {}" , className , e );
64
+ return null ;
65
+ }
66
+ }
67
+
34
68
/**
35
69
* Get an invocation from the source class, which will be unavailable() if
36
70
* the class is null or the method isn't found.
@@ -65,6 +99,30 @@ static <T> DynMethods.UnboundMethod loadInvocation(
65
99
}
66
100
}
67
101
102
+ /**
103
+ * Load a static method from the source class, which will be a noop() if
104
+ * the class is null or the method isn't found.
105
+ * If the class and method are not found, then an {@code IllegalStateException}
106
+ * is raised on the basis that this means that the binding class is broken,
107
+ * rather than missing/out of date.
108
+ *
109
+ * @param <T> return type
110
+ * @param source source. If null, the method is a no-op.
111
+ * @param returnType return type class (unused)
112
+ * @param name method name
113
+ * @param parameterTypes parameters
114
+ *
115
+ * @return the method or a no-op.
116
+ * @throws IllegalStateException if the method is not static.
117
+ */
118
+ public static <T > DynMethods .UnboundMethod loadStaticMethod (
119
+ Class <?> source , Class <? extends T > returnType , String name , Class <?>... parameterTypes ) {
120
+
121
+ final DynMethods .UnboundMethod method = loadInvocation (source , returnType , name , parameterTypes );
122
+ checkState (method .isStatic (), "Method is not static %s" , method );
123
+ return method ;
124
+ }
125
+
68
126
/**
69
127
* Create a no-op method.
70
128
*
@@ -91,4 +149,40 @@ static boolean implemented(DynMethods.UnboundMethod... methods) {
91
149
}
92
150
return true ;
93
151
}
152
+
153
+ /**
154
+ * Require a method to be available.
155
+ * @param method method to probe
156
+ * @throws UnsupportedOperationException if the method was not found.
157
+ */
158
+ static void checkAvailable (DynMethods .UnboundMethod method ) throws UnsupportedOperationException {
159
+ if (method .isNoop ()) {
160
+ throw new UnsupportedOperationException ("Unbound " + method );
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Is a method available?
166
+ * @param method method to probe
167
+ * @return true iff the method is found and loaded.
168
+ */
169
+ static boolean available (DynMethods .UnboundMethod method ) {
170
+ return !method .isNoop ();
171
+ }
172
+
173
+ /**
174
+ * Invoke the supplier, catching any {@code UncheckedIOException} raised,
175
+ * extracting the inner IOException and rethrowing it.
176
+ * @param call call to invoke
177
+ * @return result
178
+ * @param <T> type of result
179
+ * @throws IOException if the call raised an IOException wrapped by an UncheckedIOException.
180
+ */
181
+ static <T > T extractIOEs (Supplier <T > call ) throws IOException {
182
+ try {
183
+ return call .get ();
184
+ } catch (UncheckedIOException e ) {
185
+ throw e .getCause ();
186
+ }
187
+ }
94
188
}
0 commit comments