fr.inria.eventcloud.utils.ReflectionUtils Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2012 INRIA.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
**/
package fr.inria.eventcloud.utils;
import java.lang.reflect.Field;
/**
* A utility class for reflectively finding fields using reflection.
*
* @author bsauvan
*/
public class ReflectionUtils {
/**
* Returns the value of a private/protected/public field.
*
* @param instance
* the object instance.
* @param fieldName
* the name of the field.
*
* @return the value of the field or null if not found or an error is
* encountered.
*/
public static Object getFieldValue(Object instance, String fieldName) {
try {
Field field = instance.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(instance);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}