com.gregmarut.support.util.ReflectionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-beangen Show documentation
Show all versions of test-beangen Show documentation
Supports unit testing by dynamically creating bean objects and populating their fields to default values.
/*******************************************************************************
*
* Copyright (c) 2015 Greg Marut.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Greg Marut - initial API and implementation
*
******************************************************************************/
package com.gregmarut.support.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReflectionUtil
{
private ReflectionUtil()
{
}
/**
* Retrieve all of the fields for a given object including any parent classes
*
* @param object
* @return
*/
public static Field[] getAllFields(final Object object)
{
// holds the list of fields
List allFields = new ArrayList();
// holds the class to inspect
Class> clazz = object.getClass();
// while the class is not null
while (null != clazz)
{
// retrieve all of the declared field for this class and add them to the list
Field[] fields = clazz.getDeclaredFields();
allFields.addAll(Arrays.asList(fields));
// read this class' superclass next if one exists
clazz = clazz.getSuperclass();
}
return allFields.toArray(new Field[allFields.size()]);
}
/**
* Retrieve a method on the given class including any parent class
*
* @param object
* @return
* @throws NoSuchMethodException
*/
public static Method getDeclaredMethod(final Object object, final String name, final Class>... parameterTypes)
throws NoSuchMethodException
{
// holds the class to inspect
Class> clazz = object.getClass();
// while the class is not null
while (null != clazz)
{
// retrieve the method from this class
try
{
return clazz.getDeclaredMethod(name, parameterTypes);
}
catch (NoSuchMethodException e)
{
// ignore
}
catch (SecurityException e)
{
// ignore
}
// read this class' superclass next if one exists
clazz = clazz.getSuperclass();
}
// build the error message
StringBuilder sb = new StringBuilder();
sb.append(object.getClass().getName());
sb.append(".");
sb.append(name);
sb.append("()");
throw new NoSuchMethodException(sb.toString());
}
/**
* Extracts the list of generic classes from a field if they are defined
*
* @param field
* @return
*/
public static List> extractGenericClasses(final Field field)
{
// hold the list of generic classes for the object defined in this field
List> generics = new ArrayList>();
// get the generic type for this collection
Type genericType = field.getGenericType();
// make sure there is a generic type assigned for this collection
if (genericType instanceof ParameterizedType)
{
// attempt to extract the generic from this collection
ParameterizedType parameterizedTypes = (ParameterizedType) genericType;
// for each of the actual type arguments
for (Type type : parameterizedTypes.getActualTypeArguments())
{
final Class> clazz = (Class>) type;
generics.add(clazz);
}
}
return generics;
}
}