com.helger.commons.lang.GenericReflection Maven / Gradle / Ivy
/**
* Copyright (C) 2014-2015 Philip Helger (www.helger.com)
* philip[at]helger[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.helger.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.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.collection.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
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 ClassLoader aClassLoader,
@Nonnull final String sName) throws ClassNotFoundException
{
ValueEnforcer.notNull (aClassLoader, "ClassLoader");
return uncheckedCast (aClassLoader.loadClass (sName));
}
@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 aClassLoader
* The class loader to be used. May not be null
.
* @param sName
* The name to be resolved.
* @return null
if the class could not be resolved
*/
@Nullable
public static Class getClassFromNameSafe (@Nonnull final ClassLoader aClassLoader,
@Nonnull final String sName)
{
ValueEnforcer.notNull (aClassLoader, "ClassLoader");
try
{
return getClassFromName (aClassLoader, sName);
}
catch (final ClassNotFoundException e)
{
return null;
}
}
/**
* 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
* The method return type
* @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.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy