Best Powermock code snippet using org.powermock.reflect.internal.WhiteboxImpl.convertParameterTypesToPrimitive
Source:WhiteboxImpl.java
...1843 for (Method method : methods) {1844 final Class<?>[] parameterTypes = method.getParameterTypes();1845 if (checkIfParameterTypesAreSame(method.isVarArgs(), expectedTypes, parameterTypes)1846 || (!exactParameterTypeMatch && checkIfParameterTypesAreSame(method.isVarArgs(),1847 convertParameterTypesToPrimitive(expectedTypes), parameterTypes))) {1848 matchingArgumentTypes.add(method);1849 }1850 }1851 final Method[] methodArray = matchingArgumentTypes.toArray(new Method[0]);1852 if (methodArray.length == 0) {1853 throw new MethodNotFoundException(String.format(1854 "No methods matching the name(s) %s were found in the class hierarchy of %s.",1855 concatenateStrings(methodName), getType(clazz)));1856 }1857 return matchingArgumentTypes.toArray(new Method[matchingArgumentTypes.size()]);1858 }1859 /**1860 * Get an array of {@link Field}'s that matches the supplied list of field1861 * names. Both instance and static fields are taken into account.1862 *1863 * @param clazz1864 * The class that should contain the fields.1865 * @param fieldNames1866 * Names of the fields that will be returned.1867 * @return An array of Field's. May be of length 0 but not .1868 */1869 public static Field[] getFields(Class<?> clazz, String... fieldNames) {1870 final List<Field> fields = new LinkedList<Field>();1871 for (Field field : getAllFields(clazz)) {1872 for (String fieldName : fieldNames) {1873 if (field.getName().equals(fieldName)) {1874 fields.add(field);1875 }1876 }1877 }1878 final Field[] fieldArray = fields.toArray(new Field[fields.size()]);1879 if (fieldArray.length == 0) {1880 throw new FieldNotFoundException(String.format(1881 "No fields matching the name(s) %s were found in the class hierarchy of %s.",1882 concatenateStrings(fieldNames), getType(clazz)));1883 }1884 return fieldArray;1885 }1886 /**1887 * Perform method invocation.1888 *1889 * @param <T>1890 * the generic type1891 * @param tested1892 * the tested1893 * @param methodToInvoke1894 * the method to invoke1895 * @param arguments1896 * the arguments1897 * @return the t1898 * @throws Exception1899 * the exception1900 */1901 @SuppressWarnings("unchecked")1902 public static <T> T performMethodInvocation(Object tested, Method methodToInvoke, Object... arguments)1903 throws Exception {1904 final boolean accessible = methodToInvoke.isAccessible();1905 if (!accessible) {1906 methodToInvoke.setAccessible(true);1907 }1908 try {1909 if (isPotentialVarArgsMethod(methodToInvoke, arguments)) {1910 Class<?>[] parameterTypes = methodToInvoke.getParameterTypes();1911 final int varArgsIndex = parameterTypes.length - 1;1912 Class<?> varArgsType = parameterTypes[varArgsIndex].getComponentType();1913 Object varArgsArrayInstance = createAndPopulateVarArgsArray(varArgsType, varArgsIndex, arguments);1914 Object[] completeArgumentList = new Object[parameterTypes.length];1915 for (int i = 0; i < varArgsIndex; i++) {1916 completeArgumentList[i] = arguments[i];1917 }1918 completeArgumentList[completeArgumentList.length - 1] = varArgsArrayInstance;1919 return (T) methodToInvoke.invoke(tested, completeArgumentList);1920 } else {1921 return (T) methodToInvoke.invoke(tested, arguments == null ? new Object[] { arguments } : arguments);1922 }1923 } catch (InvocationTargetException e) {1924 Throwable cause = e.getCause();1925 if (cause instanceof Exception) {1926 throw (Exception) cause;1927 } else if (cause instanceof Error) {1928 throw (Error) cause;1929 } else {1930 throw new MethodInvocationException(cause);1931 }1932 } finally {1933 if (!accessible) {1934 methodToInvoke.setAccessible(false);1935 }1936 }1937 }1938 /**1939 * Gets the all method except.1940 *1941 * @param <T>1942 * the generic type1943 * @param type1944 * the type1945 * @param methodNames1946 * the method names1947 * @return the all method except1948 */1949 public static <T> Method[] getAllMethodExcept(Class<T> type, String... methodNames) {1950 List<Method> methodsToMock = new LinkedList<Method>();1951 Method[] methods = getAllMethods(type);1952 iterateMethods: for (Method method : methods) {1953 for (String methodName : methodNames) {1954 if (method.getName().equals(methodName)) {1955 continue iterateMethods;1956 }1957 }1958 methodsToMock.add(method);1959 }1960 return methodsToMock.toArray(new Method[0]);1961 }1962 /**1963 * Gets the all metods except.1964 *1965 * @param <T>1966 * the generic type1967 * @param type1968 * the type1969 * @param methodNameToExclude1970 * the method name to exclude1971 * @param argumentTypes1972 * the argument types1973 * @return the all metods except1974 */1975 public static <T> Method[] getAllMetodsExcept(Class<T> type, String methodNameToExclude, Class<?>[] argumentTypes) {1976 Method[] methods = getAllMethods(type);1977 List<Method> methodList = new ArrayList<Method>();1978 outer: for (Method method : methods) {1979 if (method.getName().equals(methodNameToExclude)) {1980 if (argumentTypes != null && argumentTypes.length > 0) {1981 final Class<?>[] args = method.getParameterTypes();1982 if (args != null && args.length == argumentTypes.length) {1983 for (int i = 0; i < args.length; i++) {1984 if (args[i].isAssignableFrom(getUnmockedType(argumentTypes[i]))) {1985 /*1986 * Method was not found thus it should not be1987 * mocked. Continue to investigate the next1988 * method.1989 */1990 continue outer;1991 }1992 }1993 }1994 } else {1995 continue;1996 }1997 }1998 methodList.add(method);1999 }2000 return methodList.toArray(new Method[0]);2001 }2002 /**2003 * Are all methods static.2004 *2005 * @param methods2006 * the methods2007 * @return true, if successful2008 */2009 public static boolean areAllMethodsStatic(Method... methods) {2010 for (Method method : methods) {2011 if (!Modifier.isStatic(method.getModifiers())) {2012 return false;2013 }2014 }2015 return true;2016 }2017 /**2018 * Check if all arguments are of the same type.2019 *2020 * @param arguments2021 * the arguments2022 * @return true, if successful2023 */2024 static boolean areAllArgumentsOfSameType(Object[] arguments) {2025 if (arguments == null || arguments.length <= 1) {2026 return true;2027 }2028 // Handle null values2029 int index = 0;2030 Object object = null;2031 while (object == null && index < arguments.length) {2032 object = arguments[index++];2033 }2034 if (object == null) {2035 return true;2036 }2037 // End of handling null values2038 final Class<?> firstArgumentType = getType(object);2039 for (int i = index; i < arguments.length; i++) {2040 final Object argument = arguments[i];2041 if (argument != null && !getType(argument).isAssignableFrom(firstArgumentType)) {2042 return false;2043 }2044 }2045 return true;2046 }2047 /**2048 * Check argument types match parameter types.2049 *2050 * @param isVarArgs2051 * If the last parameter is a var args.2052 * @param parameterTypes2053 * the parameter types2054 * @param arguments2055 * the arguments2056 * @return if all actual parameter types are assignable from the expected2057 * arguments, otherwise.2058 */2059 private static boolean checkArgumentTypesMatchParameterTypes(boolean isVarArgs, Class<?>[] parameterTypes,2060 Object[] arguments) {2061 if (parameterTypes == null) {2062 throw new IllegalArgumentException("parameter types cannot be null");2063 } else if (parameterTypes.length != arguments.length) {2064 return false;2065 }2066 for (int i = 0; i < parameterTypes.length; i++) {2067 Object argument = arguments[i];2068 if (argument == null) {2069 continue;2070 } else {2071 final boolean assignableFrom;2072 final Class<?> argumentType = getType(argument);2073 if (isVarArgs && i == parameterTypes.length - 1) {2074 assignableFrom = parameterTypes[i].getComponentType().isAssignableFrom(2075 argumentType.isArray() ? argumentType.getComponentType() : argumentType);2076 } else {2077 assignableFrom = parameterTypes[i].isAssignableFrom(argumentType);2078 }2079 final boolean isClass = parameterTypes[i].equals(Class.class) && isClass(argument);2080 if (!assignableFrom && !isClass) {2081 return false;2082 }2083 }2084 }2085 return true;2086 }2087 /**2088 * Gets the type.2089 *2090 * @param object2091 * the object2092 * @return The type of the of an object.2093 */2094 public static Class<?> getType(Object object) {2095 Class<?> type = null;2096 if (isClass(object)) {2097 type = (Class<?>) object;2098 } else if (object != null) {2099 type = object.getClass();2100 }2101 return type == null ? null : getUnmockedType(type);2102 }2103 /**2104 * Get an inner class type.2105 *2106 * @param declaringClass2107 * The class in which the inner class is declared.2108 * @param name2109 * The unqualified name (simple name) of the inner class.2110 * @return The type.2111 * @throws ClassNotFoundException2112 * the class not found exception2113 */2114 @SuppressWarnings("unchecked")2115 public static Class<Object> getInnerClassType(Class<?> declaringClass, String name) throws ClassNotFoundException {2116 return (Class<Object>) Class.forName(declaringClass.getName() + "$" + name);2117 }2118 /**2119 * Get the type of a local inner class.2120 *2121 * @param declaringClass2122 * The class in which the local inner class is declared.2123 * @param occurrence2124 * The occurrence of the local class. For example if you have two2125 * local classes in the <code>declaringClass</code> you must pass2126 * in <code>1</code> if you want to get the type for the first2127 * one or <code>2</code> if you want the second one.2128 * @param name2129 * The unqualified name (simple name) of the local class.2130 * @return The type.2131 * @throws ClassNotFoundException2132 * the class not found exception2133 */2134 @SuppressWarnings("unchecked")2135 public static Class<Object> getLocalClassType(Class<?> declaringClass, int occurrence, String name)2136 throws ClassNotFoundException {2137 return (Class<Object>) Class.forName(declaringClass.getName() + "$" + occurrence + name);2138 }2139 /**2140 * Get the type of an anonymous inner class.2141 *2142 * @param declaringClass2143 * The class in which the anonymous inner class is declared.2144 * @param occurrence2145 * The occurrence of the anonymous inner class. For example if2146 * you have two anonymous inner classes classes in the2147 * <code>declaringClass</code> you must pass in <code>1</code> if2148 * you want to get the type for the first one or <code>2</code>2149 * if you want the second one.2150 * @return The type.2151 * @throws ClassNotFoundException2152 * the class not found exception2153 */2154 @SuppressWarnings("unchecked")2155 public static Class<Object> getAnonymousInnerClassType(Class<?> declaringClass, int occurrence)2156 throws ClassNotFoundException {2157 return (Class<Object>) Class.forName(declaringClass.getName() + "$" + occurrence);2158 }2159 /**2160 * Get all fields annotated with a particular annotation. This method2161 * traverses the class hierarchy when checking for the annotation.2162 *2163 * @param object2164 * The object to look for annotations. Note that if're you're2165 * passing an object only instance fields are checked, passing a2166 * class will only check static fields.2167 * @param annotation2168 * The annotation type to look for.2169 * @param additionalAnnotations2170 * Optionally more annotations to look for. If any of the2171 * annotations are associated with a particular field it will be2172 * added to the resulting <code>Set</code>.2173 * @return A set of all fields containing the particular annotation.2174 */2175 @SuppressWarnings("unchecked")2176 public static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation> annotation,2177 Class<? extends Annotation>... additionalAnnotations) {2178 Class<? extends Annotation>[] annotations = null;2179 if (additionalAnnotations == null || additionalAnnotations.length == 0) {2180 annotations = (Class<? extends Annotation>[]) new Class<?>[] { annotation };2181 } else {2182 annotations = (Class<? extends Annotation>[]) new Class<?>[additionalAnnotations.length + 1];2183 annotations[0] = annotation;2184 System.arraycopy(additionalAnnotations, 0, annotations, 1, additionalAnnotations.length);2185 }2186 return getFieldsAnnotatedWith(object, annotations);2187 }2188 /**2189 * Get all fields annotated with a particular annotation. This method2190 * traverses the class hierarchy when checking for the annotation.2191 *2192 * @param object2193 * The object to look for annotations. Note that if're you're2194 * passing an object only instance fields are checked, passing a2195 * class will only check static fields.2196 * @param annotationTypes2197 * The annotation types to look for2198 * @return A set of all fields containing the particular annotation(s).2199 * @since 1.32200 */2201 public static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation>[] annotationTypes) {2202 return findAllFieldsUsingStrategy(new FieldAnnotationMatcherStrategy(annotationTypes), object, true,2203 getType(object));2204 }2205 /**2206 * Get all fields assignable from a particular type. This method traverses2207 * the class hierarchy when checking for the type.2208 *2209 * @param object2210 * The object to look for type. Note that if're you're passing an2211 * object only instance fields are checked, passing a class will2212 * only check static fields.2213 * @param type2214 * The type to look for.2215 * @return A set of all fields of the particular type.2216 */2217 public static Set<Field> getFieldsOfType(Object object, Class<?> type) {2218 return findAllFieldsUsingStrategy(new AssignableFromFieldTypeMatcherStrategy(type), object, true,2219 getType(object));2220 }2221 /**2222 * Get all instance fields for a particular object. It returns all fields2223 * regardless of the field modifier and regardless of where in the class2224 * hierarchy a field is located.2225 *2226 * @param object2227 * The object whose instance fields to get.2228 * @return All instance fields in the hierarchy. All fields are set to2229 * accessible2230 */2231 public static Set<Field> getAllInstanceFields(Object object) {2232 return findAllFieldsUsingStrategy(new AllFieldsMatcherStrategy(), object, true, getType(object));2233 }2234 /**2235 * Get all static fields for a particular type.2236 *2237 * @param type2238 * The class whose static fields to get.2239 * @return All static fields in . All fields are set to accessible.2240 */2241 public static Set<Field> getAllStaticFields(Class<?> type) {2242 final Set<Field> fields = new LinkedHashSet<Field>();2243 final Field[] declaredFields = type.getDeclaredFields();2244 for (Field field : declaredFields) {2245 if (Modifier.isStatic(field.getModifiers())) {2246 field.setAccessible(true);2247 fields.add(field);2248 }2249 }2250 return fields;2251 }2252 /**2253 * Checks if is class.2254 *2255 * @param argument2256 * the argument2257 * @return true, if is class2258 */2259 public static boolean isClass(Object argument) {2260 return argument instanceof Class<?>;2261 }2262 /**2263 * Check if parameter types are same.2264 *2265 * @param isVarArgs2266 * Whether or not the method or constructor contains var args.2267 * @param expectedParameterTypes2268 * the expected parameter types2269 * @param actualParameterTypes2270 * the actual parameter types2271 * @return if all actual parameter types are assignable from the expected2272 * parameter types, otherwise.2273 */2274 private static boolean checkIfParameterTypesAreSame(boolean isVarArgs, Class<?>[] expectedParameterTypes,2275 Class<?>[] actualParameterTypes) {2276 if (expectedParameterTypes == null || actualParameterTypes == null) {2277 throw new IllegalArgumentException("parameter types cannot be null");2278 } else if (expectedParameterTypes.length != actualParameterTypes.length) {2279 return false;2280 } else {2281 for (int i = 0; i < expectedParameterTypes.length; i++) {2282 final Class<?> actualParameterType = getType(actualParameterTypes[i]);2283 if (isVarArgs && i == expectedParameterTypes.length - 12284 && actualParameterType.getComponentType().isAssignableFrom(expectedParameterTypes[i])) {2285 return true;2286 } else if (!actualParameterType.isAssignableFrom(expectedParameterTypes[i])) {2287 return false;2288 }2289 }2290 }2291 return true;2292 }2293 /**2294 * Gets the field.2295 *2296 * @param fieldName2297 * the field name2298 * @param where2299 * the where2300 * @return the field2301 */2302 private static Field getField(String fieldName, Class<?> where) {2303 if (where == null) {2304 throw new IllegalArgumentException("where cannot be null");2305 }2306 Field field = null;2307 try {2308 field = where.getDeclaredField(fieldName);2309 field.setAccessible(true);2310 } catch (NoSuchFieldException e) {2311 throw new FieldNotFoundException("Field '" + fieldName + "' was not found in class " + where.getName()2312 + ".");2313 }2314 return field;2315 }2316 /**2317 * Find field or throw exception.2318 *2319 * @param fieldType2320 * the field type2321 * @param where2322 * the where2323 * @return the field2324 */2325 private static Field findFieldOrThrowException(Class<?> fieldType, Class<?> where) {2326 if (fieldType == null || where == null) {2327 throw new IllegalArgumentException("fieldType and where cannot be null");2328 }2329 Field field = null;2330 for (Field currentField : where.getDeclaredFields()) {2331 currentField.setAccessible(true);2332 if (currentField.getType().equals(fieldType)) {2333 field = currentField;2334 break;2335 }2336 }2337 if (field == null) {2338 throw new FieldNotFoundException("Cannot find a field of type " + fieldType + "in where.");2339 }2340 return field;2341 }2342 /**2343 * Sets the field.2344 *2345 * @param object2346 * the object2347 * @param value2348 * the value2349 * @param foundField2350 * the found field2351 */2352 private static void setField(Object object, Object value, Field foundField) {2353 foundField.setAccessible(true);2354 try {2355 foundField.set(object, value);2356 } catch (IllegalAccessException e) {2357 throw new RuntimeException("Internal error: Failed to set field in method setInternalState.", e);2358 }2359 }2360 /**2361 * Concatenate strings.2362 *2363 * @param stringsToConcatenate2364 * the strings to concatenate2365 * @return the string2366 */2367 private static String concatenateStrings(String... stringsToConcatenate) {2368 StringBuilder builder = new StringBuilder();2369 final int stringsLength = stringsToConcatenate.length;2370 for (int i = 0; i < stringsLength; i++) {2371 if (i == stringsLength - 1 && stringsLength != 1) {2372 builder.append(" or ");2373 } else if (i != 0) {2374 builder.append(", ");2375 }2376 builder.append(stringsToConcatenate[i]);2377 }2378 return builder.toString();2379 }2380 /**2381 * Checks if is potential var args method.2382 *2383 * @param method2384 * the method2385 * @param arguments2386 * the arguments2387 * @return true, if is potential var args method2388 */2389 private static boolean isPotentialVarArgsMethod(Method method, Object[] arguments) {2390 return doesParameterTypesMatchForVarArgsInvocation(method.isVarArgs(), method.getParameterTypes(), arguments);2391 }2392 /**2393 * Checks if is potential var args constructor.2394 *2395 * @param constructor2396 * the constructor2397 * @param arguments2398 * the arguments2399 * @return true, if is potential var args constructor2400 */2401 private static boolean isPotentialVarArgsConstructor(Constructor<?> constructor, Object[] arguments) {2402 final Class<?>[] parameterTypes = constructor.getParameterTypes();2403 return doesParameterTypesMatchForVarArgsInvocation(constructor.isVarArgs(), parameterTypes, arguments);2404 }2405 /**2406 * Does parameter types match for var args invocation.2407 *2408 * @param isVarArgs2409 * the is var args2410 * @param parameterTypes2411 * the parameter types2412 * @param arguments2413 * the arguments2414 * @return true, if successful2415 */2416 private static boolean doesParameterTypesMatchForVarArgsInvocation(boolean isVarArgs, Class<?>[] parameterTypes,2417 Object[] arguments) {2418 if (isVarArgs && arguments != null && arguments.length >= 1 && parameterTypes != null2419 && parameterTypes.length >= 1) {2420 final Class<?> componentType = parameterTypes[parameterTypes.length - 1].getComponentType();2421 final Object lastArgument = arguments[arguments.length - 1];2422 if (lastArgument != null) {2423 final Class<?> firstArgumentTypeAsPrimitive = getTypeAsPrimitiveIfWrapped(lastArgument);2424 final Class<?> varArgsParameterTypeAsPrimitive = getTypeAsPrimitiveIfWrapped(componentType);2425 isVarArgs = isVarArgs && varArgsParameterTypeAsPrimitive.isAssignableFrom(firstArgumentTypeAsPrimitive);2426 }2427 }2428 return isVarArgs && areAllArgumentsOfSameType(arguments);2429 }2430 /**2431 * Get the type of an object and convert it to primitive if the type has a2432 * primitive counter-part. E.g. if object is an instance of2433 * <code>java.lang.Integer</code> this method will return2434 * <code>int.class</code>.2435 *2436 * @param object2437 * The object whose type to get.2438 * @return the type as primitive if wrapped2439 */2440 private static Class<?> getTypeAsPrimitiveIfWrapped(Object object) {2441 if (object != null) {2442 final Class<?> firstArgumentType = getType(object);2443 final Class<?> firstArgumentTypeAsPrimitive = PrimitiveWrapper.hasPrimitiveCounterPart(firstArgumentType) ? PrimitiveWrapper2444 .getPrimitiveFromWrapperType(firstArgumentType) : firstArgumentType;2445 return firstArgumentTypeAsPrimitive;2446 }2447 return null;2448 }2449 /**2450 * Set the values of multiple instance fields defined in a context using2451 * reflection. The values in the context will be assigned to values on the2452 * <code>instance</code>. This method will traverse the class hierarchy when2453 * searching for the fields. Example usage:2454 * <p>2455 * Given:2456 *2457 * <pre>2458 * public class MyContext {2459 * private String myString = "myString";2460 * protected int myInt = 9;2461 * }2462 *2463 * public class MyInstance {2464 * private String myInstanceString;2465 * private int myInstanceInt;2466 *2467 * }2468 * </pre>2469 *2470 * then2471 *2472 * <pre>2473 * Whitebox.setInternalStateFromContext(new MyInstance(), new MyContext());2474 * </pre>2475 *2476 * will set the instance variables of <code>myInstance</code> to the values2477 * specified in <code>MyContext</code>.2478 *2479 * @param object2480 * the object2481 * @param context2482 * The context where the fields are defined.2483 * @param additionalContexts2484 * Optionally more additional contexts.2485 */2486 public static void setInternalStateFromContext(Object object, Object context, Object[] additionalContexts) {2487 setInternalStateFromContext(object, context, FieldMatchingStrategy.MATCHING);2488 if (additionalContexts != null && additionalContexts.length > 0) {2489 for (Object additionContext : additionalContexts) {2490 setInternalStateFromContext(object, additionContext, FieldMatchingStrategy.MATCHING);2491 }2492 }2493 }2494 public static void setInternalStateFromContext(Object object, Object context, FieldMatchingStrategy strategy) {2495 if (isClass(context)) {2496 copyState(object, getType(context), strategy);2497 } else {2498 copyState(object, context, strategy);2499 }2500 }2501 /**2502 * Set the values of multiple static fields defined in a context using2503 * reflection. The values in the context will be assigned to values on the2504 * <code>classOrInstance</code>. This method will traverse the class2505 * hierarchy when searching for the fields. Example usage:2506 * <p>2507 * Given:2508 *2509 * <pre>2510 * public class MyContext {2511 * private static String myString = "myString";2512 * protected static int myInt = 9;2513 * }2514 *2515 * public class MyInstance {2516 * private static String myInstanceString;2517 * private static int myInstanceInt;2518 *2519 * }2520 * </pre>2521 *2522 * then2523 *2524 * <pre>2525 * Whitebox.setInternalStateFromContext(MyInstance.class, MyContext.class);2526 * </pre>2527 *2528 * will set the static variables of <code>MyInstance</code> to the values2529 * specified in <code>MyContext</code>.2530 *2531 * @param object2532 * the object2533 * @param context2534 * The context where the fields are defined.2535 * @param additionalContexts2536 * Optionally more additional contexts.2537 */2538 public static void setInternalStateFromContext(Object object, Class<?> context, Class<?>[] additionalContexts) {2539 setInternalStateFromContext(object, context, FieldMatchingStrategy.MATCHING);2540 if (additionalContexts != null && additionalContexts.length > 0) {2541 for (Class<?> additionContext : additionalContexts) {2542 setInternalStateFromContext(object, additionContext, FieldMatchingStrategy.MATCHING);2543 }2544 }2545 }2546 /**2547 * Copy state.2548 *2549 * @param object2550 * the object2551 * @param context2552 * the context2553 * @param strategy2554 * The field matching strategy.2555 */2556 static void copyState(Object object, Object context, FieldMatchingStrategy strategy) {2557 if (object == null) {2558 throw new IllegalArgumentException("object to set state cannot be null");2559 } else if (context == null) {2560 throw new IllegalArgumentException("context cannot be null");2561 } else if (strategy == null) {2562 throw new IllegalArgumentException("strategy cannot be null");2563 }2564 Set<Field> allFields = isClass(context) ? getAllStaticFields(getType(context)) : getAllInstanceFields(context);2565 for (Field field : allFields) {2566 try {2567 final boolean isStaticField = Modifier.isStatic(field.getModifiers());2568 setInternalState(isStaticField ? getType(object) : object, field.getType(), field.get(context));2569 } catch (FieldNotFoundException e) {2570 if (strategy == FieldMatchingStrategy.STRICT) {2571 throw e;2572 }2573 } catch (IllegalAccessException e) {2574 // Should never happen2575 throw new RuntimeException(2576 "Internal Error: Failed to get the field value in method setInternalStateFromContext.", e);2577 }2578 }2579 }2580 /**2581 * Assert object in get internal state is not null.2582 *2583 * @param object2584 * the object2585 */2586 private static void assertObjectInGetInternalStateIsNotNull(Object object) {2587 if (object == null) {2588 throw new IllegalArgumentException("The object containing the field cannot be null");2589 }2590 }2591 /**2592 * Convert parameter types to primitive.2593 *2594 * @param parameterTypes2595 * the parameter types2596 * @return the class[]2597 */2598 private static Class<?>[] convertParameterTypesToPrimitive(Class<?>[] parameterTypes) {2599 Class<?>[] converted = new Class<?>[parameterTypes.length];2600 for (int i = 0; i < parameterTypes.length; i++) {2601 Class<?> primitiveWrapperType = PrimitiveWrapper.getPrimitiveFromWrapperType(parameterTypes[i]);2602 if (primitiveWrapperType == null) {2603 converted[i] = parameterTypes[i];2604 } else {2605 converted[i] = primitiveWrapperType;2606 }2607 }2608 return converted;2609 }2610}...
convertParameterTypesToPrimitive
Using AI Code Generation
1import org.powermock.reflect.internal.WhiteboxImpl;2public class Main {3 public static void main(String[] args) {4 Class<?>[] classes = {Integer.class, String.class};5 Class<?>[] primitiveClasses = WhiteboxImpl.convertParameterTypesToPrimitive(classes);6 for (Class<?> clazz : primitiveClasses) {7 System.out.println(clazz);8 }9 }10}
convertParameterTypesToPrimitive
Using AI Code Generation
1WhiteboxImpl whiteboxImpl = new WhiteboxImpl();2whiteboxImpl.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});3Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});4Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});5Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});6Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});7Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});8Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});9Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});10Whitebox.convertParameterTypesToPrimitive(new Class[]{String.class, Integer.class, Long.class, Double.class, Float.class, Boolean.class, Character.class, Byte.class, Short.class});
convertParameterTypesToPrimitive
Using AI Code Generation
1import org.powermock.reflect.internal.WhiteboxImpl;2public class ConvertParameterTypesToPrimitiveTest {3 public static void main(String[] args) {4 Class<?>[] classes = { String.class, Integer.class, int.class };5 Class<?>[] primitiveClasses = WhiteboxImpl.convertParameterTypesToPrimitive(classes);6 for (Class<?> primitiveClass : primitiveClasses) {7 System.out.println(primitiveClass);8 }9 }10}
convertParameterTypesToPrimitive
Using AI Code Generation
1 import org.powermock.reflect.internal.WhiteboxImpl;2 import java.lang.reflect.Method;3 public class ConvertParameterTypesToPrimitiveMethodOfWhiteboxImpl {4 public static void main(String[] args) throws Exception {5 Method method = ConvertParameterTypesToPrimitiveMethodOfWhiteboxImpl.class.getMethod("foo", String.class, Integer.class);6 Class<?>[] parameterTypes = WhiteboxImpl.convertParameterTypesToPrimitive(method);7 for (Class<?> parameterType : parameterTypes) {8 System.out.println(parameterType);9 }10 }11 public void foo(String s, Integer i) {12 }13 }14### [convertParameterTypesToPrimitive method of org.powermock.reflect.internal.WhiteboxImpl class](www.codepile.net/pile/6LjK8zY...) 15import org.powermock.reflect.internal.WhiteboxImpl;16import java.lang.reflect.Method;17public class ConvertParameterTypesToPrimitiveMethodOfWhiteboxImpl { 18public static void main(String[] args) throws Exception { 19Method method = ConvertParameterTypesToPrimitiveMethodOfWhiteboxImpl.class.getMethod(“foo”, String.class, Integer.class); 20Class<?>[] parameterTypes = WhiteboxImpl.convertParameterTypesToPrimitive(method); 21for (Class<?> parameterType : parameterTypes) { 22System.out.println(parameterType); 23} 24}25public void foo(String s, Integer i) { 26} 27}28### [convertParameterTypesToPrimitive method of org.powermock.reflect.internal.WhiteboxImpl class](www.codepile.net/pile/6LjK8zY...) 29import org.powermock.reflect.internal.WhiteboxImpl;30import java.lang.reflect.Method;31public class ConvertParameterTypesToPrimitiveMethodOfWhiteboxImpl {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!