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