|
| 1 | +package org.xomda.common.reflect; |
| 2 | + |
| 3 | +import static org.xomda.common.function.Predicates.against; |
| 4 | + |
| 5 | +import java.lang.reflect.Method; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import org.xomda.common.exception.SneakyThrow; |
| 12 | + |
| 13 | +/** |
| 14 | + * Be careful what you do. |
| 15 | + */ |
| 16 | +public final class Reflect { |
| 17 | + |
| 18 | + /** |
| 19 | + * Unchecked cast without warning. |
| 20 | + */ |
| 21 | + public static <P> P unchecked(final Object p) { |
| 22 | + @SuppressWarnings("unchecked") |
| 23 | + final P r = (P) p; |
| 24 | + return r; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Find a class with a given classname. |
| 29 | + */ |
| 30 | + public static <T> Optional<Class<T>> findClass(final String className) { |
| 31 | + try { |
| 32 | + final Class<T> found = unchecked(Class.forName(className)); |
| 33 | + return Optional.of(found); |
| 34 | + } catch (final ClassNotFoundException e) { |
| 35 | + return Optional.empty(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Find a class with a given classname and class loader and specify whether to initialize the class or not. |
| 41 | + */ |
| 42 | + public static <T> Optional<Class<T>> findClass(final String className, final boolean initialize, final ClassLoader classLoader) { |
| 43 | + try { |
| 44 | + final Class<T> found = unchecked(Class.forName(className, initialize, classLoader)); |
| 45 | + return Optional.of(found); |
| 46 | + } catch (final ClassNotFoundException e) { |
| 47 | + return Optional.empty(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Find a class with a given classname and class loader. |
| 53 | + */ |
| 54 | + public static <T> Optional<Class<T>> findClass(final String className, final ClassLoader classLoader) { |
| 55 | + return findClass(className, true, classLoader); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Find all methods in given class with a given name. |
| 60 | + */ |
| 61 | + public static Stream<Method> findMethods(final Class<?> clazz, final String name) { |
| 62 | + if (null == name || name.isBlank()) { |
| 63 | + throw new IllegalArgumentException("The name should not be blank or null"); |
| 64 | + } |
| 65 | + return Arrays |
| 66 | + .stream(clazz.getDeclaredMethods()) |
| 67 | + .filter(against(Method::getName, name)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Find a method in given class with a given name and arguments. |
| 72 | + */ |
| 73 | + public static Optional<Method> findMethod(final Class<?> clazz, final String name, Class<?>... args) { |
| 74 | + return findMethods(clazz, name) |
| 75 | + .filter(null == args || args.length == 0 |
| 76 | + ? (Method method) -> method.getParameterCount() == 0 |
| 77 | + : (Method method) -> { |
| 78 | + if (method.getParameterCount() < args.length) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + Class<?>[] params = method.getParameterTypes(); |
| 82 | + for (int i = 0; i < args.length; i++) { |
| 83 | + if (!Objects.equals(params[i], args[i])) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + return true; |
| 88 | + }) |
| 89 | + .findFirst(); |
| 90 | + } |
| 91 | + |
| 92 | + @FunctionalInterface |
| 93 | + public interface Invoker { |
| 94 | + <T, E extends Throwable> T invoke(Object subject, Object... args) throws E; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Find a method in given class with a given name and arguments. |
| 99 | + */ |
| 100 | + public static Optional<Invoker> findInvoker(final Class<?> clazz, final String name, Class<?>... args) { |
| 101 | + return findMethod(clazz, name, args) |
| 102 | + .map((Method method) -> new Invoker() { |
| 103 | + @Override |
| 104 | + public <T, E extends Throwable> T invoke(Object subject, final Object... args) throws E { |
| 105 | + try { |
| 106 | + return unchecked(method.invoke(subject, args)); |
| 107 | + } catch (Exception e) { |
| 108 | + SneakyThrow.throwSneaky(e); |
| 109 | + } |
| 110 | + return null; |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + private Reflect() { |
| 116 | + } |
| 117 | +} |
0 commit comments