/
TypeSupport.java
49 lines (41 loc) · 1.38 KB
/
TypeSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Copyright (c) 2021 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy;
import org.mockito.exceptions.base.MockitoException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
class TypeSupport {
static final TypeSupport INSTANCE;
static {
MethodHandle isSealed;
try {
isSealed =
MethodHandles.publicLookup()
.findVirtual(
Class.class, "isSealed", MethodType.methodType(boolean.class));
} catch (NoSuchMethodException e) {
isSealed = null;
} catch (IllegalAccessException e) {
throw new MockitoException("Failed to access Class.isSealed", e);
}
INSTANCE = new TypeSupport(isSealed);
}
private final MethodHandle isSealed;
private TypeSupport(MethodHandle isSealed) {
this.isSealed = isSealed;
}
boolean isSealed(Class<?> type) {
if (isSealed == null) {
return false;
}
try {
return (boolean) isSealed.invoke(type);
} catch (Throwable t) {
throw new MockitoException(
"Failed to check if type is sealed using handle " + isSealed, t);
}
}
}