Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2006-2015 phloc systems
* http://www.phloc.com
* office[at]phloc[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.phloc.commons.lang;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.phloc.commons.annotations.PresentForCodeCoverage;
import com.phloc.commons.collections.ArrayHelper;
/**
* This is a special helper class that provides many utility methods that
* require the SuppressWarnings("unchecked") annotation.
*
* @author Philip Helger
*/
@Immutable
public final class GenericReflection
{
private static final Logger s_aLogger = LoggerFactory.getLogger (GenericReflection.class);
private static final Class > [] EMPTY_CLASS_ARRAY = new Class > [0];
@PresentForCodeCoverage
@SuppressWarnings ("unused")
private static final GenericReflection s_aInstance = new GenericReflection ();
private GenericReflection ()
{}
@SuppressWarnings ("unchecked")
public static DSTTYPE uncheckedCast (@Nullable final SRCTYPE aObject)
{
return (DSTTYPE) aObject;
}
@Nonnull
public static Class getClassFromName (@Nonnull final String sName) throws ClassNotFoundException
{
return uncheckedCast (Class.forName (sName));
}
/**
* Get the class of the given name
*
* @param
* The return type
* @param sName
* The name to be resolved.
* @return null if the class could not be resolved
*/
@Nullable
public static Class getClassFromNameSafe (@Nonnull final String sName)
{
try
{
return getClassFromName (sName);
}
catch (final ClassNotFoundException e)
{
return null;
}
}
/**
* Get an array with all the classes of the passed object array.
*
* @param aObjs
* The object array. May be null. No contained element may
* be null.
* @return A non-null array of classes.
*/
@Nonnull
public static Class > [] getClassArray (@Nullable final Object... aObjs)
{
if (ArrayHelper.isEmpty (aObjs))
return EMPTY_CLASS_ARRAY;
final Class > [] ret = new Class > [aObjs.length];
for (int i = 0; i < aObjs.length; ++i)
ret[i] = aObjs[i].getClass ();
return ret;
}
/**
* This method dynamically invokes the method with the given name on the given
* object.
*
* @param aSrcObj
* The source object on which the method is to be invoked. May not be
* null.
* @param sMethodName
* The method to be invoked.
* @param aArgs
* The arguments to be passed into the method. May be null
* . If not null, the members of the array may not be
* null because otherwise the classes of the arguments
* cannot be determined and will throw an Exception!
* @return The return value of the invoked method or null for
* void methods.
* @throws NoSuchMethodException
* Thrown by reflection
* @throws IllegalAccessException
* Thrown by reflection
* @throws InvocationTargetException
* Thrown by reflection
*/
@Nullable
public static RETURNTYPE invokeMethod (@Nonnull final Object aSrcObj,
@Nonnull final String sMethodName,
@Nullable final Object... aArgs) throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
{
return GenericReflection. invokeMethod (aSrcObj, sMethodName, getClassArray (aArgs), aArgs);
}
@Nullable
public static RETURNTYPE invokeMethod (@Nonnull final Object aSrcObj,
@Nonnull final String sMethodName,
@Nullable final Class > [] aArgClasses,
@Nullable final Object [] aArgs) throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
{
final Method aMethod = aSrcObj.getClass ().getDeclaredMethod (sMethodName, aArgClasses);
final Object aReturn = aMethod.invoke (aSrcObj, aArgs);
return GenericReflection.