HealthBean { public static HealthBean forTarget(Object target) { requireNonNull(target, "target is null"); ImmutableList.Builder<HealthBeanAttribute> attributes = ImmutableList.builder(); for (Map.Entry<Method, Method> entry : AnnotationUtils.findAnnotatedMethods(target.getClass(), HealthCheck.class, HealthCheckRemoveFromRotation.class, HealthCheckRestartDesired.class).entrySet()) { Method concreteMethod = entry.getKey(); Method annotatedMethod = entry.getValue(); if (!isValidGetter(concreteMethod)) { throw new RuntimeException("healthcheck annotation on non-getter " + annotatedMethod.toGenericString()); } attributes.addAll(new HealthBeanAttributeBuilder() .onInstance(target) .withConcreteGetter(concreteMethod) .withAnnotatedGetter(annotatedMethod) .build()); } for (Field field : AnnotationUtils.findAnnotatedFields(target.getClass(), HealthCheck.class, HealthCheckRemoveFromRotation.class, HealthCheckRestartDesired.class)) { if (!AtomicReference.class.isAssignableFrom(field.getType())) { throw new RuntimeException("healthcheck annotation on non-AtomicReference field " + field.toGenericString()); } attributes.addAll(new HealthBeanAttributeBuilder() .onInstance(target) .withField(field) .build()); } return new AutoValue_HealthBean(attributes.build()); }  abstract Collection<HealthBeanAttribute> getAttributes(); static HealthBean forTarget(Object target);  }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "healthcheck annotation on non-getter .*operation\\(int\\)") public void testNonAttribute() { HealthBean.forTarget(new Object() { @HealthCheck("Operation") public void operation(int param) { } }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "healthcheck annotation on non-getter .*getVoid\\(\\)") public void testInvalidGetter() { HealthBean.forTarget(new Object() { @HealthCheck("Void") public void getVoid() { } }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "healthcheck annotation on non-AtomicReference field .*field") public void testFieldNotAtomicReference() { HealthBean.forTarget(new Object() { @HealthCheck("Field") private Object field; }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*getString\\(\\) cannot have both @HealthCheckRemoveFromRotation and @HealthCheckRestartDesired annotations") public void testMethodRestartAndRemoveAnnotations() { HealthBean.forTarget(new Object() { @HealthCheckRemoveFromRotation("Name 1") @HealthCheckRestartDesired("Name 2") public String getString() { return null; } }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*field cannot have both @HealthCheckRemoveFromRotation and @HealthCheckRestartDesired annotations") public void testFieldRestartAndRemoveAnnotations() { HealthBean.forTarget(new Object() { @HealthCheckRemoveFromRotation("Field 1") @HealthCheckRestartDesired("Field 2") private AtomicReference<String> field; }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*getString\\(\\) cannot have both @HealthCheck and @HealthCheckRestartDesired annotations") public void testMethodRegularAndRestartAnnotations() { HealthBean.forTarget(new Object() { @HealthCheck("Name 1") @HealthCheckRestartDesired("Name 2") public String getString() { return null; } }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*field cannot have both @HealthCheck and @HealthCheckRestartDesired annotations") public void testFieldRegularAndRestartAnnotations() { HealthBean.forTarget(new Object() { @HealthCheck("Field 1") @HealthCheckRestartDesired("Field 2") private AtomicReference<String> field; }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*getString\\(\\) cannot have both @HealthCheck and @HealthCheckRemoveFromRotation annotations") public void testMethodRegularAndRemoveAnnotations() { HealthBean.forTarget(new Object() { @HealthCheck("Name 1") @HealthCheckRemoveFromRotation("Name 2") public String getString() { return null; } }); }
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*field cannot have both @HealthCheck and @HealthCheckRemoveFromRotation annotations") public void testFieldRegularAndRemoveAnnotations() { HealthBean.forTarget(new Object() { @HealthCheck("Field 1") @HealthCheckRemoveFromRotation("Field 2") private AtomicReference<String> field; }); }