Last active
          March 31, 2018 17:57 
        
      - 
      
- 
        Save DarkSeraphim/4432569613dac8593c2e1fa2c254138e to your computer and use it in GitHub Desktop. 
    ReflectionResolver fanciness
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class ReflectionResolver { | |
| public static Field resolveField(Class<?> cls, Predicate<Field> filter) { | |
| resolveField(cls, filter, ONE_OR_ERROR); | |
| } | |
| public static Method resolveMethod(Class<?> cls, Predicate<Method> filter) { | |
| resolveMethod(cls, filter, ONE_OR_ERROR); | |
| } | |
| public static Constructor<?> resolveConstructor(Class<?> cls, Predicate<Constructor> filter) { | |
| resolveConstructor(cls, filter, ONE_OR_ERROR); | |
| } | |
| public static Class<?> resolveClass(String name) { | |
| } | |
| public static Field resolveField(Class<?> cls, Predicate<Field> filter, ResolutionPolicy policy) { | |
| } | |
| public static Method resolveMethod(Class<?> cls, Predicate<Method> filter, ResolutionPolicy policy) { | |
| } | |
| public static Constructor<?> resolveConstructor(Class<?> cls, Predicate<Method> filter, ResolutionPolicy policy) { | |
| } | |
| public static Predicate<Method> returns(Class<?> returnType) { | |
| return method -> method.getReturnType().isAssignableFrom(returnType); | |
| } | |
| public static Predicate<Executable> hasParameters(Class<?>... params) { | |
| return method -> { | |
| Class<?>[] actual = method.getParameterTypes(); | |
| if (params.length != actual.length) { | |
| return false; | |
| } | |
| // Optionally: support primitives | |
| for (int i = 0; i < actual.length; i++) { | |
| if (!actual.isAssignableFrom(params[i])) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }; | |
| } | |
| public static Predicate<Member> hasName(String name) { | |
| return hasName(name, false); | |
| } | |
| public static Predicate<Member> hasName(String name, boolean ignoreCase) { | |
| if (ignoreCase) { | |
| return member -> member.getName().equalsIgnoreCase(name); | |
| } else { | |
| return return member -> member.getName().equals(name); | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | @FunctionalInterface | |
| public interface ResolutionPolicy extends Predicate<List<? super AccessibleObject>> { | |
| ResolutionPolicy PICK_FIRST = list -> list.isEmpty() ? null : list.get(0); | |
| ResolutionPolicy ONE_OR_ERROR = list -> { | |
| if (list.size() == 1) { | |
| return list.get(0); | |
| } | |
| return null; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment