Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package de.cbb.mplayer.util; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.logging.Level; import java.util.logging.Logger; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.text.WordUtils; /** * * @author cbb */ @Slf4j public class ReflectionHelper { public static boolean ofType(Object object, String fieldname, Class type) { try { return object.getClass().getDeclaredField(fieldname).getType().equals(type); // PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(object, fieldname); // Class<?> ltype = PropertyUtils.getPropertyType(object, fieldname); // return ltype.equals(type); } catch (Exception ex) { log.debug("no field or no access to: " + fieldname); } return false; } public static Method getter(Object source, String field) { Method getter = null; try { getter = source.getClass().getDeclaredMethod( ReflectionHelper.getterName(field, ReflectionHelper.getFieldType(source, field))); } catch (java.lang.ClassCastException ex) { log.warn("getter: No cast available for: " + field); } catch (Exception ex) { log.warn(ex.toString()); } return getter; } public static String getterName(String field, Class type) { if (Boolean.class.getSimpleName().toLowerCase().equals(type.getSimpleName().toLowerCase())) return "is" + WordUtils.capitalize(field); else return "get" + WordUtils.capitalize(field); } public static String valueOfGetter(Object source, String field) { return TreeNodeCaptionMapper.map(unmappedValueOfGetter(source, field)); } private static Object unmappedValueOfGetter(Object source, String field) { Object value = null; Method getter = getter(source, field); try { value = getter.invoke(source, null); } catch (java.lang.ClassCastException ex) { log.warn("valueOfGetter: No cast available for: " + field); } catch (Exception ex) { log.warn(ex.toString()); } return value; } /** * Searches for a field in the given class and all of its super classes. * @param clazz Class to start the search for the field * @param name Name of the field * @return The field that was found * @throws NoSuchFieldException */ public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException { Class<?> searchClass = clazz; Field field = null; while (field == null && searchClass != null) { try { field = searchClass.getDeclaredField(name); } catch (NoSuchFieldException e) { searchClass = searchClass.getSuperclass(); } } if (field == null) { throw new NoSuchFieldException(clazz.getSimpleName() + "." + name); //$NON-NLS-1$ } return field; } /** * Utility method to set a field to a value. If the field is not accessible, it will be set to be accessible. * @param object Instance in which the value should be set * @param name Name of the field who's value should be set * @param value The value to be set */ public static void setFieldValue(Object object, String name, Object value) { try { Field field = getField(object.getClass(), name); if (!field.isAccessible()) { field.setAccessible(true); } field.set(object, value); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException( "Could not set field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$ } } /** * Utility method to read a field value. If the field is not accessible, it will be set to be accessible. * @param object Instance in which the value should be read * @param name Name of the field who's value should be read * @return The value of the field */ public static Object getFieldValue(Object object, String name) { try { Field field = getField(object.getClass(), name); if (!field.isAccessible()) { field.setAccessible(true); } return field.get(object); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException( "Could not read field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$ } } public static Class getFieldType(Object source, String fieldname) { try { Field field = getField(source.getClass(), fieldname); return field.getType(); } catch (NoSuchFieldException ex) { Logger.getLogger(ReflectionHelper.class.getName()).log(Level.SEVERE, null, ex); } return String.class; } }