|
24 | 24 | */ |
25 | 25 | class InvocationSensor<T> { |
26 | 26 |
|
27 | | - private T proxyObject; |
| 27 | + private T proxyObject; |
28 | 28 |
|
29 | | - private List<String> propertyNames = new LinkedList<>(); |
| 29 | + private List<String> propertyNames = new LinkedList<>(); |
30 | 30 |
|
31 | | - InvocationSensor(Class<T> superType) { |
32 | | - Class<? extends T> proxyClass = new ByteBuddy().subclass(superType) |
33 | | - .method(isGetter()) |
34 | | - .intercept(of((proxy, method, args) -> markPropertyAsCalled( |
35 | | - method))) |
36 | | - .method(isDeclaredBy(Object.class)) |
37 | | - .intercept(of((proxy, method, args) -> invokeMethodProxySafe( |
38 | | - method, |
39 | | - this, |
40 | | - args))) |
41 | | - .method(not(isGetter()).and(not(isDeclaredBy(Object.class)))) |
42 | | - .intercept(of((proxy, method, args) -> { |
43 | | - throw ReflectionException.notAGetter(method); |
44 | | - })) |
45 | | - .make() |
46 | | - .load(getClass().getClassLoader(), |
47 | | - ClassLoadingStrategy.Default.INJECTION) |
48 | | - .getLoaded(); |
| 31 | + InvocationSensor(Class<T> superType) { |
| 32 | + Class<? extends T> proxyClass = new ByteBuddy().subclass(superType) |
| 33 | + .method(isGetter()) |
| 34 | + .intercept(of((proxy, method, args) -> markPropertyAsCalled(method))) |
| 35 | + .method(isDeclaredBy(Object.class)) |
| 36 | + .intercept(of((proxy, method, args) -> invokeMethodProxySafe(method, this, args))) |
| 37 | + .method(not(isGetter()).and(not(isDeclaredBy(Object.class)))) |
| 38 | + .intercept(of((proxy, method, args) -> { |
| 39 | + throw ReflectionException.notAGetter(method); |
| 40 | + })) |
| 41 | + .make() |
| 42 | + .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.INJECTION) |
| 43 | + .getLoaded(); |
49 | 44 |
|
50 | | - proxyObject = superType.cast(ReflectionUtil.newInstance(proxyClass)); |
51 | | - } |
| 45 | + proxyObject = superType.cast(ReflectionUtil.newInstance(proxyClass)); |
| 46 | + } |
52 | 47 |
|
53 | | - private Object markPropertyAsCalled(Method method) { |
54 | | - String propertyName = toPropertyName(method); |
55 | | - propertyNames.add(propertyName); |
56 | | - return nullOrDefaultValue(method.getReturnType()); |
57 | | - } |
| 48 | + private Object markPropertyAsCalled(Method method) { |
| 49 | + String propertyName = toPropertyName(method); |
| 50 | + propertyNames.add(propertyName); |
| 51 | + return nullOrDefaultValue(method.getReturnType()); |
| 52 | + } |
58 | 53 |
|
59 | | - /** |
60 | | - * Returns the proxy object get-method calls can be performed on. |
61 | | - * |
62 | | - * @return The proxy. |
63 | | - */ |
64 | | - T getSensor() { |
65 | | - return proxyObject; |
66 | | - } |
| 54 | + /** |
| 55 | + * Returns the proxy object get-method calls can be performed on. |
| 56 | + * |
| 57 | + * @return The proxy. |
| 58 | + */ |
| 59 | + T getSensor() { |
| 60 | + return proxyObject; |
| 61 | + } |
67 | 62 |
|
68 | | - /** |
69 | | - * Returns the list of property names that were tracked by get calls. |
70 | | - * |
71 | | - * @return Returns the tracked property names. |
72 | | - */ |
73 | | - List<String> getTrackedPropertyNames() { |
74 | | - return Collections.unmodifiableList(propertyNames); |
75 | | - } |
| 63 | + /** |
| 64 | + * Returns the list of property names that were tracked by get calls. |
| 65 | + * |
| 66 | + * @return Returns the tracked property names. |
| 67 | + */ |
| 68 | + List<String> getTrackedPropertyNames() { |
| 69 | + return Collections.unmodifiableList(propertyNames); |
| 70 | + } |
76 | 71 |
|
77 | | - /** |
78 | | - * Checks if there were any properties accessed by get calls. |
79 | | - * |
80 | | - * @return Returns <code>true</code> if there were at least one interaction with |
81 | | - * a property. Otherwise <code>false</code> is returned. |
82 | | - */ |
83 | | - boolean hasTrackedProperties() { |
84 | | - return !propertyNames.isEmpty(); |
85 | | - } |
| 72 | + /** |
| 73 | + * Checks if there were any properties accessed by get calls. |
| 74 | + * |
| 75 | + * @return Returns <code>true</code> if there were at least one interaction with |
| 76 | + * a property. Otherwise <code>false</code> is returned. |
| 77 | + */ |
| 78 | + boolean hasTrackedProperties() { |
| 79 | + return !propertyNames.isEmpty(); |
| 80 | + } |
86 | 81 |
|
87 | | - /** |
88 | | - * Resets all tracked information. |
89 | | - */ |
90 | | - void reset() { |
91 | | - propertyNames.clear(); |
92 | | - } |
| 82 | + /** |
| 83 | + * Resets all tracked information. |
| 84 | + */ |
| 85 | + void reset() { |
| 86 | + propertyNames.clear(); |
| 87 | + } |
93 | 88 |
|
94 | | - private static Object nullOrDefaultValue(Class<?> returnType) { |
95 | | - if (returnType.isPrimitive()) { |
96 | | - return defaultValue(returnType); |
97 | | - } else { |
98 | | - return null; |
99 | | - } |
| 89 | + private static Object nullOrDefaultValue(Class<?> returnType) { |
| 90 | + if (returnType.isPrimitive()) { |
| 91 | + return defaultValue(returnType); |
| 92 | + } else { |
| 93 | + return null; |
100 | 94 | } |
| 95 | + } |
101 | 96 | } |
0 commit comments