static void setFieldValue0(Object instance, Field field, Object value) { try { field.set(instance, value); } catch (IllegalAccessException e) { throw new ReflectionException(e); } }
static void setFieldValue0(Object instance, Field field, Object value) { try { field.set(instance, value); } catch (IllegalAccessException e) { throw new ReflectionException(e); } }
@SuppressWarnings("unchecked") static <E> E getFieldValue0(Object instance, Field field, String className, String fieldName) { try { return (E) field.get(instance); } catch (IllegalAccessException e) { throw new ReflectionException(format("Failed to access %s.%s", className, fieldName), e); } }
private void verifyArgs(Method method) { if (mustBoNoArg && method.getParameterTypes().length != 0) { throw new ReflectionException(format("Method '%s' can't have any args", method)); } }
@SuppressWarnings("unchecked") static <E> E getFieldValue0(Object instance, Field field, String className, String fieldName) { try { return (E) field.get(instance); } catch (IllegalAccessException e) { throw new ReflectionException(format("Failed to access %s.%s", className, fieldName), e); } }
private void verifyPublic(Method method) { if (mustBePublic && !isPublic(method.getModifiers())) { throw new ReflectionException(format("Method '%s' should be public", method)); } }
private void verifyNotStatic(Method method) { if (mustBeNotStatic && isStatic(method.getModifiers())) { throw new ReflectionException(format("Method '%s' should no be static", method)); } }
@SuppressWarnings("unchecked") private void verifyReturnType(Method method) { if (returnType != null && !returnType.isAssignableFrom(method.getReturnType())) { throw new ReflectionException(format("Method '%s' should have returnType %s", method, returnType)); } }
public Method find() { List<Method> methods = findAll(); switch (methods.size()) { case 0: return null; case 1: return methods.iterator().next(); default: throw new ReflectionException(format("Too many methods on class %s with annotation %s", clazz.getName(), annotationClazz.getName())); } }
public static <E> E getFieldValue(Object instance, String fieldName) { checkNotNull(instance, "Object to retrieve field from can't be null"); Field field; Class<?> clazz = instance.getClass(); try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { throw new ReflectionException(e); } return getFieldValue0(instance, field, clazz.getName(), fieldName); }
public static <E> E getFieldValue(Object instance, String fieldName) { checkNotNull(instance, "Object to retrieve field from can't be null"); Field field; Class<?> clazz = instance.getClass(); try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { throw new ReflectionException(e); } return getFieldValue0(instance, field, clazz.getName(), fieldName); }
/** * Gets the value for a static field. * * @param clazz class which contains the field * @param fieldName name of the field * @param fieldType type of the field * @param <E> type of the field * @return the value of the static field */ public static <E> E getStaticFieldValue(Class clazz, String fieldName, Class fieldType) { Field field = getField(clazz, fieldName, fieldType); if (field == null) { throw new ReflectionException(format("Field %s.%s is not found", clazz.getName(), fieldName)); } field.setAccessible(true); return getFieldValue0(null, field, clazz.getName(), fieldName); }
/** * Gets the value for a static field. * * @param clazz class which contains the field * @param fieldName name of the field * @param fieldType type of the field * @param <E> type of the field * @return the value of the static field */ public static <E> E getStaticFieldValue(Class clazz, String fieldName, Class fieldType) { Field field = getField(clazz, fieldName, fieldType); if (field == null) { throw new ReflectionException(format("Field %s.%s is not found", clazz.getName(), fieldName)); } field.setAccessible(true); return getFieldValue0(null, field, clazz.getName(), fieldName); }