/** * Finds and returns the default constructor. If the constructor couldn't be * found this method delegates to {@link #throwExceptionWhenMultipleConstructorMatchesFound(java.lang.reflect.Constructor[])}. * * @param type The type where the constructor should be located. * @return The found constructor. * @throws ConstructorNotFoundException if too many constructors was found. */ public static Constructor<?> findDefaultConstructorOrThrowException(Class<?> type) throws ConstructorNotFoundException { if (type == null) { throw new IllegalArgumentException("type cannot be null"); } final Constructor<?> declaredConstructor; try { declaredConstructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new ConstructorNotFoundException(String.format("Couldn't find a default constructor in %s.", type.getName())); } return declaredConstructor; }
/** * Get the first parent constructor defined in a super class of * {@code klass}. * * @param klass The class where the constructor is located. {@code null} * ). * @return A . */ public static Constructor<?> getFirstParentConstructor(Class<?> klass) { try { return getOriginalUnmockedType(klass).getSuperclass().getDeclaredConstructors()[0]; } catch (Exception e) { throw new ConstructorNotFoundException("Failed to lookup constructor.", e); } }
/** * Get the first parent constructor defined in a super class of * <code>klass</code>. * * @param klass * The class where the constructor is located. <code>null</code> * ). * @return A . */ public static Constructor<?> getFirstParentConstructor(Class<?> klass) { try { return getUnmockedType(klass).getSuperclass().getDeclaredConstructors()[0]; } catch (Exception e) { throw new ConstructorNotFoundException("Failed to lookup constructor.", e); } }
private void throwExceptionIfConstructorWasNotFound() { if (potentialConstructor == null) { String message = "No constructor found in class '" + WhiteboxImpl.getOriginalUnmockedType(type).getName() + "' " + "with " + "parameter types: [ " + WhiteboxImpl.getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } } }
/** * Throw exception if constructor was not found. * * @param type the type * @param potentialConstructor the potential constructor * @param arguments the arguments */ static void throwExceptionIfConstructorWasNotFound(Class<?> type, Constructor<?> potentialConstructor, Object... arguments) { if (potentialConstructor == null) { String message = "No constructor found in class '" + getOriginalUnmockedType(type).getName() + "' with " + "parameter types: [ " + getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } }
/** * Throw exception if constructor was not found. * * @param type * the type * @param potentialConstructor * the potential constructor * @param arguments * the arguments */ static void throwExceptionIfConstructorWasNotFound(Class<?> type, Constructor<?> potentialConstructor, Object... arguments) { if (potentialConstructor == null) { String message = "No constructor found in class '" + getUnmockedType(type).getName() + "' with " + "parameter types: [ " + getArgumentTypesAsString(arguments) + " ]."; throw new ConstructorNotFoundException(message); } }
/** * Convenience method to get a (declared) constructor from a class type * without having to catch the checked exceptions otherwise required. These * exceptions are wrapped as runtime exceptions. The constructor is also set * to accessible. * * @param type The type of the class where the constructor is located. * @param parameterTypes All parameter types of the constructor (may be * {@code null}). * @return A . */ public static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes) { Class<?> unmockedType = WhiteboxImpl.getOriginalUnmockedType(type); try { final Constructor<?> constructor = unmockedType.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (RuntimeException e) { throw e; } catch (Error e) { throw e; } catch (Throwable e) { throw new ConstructorNotFoundException(String.format( "Failed to lookup constructor with parameter types [ %s ] in class %s.", getArgumentTypesAsString((Object[]) parameterTypes), unmockedType.getName()), e); } }
constructor = classThatContainsTheConstructorToTest.getDeclaredConstructor(parameterTypes); } catch (Exception e) { throw new ConstructorNotFoundException("Could not lookup the constructor", e);
constructor = classThatContainsTheConstructorToTest.getDeclaredConstructor(parameterTypes); } catch (Exception e) { throw new ConstructorNotFoundException("Could not lookup the constructor", e);
/** * Convenience method to get a (declared) constructor from a class type * without having to catch the checked exceptions otherwise required. These * exceptions are wrapped as runtime exceptions. The constructor is also set * to accessible. * * @param type * The type of the class where the constructor is located. * @param parameterTypes * All parameter types of the constructor (may be * <code>null</code>). * @return A . */ public static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes) { Class<?> unmockedType = WhiteboxImpl.getUnmockedType(type); try { final Constructor<?> constructor = unmockedType.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (RuntimeException e) { throw (RuntimeException) e; } catch (Error e) { throw (Error) e; } catch (Throwable e) { throw new ConstructorNotFoundException(String.format( "Failed to lookup constructor with parameter types [ %s ] in class %s.", getArgumentTypesAsString((Object[]) parameterTypes), unmockedType.getName()), e); } }
throw new ConstructorNotFoundException("Failed to find a constructor with parameter types: [" + getArgumentTypesAsString(arguments) + "]");
private static <T> Constructor<T> getBestCandidateConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] argumentTypes, Object[] arguments) { Constructor<T> constructor; Constructor<T> potentialConstructorWrapped = getPotentialConstructorWrapped(classThatContainsTheConstructorToTest, argumentTypes); Constructor<T> potentialConstructorPrimitive = getPotentialConstructorPrimitive(classThatContainsTheConstructorToTest, argumentTypes); if (potentialConstructorPrimitive == null && potentialConstructorWrapped == null) { // Check if we can find a matching var args constructor. constructor = getPotentialVarArgsConstructor(classThatContainsTheConstructorToTest, arguments); if (constructor == null) { throw new ConstructorNotFoundException("Failed to find a constructor with parameter types: [" + getArgumentTypesAsString(arguments) + "]"); } } else if (potentialConstructorPrimitive == null) { constructor = potentialConstructorWrapped; } else if (potentialConstructorWrapped == null) { constructor = potentialConstructorPrimitive; } else if (arguments == null || arguments.length == 0 && potentialConstructorPrimitive != null) { constructor = potentialConstructorPrimitive; } else { throw new TooManyConstructorsFoundException( "Could not determine which constructor to execute. Please specify the parameter types by hand."); } return constructor; }