| 1 |
package org.jpype; |
| 2 |
|
| 3 |
import java.lang.invoke.MethodHandle; |
| 4 |
import java.lang.invoke.MethodHandleProxies; |
| 5 |
import java.lang.invoke.MethodHandles; |
| 6 |
import java.lang.reflect.Method; |
| 7 |
import java.lang.reflect.Modifier; |
| 8 |
import java.net.URISyntaxException; |
| 9 |
import java.nio.file.Path; |
| 10 |
import java.nio.file.Paths; |
| 11 |
import java.util.Arrays; |
| 12 |
import java.util.function.Predicate; |
| 13 |
|
| 14 |
public class JPypeUtilities |
| 15 |
{ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private static final Method[] OBJECT_METHODS |
| 20 |
= Arrays.stream(Object.class.getMethods()) |
| 21 |
.filter(m -> !Modifier.isFinal(m.getModifiers())) |
| 22 |
.toArray(Method[]::new); |
| 23 |
|
| 24 |
private static final Predicate<Class> isSealed; |
| 25 |
|
| 26 |
static |
| 27 |
{ |
| 28 |
Predicate<Class> result = null; |
| 29 |
try |
| 30 |
{ |
| 31 |
Method m = Class.class.getMethod("isSealed"); |
| 32 |
MethodHandle handle = MethodHandles.publicLookup().unreflect(m); |
| 33 |
result = MethodHandleProxies.asInterfaceInstance(Predicate.class, handle); |
| 34 |
} catch (IllegalAccessException e) |
| 35 |
{ |
| 36 |
|
| 37 |
throw new IllegalAccessError(e.getMessage()); |
| 38 |
} catch (NoSuchMethodException e) |
| 39 |
{ |
| 40 |
|
| 41 |
result = c -> false; |
| 42 |
} |
| 43 |
isSealed = result; |
| 44 |
} |
| 45 |
|
| 46 |
public static Path getJarPath(Class c) |
| 47 |
{ |
| 48 |
try |
| 49 |
{ |
| 50 |
return Paths.get(c.getProtectionDomain().getCodeSource().getLocation() |
| 51 |
.toURI()).getParent(); |
| 52 |
} catch (URISyntaxException ex) |
| 53 |
{ |
| 54 |
return null; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public static Method getFunctionalInterfaceMethod(Class cls) |
| 59 |
{ |
| 60 |
if (!cls.isInterface() || cls.isAnnotation() || isSealed.test(cls)) |
| 61 |
return null; |
| 62 |
|
| 63 |
Method result = null; |
| 64 |
for (Method m : cls.getMethods()) |
| 65 |
{ |
| 66 |
if (Modifier.isAbstract(m.getModifiers())) |
| 67 |
{ |
| 68 |
if (isObjectMethodOverride(m)) |
| 69 |
continue; |
| 70 |
|
| 71 |
if (result != null && !equals(m, result)) |
| 72 |
return null; |
| 73 |
|
| 74 |
if (result == null || cls.equals(m.getDeclaringClass())) |
| 75 |
result = m; |
| 76 |
} |
| 77 |
} |
| 78 |
return result; |
| 79 |
} |
| 80 |
|
| 81 |
private static boolean isObjectMethodOverride(Method m) |
| 82 |
{ |
| 83 |
for (Method objectMethod : OBJECT_METHODS) |
| 84 |
{ |
| 85 |
if (equals(m, objectMethod)) |
| 86 |
return true; |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
private static boolean equals(Method a, Method b) |
| 92 |
{ |
| 93 |
|
| 94 |
if (a.getParameterCount() != b.getParameterCount()) |
| 95 |
return false; |
| 96 |
|
| 97 |
if (!a.getName().equals(b.getName())) |
| 98 |
return false; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
if (!Arrays.equals(a.getParameterTypes(), b.getParameterTypes())) |
| 103 |
return false; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
} |