1 | package com.github.valid8j.pcond.core.refl; | |
2 | ||
3 | import com.github.valid8j.pcond.internals.MethodAmbiguous; | |
4 | import com.github.valid8j.pcond.internals.MethodInvocationException; | |
5 | import com.github.valid8j.pcond.internals.InternalUtils; | |
6 | import com.github.valid8j.pcond.internals.MethodNotFound; | |
7 | ||
8 | import java.lang.reflect.InvocationTargetException; | |
9 | import java.lang.reflect.Method; | |
10 | import java.util.*; | |
11 | import java.util.function.BinaryOperator; | |
12 | import java.util.function.Function; | |
13 | import java.util.function.Predicate; | |
14 | import java.util.function.Supplier; | |
15 | import java.util.stream.Collector; | |
16 | ||
17 | import static java.lang.String.format; | |
18 | import static java.util.Arrays.asList; | |
19 | import static java.util.Objects.requireNonNull; | |
20 | import static java.util.stream.Collectors.*; | |
21 | ||
22 | /** | |
23 | * This class consists of {@code static} utility methods for creating printable functions and predicate | |
24 | * on objects. | |
25 | */ | |
26 | public enum ReflUtils { | |
27 | ; | |
28 | ||
29 | public static final Map<Class<?>, Set<Class<?>>> WIDER_TYPES = new HashMap<Class<?>, Set<Class<?>>>() { | |
30 | { | |
31 | // https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 | |
32 | put(InternalUtils.wrapperClassOf(byte.class), wrapperClassesOf(asSet(short.class, int.class, long.class, float.class, double.class))); | |
33 | put(InternalUtils.wrapperClassOf(short.class), wrapperClassesOf(asSet(int.class, long.class, float.class, double.class))); | |
34 | put(InternalUtils.wrapperClassOf(char.class), wrapperClassesOf(asSet(int.class, long.class, float.class, double.class))); | |
35 | put(InternalUtils.wrapperClassOf(int.class), wrapperClassesOf(asSet(long.class, float.class, double.class))); | |
36 | put(InternalUtils.wrapperClassOf(long.class), wrapperClassesOf(asSet(float.class, double.class))); | |
37 | put(InternalUtils.wrapperClassOf(float.class), wrapperClassesOf(asSet(double.class))); | |
38 | } | |
39 | ||
40 | private Set<Class<?>> wrapperClassesOf(Set<Class<?>> primitiveClasses) { | |
41 |
1
1. wrapperClassesOf : replaced return value with Collections.emptySet for com/github/valid8j/pcond/core/refl/ReflUtils$1::wrapperClassesOf → SURVIVED |
return primitiveClasses.stream().map(InternalUtils::wrapperClassOf).collect(toSet()); |
42 | } | |
43 | ||
44 | private Set<Class<?>> asSet(Class<?>... classes) { | |
45 |
1
1. asSet : replaced return value with Collections.emptySet for com/github/valid8j/pcond/core/refl/ReflUtils$1::asSet → SURVIVED |
return new HashSet<Class<?>>() {{ |
46 | addAll(asList(classes)); | |
47 | }}; | |
48 | } | |
49 | }; | |
50 | ||
51 | /** | |
52 | * Invokes a method found by {@code methodQuery}. | |
53 | * All parameters in the query needs to be bound before calling this method. | |
54 | * When a query matches no or more than one methods, an exception will be thrown. | |
55 | * | |
56 | * If an exception is thrown by the method, it will be wrapped by {@link RuntimeException} and re-thrown. | |
57 | * | |
58 | * @param methodQuery A query that speficies the method to be executed. | |
59 | * @param <R> Type of the returned value. | |
60 | * @return The value returned from the method found by the query. | |
61 | * @see MethodQuery | |
62 | * @see ReflUtils#findMethod(Class, String, Object[]) | |
63 | */ | |
64 | public static <R> R invokeMethod(MethodQuery methodQuery) { | |
65 |
1
1. invokeMethod : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::invokeMethod → KILLED |
return invokeMethod( |
66 | findMethod(methodQuery.targetClass(), methodQuery.methodName(), methodQuery.arguments()), | |
67 | methodQuery.targetObject(), | |
68 | methodQuery.arguments()); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Invokes a given {@code method} on the object with arguments passed as {@code obj} and {@code arguments}. | |
73 | * | |
74 | * @param method A method to be invoked. | |
75 | * @param obj An object on which the {@code method} is invoked. | |
76 | * @param arguments Arguments passed to the {@code method}. | |
77 | * @param <R> The type of the value returned from the {@code method}. | |
78 | * @return The value returned by {@code method}. | |
79 | */ | |
80 | @SuppressWarnings("unchecked") | |
81 | public static <R> R invokeMethod(Method method, Object obj, Object[] arguments) { | |
82 | boolean wasAccessible = method.isAccessible(); | |
83 | try { | |
84 | //// | |
85 | // Issue-42 | |
86 | // Without setting accessible, a public method defined in a private class | |
87 | // overriding a public method cannot be invoked. | |
88 |
1
1. invokeMethod : removed call to java/lang/reflect/Method::setAccessible → KILLED |
method.setAccessible(true); |
89 |
1
1. invokeMethod : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::invokeMethod → KILLED |
return (R) method.invoke(obj, arguments); |
90 | } catch (IllegalAccessException | InvocationTargetException e) { | |
91 | throw new MethodInvocationException(format("Method invocation of '%s' was failed", method), e.getCause()); | |
92 | } finally { | |
93 |
1
1. invokeMethod : removed call to java/lang/reflect/Method::setAccessible → SURVIVED |
method.setAccessible(wasAccessible); |
94 | } | |
95 | } | |
96 | ||
97 | /** | |
98 | * Tries to find a method whose name is {@code methodName} from a given class {@code aClass} | |
99 | * and that can be invoked with parameter values {@code args}. | |
100 | * | |
101 | * Unless one and only one method is found appropriate, an exception will be | |
102 | * thrown. | |
103 | * | |
104 | * In this version, boxing/unboxing and casting are not attempted to determine | |
105 | * the methodto be returned during the search. This means, if there are overloaded | |
106 | * methods of the {@code methodName} that can be invoked with {@code args}, this | |
107 | * method will fail. Also even if there is a method of the {@code methodName} | |
108 | * that can be invoked if boxing/unboxing happens, this method will fail. | |
109 | * | |
110 | * @param aClass A class from which the method is searched. | |
111 | * @param methodName A name of the method | |
112 | * @param args Arguments which should be given to the method | |
113 | * @return A method for given class {@code aClass}, {@code method}, and {@code args}. | |
114 | */ | |
115 | public static Method findMethod(Class<?> aClass, String methodName, Object[] args) { | |
116 | MethodSelector methodSelector = new MethodSelector.Default() | |
117 | .andThen(new MethodSelector.PreferNarrower()) | |
118 | .andThen(new MethodSelector.PreferExact()); | |
119 |
1
1. findMethod : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::findMethod → KILLED |
return getIfOnlyOneElseThrow( |
120 | methodSelector.select( | |
121 | Arrays.stream(aClass.getMethods()) | |
122 |
2
1. lambda$findMethod$0 : replaced boolean return with false for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$findMethod$0 → KILLED 2. lambda$findMethod$0 : replaced boolean return with true for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$findMethod$0 → KILLED |
.filter((Method m) -> m.getName().equals(methodName)) |
123 | .collect(toMethodList()), | |
124 | args), | |
125 |
1
1. lambda$findMethod$1 : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$findMethod$1 → KILLED |
() -> exceptionOnAmbiguity(aClass, methodName, args, methodSelector), |
126 |
1
1. lambda$findMethod$2 : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$findMethod$2 → KILLED |
() -> exceptionOnMethodNotFound(aClass, methodName, args, methodSelector)); |
127 | } | |
128 | ||
129 | private static RuntimeException exceptionOnMethodNotFound(Class<?> aClass, String methodName, Object[] args, MethodSelector methodSelector) { | |
130 |
1
1. exceptionOnMethodNotFound : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::exceptionOnMethodNotFound → KILLED |
return new MethodNotFound(format( |
131 | "Method matching '%s%s' was not found by selector=%s in %s.", | |
132 | methodName, | |
133 | asList(args), | |
134 | methodSelector, | |
135 | aClass.getCanonicalName() | |
136 | )); | |
137 | } | |
138 | ||
139 | private static RuntimeException exceptionOnAmbiguity(Class<?> aClass, String methodName, Object[] args, MethodSelector methodSelector) { | |
140 |
1
1. exceptionOnAmbiguity : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::exceptionOnAmbiguity → KILLED |
return new MethodAmbiguous(format( |
141 | "Methods matching '%s%s' were found more than one in %s by selector=%s.: %s ", | |
142 | methodName, | |
143 | asList(args), | |
144 | aClass.getCanonicalName(), | |
145 | methodSelector, | |
146 | summarizeMethods(methodSelector.select( | |
147 | Arrays.stream(aClass.getMethods()) | |
148 |
2
1. lambda$exceptionOnAmbiguity$3 : replaced boolean return with true for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$exceptionOnAmbiguity$3 → SURVIVED 2. lambda$exceptionOnAmbiguity$3 : replaced boolean return with false for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$exceptionOnAmbiguity$3 → KILLED |
.filter((Method m) -> m.getName().equals(methodName)) |
149 | .collect(toMethodList()), | |
150 | args)))); | |
151 | } | |
152 | ||
153 | static Class<?> targetTypeOf(Object targetObject) { | |
154 | requireNonNull(targetObject); | |
155 |
2
1. targetTypeOf : negated conditional → SURVIVED 2. targetTypeOf : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::targetTypeOf → KILLED |
return targetObject instanceof Parameter ? |
156 | Object.class : | |
157 | targetObject.getClass(); | |
158 | } | |
159 | ||
160 | /** | |
161 | * A collector to gather methods which have narrowest possible signatures. | |
162 | * | |
163 | * @return A collector. | |
164 | */ | |
165 | private static Collector<Method, List<Method>, List<Method>> toMethodList() { | |
166 |
1
1. toMethodList : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::toMethodList → KILLED |
return Collector.of( |
167 | LinkedList::new, | |
168 | ReflUtils::addMethodIfNecessary, | |
169 | createCombinerForMethodList()); | |
170 | } | |
171 | ||
172 | /** | |
173 | * This method is made public in order only for unit testing since with Java 8, | |
174 | * the combiner returned by this method will never be used. | |
175 | * - https://stackoverflow.com/questions/29210176/can-a-collectors-combiner-function-ever-be-used-on-sequential-streams[Can a Collector's combiner function ever be used on sequential streams?] | |
176 | * | |
177 | * @return A combiner for method list. | |
178 | */ | |
179 | public static BinaryOperator<List<Method>> createCombinerForMethodList() { | |
180 |
1
1. createCombinerForMethodList : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::createCombinerForMethodList → KILLED |
return new BinaryOperator<List<Method>>() { |
181 | @Override | |
182 | public List<Method> apply(List<Method> methods, List<Method> methods2) { | |
183 |
1
1. apply : replaced return value with Collections.emptyList for com/github/valid8j/pcond/core/refl/ReflUtils$2::apply → KILLED |
return new LinkedList<Method>() {{ |
184 | addAll(methods); | |
185 |
2
1. <init> : removed call to java/util/List::forEach → KILLED 2. lambda$new$0 : removed call to com/github/valid8j/pcond/core/refl/ReflUtils::access$000 → KILLED |
methods2.forEach(each -> addMethodIfNecessary(this, each)); |
186 | }}; | |
187 | } | |
188 | }; | |
189 | } | |
190 | ||
191 | private static Method getIfOnlyOneElseThrow(List<Method> foundMethods, Supplier<RuntimeException> exceptionSupplierOnAmbiguity, Supplier<RuntimeException> exceptionSupplierOnNotFound) { | |
192 |
1
1. getIfOnlyOneElseThrow : negated conditional → KILLED |
if (foundMethods.isEmpty()) |
193 | throw exceptionSupplierOnNotFound.get(); | |
194 |
1
1. getIfOnlyOneElseThrow : negated conditional → KILLED |
if (foundMethods.size() == 1) |
195 |
1
1. getIfOnlyOneElseThrow : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::getIfOnlyOneElseThrow → KILLED |
return foundMethods.get(0); |
196 | throw exceptionSupplierOnAmbiguity.get(); | |
197 | } | |
198 | ||
199 | private static List<String> summarizeMethods(List<Method> methods) { | |
200 |
1
1. summarizeMethods : replaced return value with Collections.emptyList for com/github/valid8j/pcond/core/refl/ReflUtils::summarizeMethods → KILLED |
return methods |
201 | .stream() | |
202 | .map(ReflUtils::summarizeMethodName) | |
203 | .collect(toList()); | |
204 | } | |
205 | ||
206 | private static String summarizeMethodName(Method method) { | |
207 |
1
1. summarizeMethodName : replaced return value with "" for com/github/valid8j/pcond/core/refl/ReflUtils::summarizeMethodName → KILLED |
return method.toString().replace( |
208 | method.getDeclaringClass().getCanonicalName() + "." + method.getName(), | |
209 | method.getName()); | |
210 | } | |
211 | ||
212 | /** | |
213 | * Add {@code method} to {@code methods} if necessary. | |
214 | * Since {@link Class#getMethods()} may return methods of the same signature when | |
215 | * a method is overridden in a sub-class with returning a narrow class in the super, | |
216 | * this consideration is necessary. | |
217 | * | |
218 | * @param methods A list of methods | |
219 | * @param method A method to be examined if it is necessay to be added to {@code methods}. | |
220 | */ | |
221 | private static void addMethodIfNecessary(List<Method> methods, Method method) { | |
222 | Optional<Method> found = methods | |
223 | .stream() | |
224 |
2
1. lambda$addMethodIfNecessary$4 : replaced boolean return with false for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$addMethodIfNecessary$4 → KILLED 2. lambda$addMethodIfNecessary$4 : replaced boolean return with true for com/github/valid8j/pcond/core/refl/ReflUtils::lambda$addMethodIfNecessary$4 → KILLED |
.filter(each -> Arrays.equals(each.getParameterTypes(), method.getParameterTypes())) |
225 | .findAny(); | |
226 |
1
1. addMethodIfNecessary : negated conditional → KILLED |
if (found.isPresent()) { |
227 |
1
1. addMethodIfNecessary : negated conditional → KILLED |
if (found.get().getDeclaringClass().isAssignableFrom(method.getDeclaringClass())) |
228 | methods.remove(found.get()); | |
229 | } | |
230 | methods.add(method); | |
231 | } | |
232 | ||
233 | static Object replacePlaceHolderWithActualArgument(Object object, Predicate<Object> isPlaceHolder, Function<Object, Object> replace) { | |
234 |
1
1. replacePlaceHolderWithActualArgument : negated conditional → KILLED |
if (isPlaceHolder.test(object)) { |
235 |
1
1. replacePlaceHolderWithActualArgument : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::replacePlaceHolderWithActualArgument → KILLED |
return replace.apply(object); |
236 | } | |
237 |
1
1. replacePlaceHolderWithActualArgument : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::replacePlaceHolderWithActualArgument → KILLED |
return object; |
238 | } | |
239 | ||
240 | @SuppressWarnings("unchecked") | |
241 | public static <R> R invokeStaticMethod(Method method, Object[] args) { | |
242 | try { | |
243 |
1
1. invokeStaticMethod : replaced return value with null for com/github/valid8j/pcond/core/refl/ReflUtils::invokeStaticMethod → KILLED |
return (R) method.invoke(null, args); |
244 | } catch (IllegalAccessException | InvocationTargetException e) { | |
245 | throw InternalUtils.executionFailure( | |
246 |
1
1. invokeStaticMethod : negated conditional → KILLED |
format("Invoked method:%s threw an exception", formatMethodName(method)), |
247 | e instanceof InvocationTargetException ? e.getCause() : e); | |
248 | } | |
249 | } | |
250 | ||
251 | public static String formatMethodName(Method method) { | |
252 |
1
1. formatMethodName : replaced return value with "" for com/github/valid8j/pcond/core/refl/ReflUtils::formatMethodName → KILLED |
return format("%s.%s(%s)", |
253 | method.getDeclaringClass().getName(), | |
254 | method.getName(), | |
255 | Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName).collect(joining(","))); | |
256 | } | |
257 | } | |
Mutations | ||
41 |
1.1 |
|
45 |
1.1 |
|
65 |
1.1 |
|
88 |
1.1 |
|
89 |
1.1 |
|
93 |
1.1 |
|
119 |
1.1 |
|
122 |
1.1 2.2 |
|
125 |
1.1 |
|
126 |
1.1 |
|
130 |
1.1 |
|
140 |
1.1 |
|
148 |
1.1 2.2 |
|
155 |
1.1 2.2 |
|
166 |
1.1 |
|
180 |
1.1 |
|
183 |
1.1 |
|
185 |
1.1 2.2 |
|
192 |
1.1 |
|
194 |
1.1 |
|
195 |
1.1 |
|
200 |
1.1 |
|
207 |
1.1 |
|
224 |
1.1 2.2 |
|
226 |
1.1 |
|
227 |
1.1 |
|
234 |
1.1 |
|
235 |
1.1 |
|
237 |
1.1 |
|
243 |
1.1 |
|
246 |
1.1 |
|
252 |
1.1 |