All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gregmarut.support.util.ReflectionUtil Maven / Gradle / Ivy

Go to download

Supports unit testing by dynamically creating bean objects and populating their fields to default values.

There is a newer version: 2.7-RELEASE
Show newest version
/*******************************************************************************
 * 
 * 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.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()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy