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

org.jvnet.hyperjaxb3.lang.reflect.util.ReflectionUtils Maven / Gradle / Ivy

There is a newer version: 0.6.2
Show newest version
package org.jvnet.hyperjaxb3.lang.reflect.util;

import java.lang.reflect.Field;

public class ReflectionUtils {

	public static Object getFieldValue(Object instance, String fieldName) {
		final Class theClass = instance.getClass();
		final Object fieldValue = getFieldValue(instance, fieldName, theClass);
		return fieldValue;
	}

	private static Object getFieldValue(Object instance, String fieldName,
			final Class theClass) {
		try {
			final Field field = theClass.getDeclaredField(fieldName);
			boolean oldAccessible = field.isAccessible();
			try {
				field.setAccessible(true);
				return field.get(instance);
			} catch (IllegalArgumentException ex) {
				return null;
			} catch (IllegalAccessException ex) {
				return null;
			} finally {
				field.setAccessible(oldAccessible);
			}
		} catch (NoSuchFieldException nsfex) {
			if (theClass.getSuperclass() == null) {
				return null;
			} else {
				final Object fieldValue = getFieldValue(instance, fieldName,
						theClass.getSuperclass());
				return fieldValue;
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy