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) 2018, TechEmpower, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name TechEmpower, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL TECHEMPOWER, INC. BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
package com.techempower.helper;
import java.lang.reflect.*;
import java.util.*;
/**
* ReflectionHelper provides utility functions for working with Reflection.
*/
public final class ReflectionHelper
{
//
// Static variables.
//
public static final Object[] NO_VALUES = new Object[0];
public static final Class>[] NO_PARAMETERS = new Class[0];
//
// Static methods.
//
/**
* Copies the values returned by the given get method when called on the
* elements in source to a new array of the same length as source. In the
* case of an error a zero length array will be returned in its place.
*
* @param source The array to copy values from.
* @param getMethodName The method to call in order to retrieve the values.
* @return A new array containing the results of calling the given method on
* each element in source, or a zero length array in the case of an
* error.
*/
public static Object[] copy(Object[] source, String getMethodName)
{
final Object[] result = new Object[source.length];
Class extends Object> previousClass = null;
Method method = null;
for(int i = 0; i < source.length; i++)
{
final Object sourceObject = source[i];
if (sourceObject != null)
{
Class extends Object> objClass = sourceObject.getClass();
if (objClass != previousClass)
{
previousClass = objClass;
method = findMethod(objClass, getMethodName);
if (method == null)
{
return new Object[0];
}
}
try
{
if (method != null)
{
result[i] = method.invoke(sourceObject, (Object[])null);
}
else
{
return new Object[0];
}
}
catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException iaexc)
{
return new Object[0];
}
}
}
return result;
}
/**
* Copies the values returned by the given get method when called on the
* elements in source to a new Collection. In the case of an empty
* Collection will be returned in its place.
*
* @param source The Collection to copy values from.
* @param getMethodName The method to call in order to retrieve the values.
* @return A new Collection containing the results of calling the given
* method on each element in source, or an empty Collection in the
* case of an error.
*/
public static Collection extends Object>
copy(Collection extends Object> source, String getMethodName)
{
final Collection