### Question:
Functions { public static <T> ToDoubleFunction<T> invokeMethod(final Method method) { method.setAccessible(true); return (obj) -> { try { final Number n = (Number) method.invoke(obj); return n.doubleValue(); } catch (Exception e) { LOGGER.warn("exception from method registered as a gauge [" + method + "]", e); return Double.NaN; } }; } private  Functions(); static DoubleFunction<AtomicLong> age(final Clock clock); static ToDoubleFunction<T> invokeMethod(final Method method); static Predicate<Meter> nameEquals(String name); static final DoubleFunction<? extends Number> IDENTITY; static final DoubleFunction<AtomicLong> AGE; }### Answer:
@Test public void invokeMethodByte() throws Exception { final ToDoubleFunction<FunctionsTest> f = Functions.invokeMethod(Utils.getMethod(getClass(), "byteMethod")); Assertions.assertEquals(f.applyAsDouble(this), 1.0, 1e-12); }
@Test public void invokeMethodShort() throws Exception { final ToDoubleFunction<FunctionsTest> f = Functions.invokeMethod(Utils.getMethod(getClass(), "shortMethod")); Assertions.assertEquals(f.applyAsDouble(this), 2.0, 1e-12); }
@Test public void invokeMethodInt() throws Exception { final ToDoubleFunction<FunctionsTest> f = Functions.invokeMethod(Utils.getMethod(getClass(), "intMethod")); Assertions.assertEquals(f.applyAsDouble(this), 3.0, 1e-12); }
@Test public void invokeMethodLong() throws Exception { final ToDoubleFunction<FunctionsTest> f = Functions.invokeMethod(Utils.getMethod(getClass(), "longMethod")); Assertions.assertEquals(f.applyAsDouble(this), 4.0, 1e-12); }
@Test public void invokeMethodWrapperLong() throws Exception { final ToDoubleFunction<FunctionsTest> f = Functions.invokeMethod( Utils.getMethod(getClass(), "wrapperLongMethod")); Assertions.assertEquals(f.applyAsDouble(this), 5.0, 1e-12); }
@Test public void invokeBadMethod() throws Exception { final ToDoubleFunction<FunctionsTest> f = Functions.invokeMethod(Utils.getMethod(getClass(), "throwsMethod")); Assertions.assertEquals(f.applyAsDouble(this), Double.NaN, 1e-12); }
@Test public void invokeNoSuchMethod() throws Exception { Assertions.assertThrows(NoSuchMethodException.class, () -> Functions.invokeMethod(Utils.getMethod(getClass(), "unknownMethod"))); }
@Test public void invokeOnSubclass() throws Exception { final ToDoubleFunction<B> f = Functions.invokeMethod(Utils.getMethod(B.class, "two")); Assertions.assertEquals(f.applyAsDouble(new B()), 2.0, 1e-12); }
@Test public void invokeOneA() throws Exception { final ToDoubleFunction<A> f = Functions.invokeMethod(Utils.getMethod(A.class, "one")); Assertions.assertEquals(f.applyAsDouble(new A()), 1.0, 1e-12); }
@Test public void invokeOneB() throws Exception { final ToDoubleFunction<B> f = Functions.invokeMethod(Utils.getMethod(B.class, "one")); Assertions.assertEquals(f.applyAsDouble(new B()), -1.0, 1e-12); }