Here you can find the source of castNullableParameterTo(String parameterName, T value, Class
public static <T, R extends T> R castNullableParameterTo(String parameterName, T value, Class<R> requiredType)
//package com.java2s; /***************************************************************************************** * *** BEGIN LICENSE BLOCK *****/*ww w. j av a2s . c o m*/ * * Version: MPL 2.0 * * echocat RedPrecursor, Copyright (c) 2011-2012 echocat * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * *** END LICENSE BLOCK ***** ****************************************************************************************/ public class Main { public static <T, R extends T> R castNullableParameterTo(String parameterName, T value, Class<R> requiredType) { final R result; if (value != null) { if (!requiredType.isInstance(value)) { throw new IllegalArgumentException( parameterName + " is not of type " + requiredType.getName() + "."); } else { result = requiredType.cast(value); } } else { result = null; } return result; } }