Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.reflect.reflect

File Reflect.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
15
7
1
67
57
12
0.8
2.14
7
1.71

Classes

Class Line # Actions
Reflect 16 15 12
0.916666791.7%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    package io.sunshower.reflect.reflect;
2   
3    import static org.springframework.aop.support.AopUtils.isAopProxy;
4    import static org.springframework.aop.support.AopUtils.isCglibProxy;
5    import static org.springframework.aop.support.AopUtils.isJdkDynamicProxy;
6   
7    import io.sunshower.lambda.Lazy;
8    import io.sunshower.lambda.Option;
9    import java.lang.reflect.Constructor;
10    import java.lang.reflect.InvocationTargetException;
11    import java.util.Arrays;
12    import java.util.function.Function;
13    import java.util.stream.Stream;
14    import org.springframework.aop.framework.Advised;
15   
 
16    public class Reflect {
17   
 
18  1 toggle private Reflect() {
19  1 throw new RuntimeException("No reflect instances for you!");
20    }
21   
 
22  5 toggle public static <T> Stream<T> collectOverHierarchy(
23    Class<?> clazz, Function<Class<?>, Stream<T>> f) {
24  5 return linearSupertypes(clazz)
25    .flatMap(i -> Stream.concat(Stream.of(i), Arrays.stream(i.getInterfaces())))
26    .flatMap(i -> f.apply(i));
27    }
28   
 
29  5 toggle public static <T> Stream<T> mapOverHierarchy(Class<?> type, Function<Class<?>, Option<T>> f) {
30  5 return collectOverHierarchy(type, cl -> f.apply(cl).stream());
31    }
32   
 
33  1 toggle public static boolean isProxy(Object o) {
34  1 return isJdkDynamicProxy(o) || isCglibProxy(o) || isAopProxy(o);
35    }
36   
 
37  1 toggle @SuppressWarnings("unchecked")
38    public static <T> T resolveProxied(Object proxy) throws Exception {
39  1 if (isProxy(proxy)) {
40  0 return (T) ((Advised) proxy).getTargetSource().getTarget();
41    } else {
42  1 return (T) proxy;
43    }
44    }
45   
 
46  6 toggle @SuppressWarnings("unchecked")
47    public static Stream<Class<?>> linearSupertypes(Class<?> a) {
48  6 return Lazy.takeWhile(Stream.iterate(a, Class::getSuperclass), i -> i != null);
49    }
50   
 
51  4 toggle @SuppressWarnings("unchecked")
52    public static <R> R instantiate(Class<R> aClass) {
53  4 try {
54  4 final Constructor<R> ctor = aClass.getDeclaredConstructor();
55  3 return ctor.newInstance();
56    } catch (NoSuchMethodException e) {
57  1 throw new InstantiationException("Class must declare a public, no-arg constructor");
58    } catch (IllegalAccessException e) {
59  1 throw new InstantiationException("Constructor must be public");
60    } catch (InvocationTargetException e) {
61  1 throw new InstantiationException("Constructor threw exception", e.getTargetException());
62    } catch (java.lang.InstantiationException e) {
63  1 throw new InstantiationException(
64    "Failed to instantiate class. " + "Did you pass an interface or abstract class?", e);
65    }
66    }
67    }