|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appulse.utils; |
| 18 | + |
| 19 | +import static java.util.Optional.empty; |
| 20 | +import static java.util.Optional.ofNullable; |
| 21 | + |
| 22 | +import java.security.AccessController; |
| 23 | +import java.security.PrivilegedAction; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.function.Function; |
| 26 | +import java.util.function.Predicate; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import lombok.NonNull; |
| 30 | +import lombok.SneakyThrows; |
| 31 | +import lombok.val; |
| 32 | + |
| 33 | +/** |
| 34 | + * Different reflection helpers. |
| 35 | + * |
| 36 | + * @author Artem Labazin |
| 37 | + * @since 1.0.1 |
| 38 | + */ |
| 39 | +public final class ReflectionUtils { |
| 40 | + |
| 41 | + /** |
| 42 | + * Extracts field value from object by its name. |
| 43 | + * |
| 44 | + * @param obj object, from which need to extract field's value. |
| 45 | + * @param name field's name |
| 46 | + * |
| 47 | + * @return optional - field's value or nothing. |
| 48 | + */ |
| 49 | + @SneakyThrows |
| 50 | + public static Optional<Object> getFieldValueFrom (@NonNull Object obj, @NonNull String name) { |
| 51 | + val optional = findIn(obj, Class::getDeclaredFields, field -> field.getName().equals(name)); |
| 52 | + if (optional.isPresent()) { |
| 53 | + val field = optional.get(); |
| 54 | + AccessController.doPrivileged(new PrivilegedAction<Object>() { |
| 55 | + |
| 56 | + @Override |
| 57 | + public Object run () { |
| 58 | + field.setAccessible(true); |
| 59 | + return null; |
| 60 | + } |
| 61 | + }); |
| 62 | + val value = field.get(obj); |
| 63 | + return ofNullable(value); |
| 64 | + } |
| 65 | + return empty(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Invoke method of object with specified arguments. |
| 70 | + * |
| 71 | + * @param obj object |
| 72 | + * @param name method's name |
| 73 | + * @param arguments optional arguments for method |
| 74 | + * |
| 75 | + * @return method's invocation result. |
| 76 | + */ |
| 77 | + @SneakyThrows |
| 78 | + public static Object invokeMethodOf (@NonNull Object obj, @NonNull String name, Object... arguments) { |
| 79 | + val optional = findIn(obj, Class::getDeclaredMethods, method -> method.getName().equals(name)); |
| 80 | + if (optional.isPresent()) { |
| 81 | + val method = optional.get(); |
| 82 | + return method.invoke(obj, arguments); |
| 83 | + } |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + // Searches method/field in object by predicate. |
| 88 | + private static <T> Optional<T> findIn (Object obj, Function<Class<?>, T[]> extractor, Predicate<T> predicate) { |
| 89 | + Class<?> current = obj.getClass(); |
| 90 | + |
| 91 | + while (current != null) { |
| 92 | + val values = extractor.apply(current); |
| 93 | + val optional = Stream.of(values) |
| 94 | + .filter(predicate) |
| 95 | + .findFirst(); |
| 96 | + |
| 97 | + if (optional.isPresent()) { |
| 98 | + return optional; |
| 99 | + } |
| 100 | + current = current.getSuperclass(); |
| 101 | + } |
| 102 | + return empty(); |
| 103 | + } |
| 104 | + |
| 105 | + private ReflectionUtils () { |
| 106 | + } |
| 107 | +} |
0 commit comments