List of usage examples for org.apache.commons.lang3.reflect MethodUtils getMethodsListWithAnnotation
public static List<Method> getMethodsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls)
From source file:com.hurence.logisland.util.kura.Metrics.java
public static <T> T readFrom(final T object, final Map<String, Object> metrics) { Objects.requireNonNull(object); for (final Field field : FieldUtils.getFieldsListWithAnnotation(object.getClass(), Metric.class)) { final Metric m = field.getAnnotation(Metric.class); final boolean optional = field.isAnnotationPresent(Optional.class); final Object value = metrics.get(m.value()); if (value == null && !optional) { throw new IllegalArgumentException( String.format("Field '%s' is missing metric '%s'", field.getName(), m.value())); }/*from ww w.j a v a 2 s . co m*/ if (value == null) { // not set but optional continue; } try { FieldUtils.writeField(field, object, value, true); } catch (final IllegalArgumentException e) { // provide a better message throw new IllegalArgumentException(String.format("Failed to assign '%s' (%s) to field '%s'", value, value.getClass().getName(), field.getName()), e); } catch (final IllegalAccessException e) { throw new RuntimeException(e); } } for (final Method method : MethodUtils.getMethodsListWithAnnotation(object.getClass(), Metric.class)) { final Metric m = method.getAnnotation(Metric.class); final boolean optional = method.isAnnotationPresent(Optional.class); final Object value = metrics.get(m.value()); if (value == null && !optional) { throw new IllegalArgumentException( String.format("Method '%s' is missing metric '%s'", method.getName(), m.value())); } if (value == null) { // not set but optional continue; } try { method.invoke(object, value); } catch (final IllegalArgumentException e) { // provide a better message throw new IllegalArgumentException(String.format("Failed to call '%s' (%s) with method '%s'", value, value.getClass().getName(), method.getName()), e); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } } return object; }
From source file:org.grouplens.grapht.LifecycleManager.java
/** * Register a component with the lifecycle manager. The component will be torn down when the lifecycle manager * is closed, using whatever teardown the lifecycle manager institutes. * * @param instance The component to register. *//*from w w w.java 2s.co m*/ public void registerComponent(Object instance) { if (instance == null) { return; } if (instance instanceof AutoCloseable) { actions.add(new CloseAction((AutoCloseable) instance)); } for (Method m : MethodUtils.getMethodsListWithAnnotation(instance.getClass(), PreDestroy.class)) { actions.add(new PreDestroyAction(instance, m)); } }