Monitors { static void addMonitorFields( List<Monitor<?>> monitors, String id, TagList tags, Object obj) { try { final SortedTagList.Builder builder = SortedTagList.builder(); builder.withTag("class", className(obj.getClass())); if (tags != null) { builder.withTags(tags); } if (id != null) { builder.withTag("id", id); } final TagList classTags = builder.build(); final Set<Field> fields = Reflection.getAllFields(obj.getClass()); for (Field field : fields) { if (isMonitorType(field.getType())) { field.setAccessible(true); final Monitor<?> m = (Monitor<?>) field.get(obj); if (m == null) { throw new NullPointerException("field " + field.getName() + " in class " + field.getDeclaringClass().getName() + " is null, all monitor fields must be" + " initialized before registering"); } monitors.add(wrap(classTags, m)); } } } catch (Exception e) { throw Throwables.propagate(e); } } private  Monitors(); static Timer newTimer(String name); static Timer newTimer(String name, TaggingContext context); static Timer newTimer(String name, TimeUnit unit); static Timer newTimer(String name, TimeUnit unit, TaggingContext context); static Counter newCounter(String name); static Counter newCounter(String name, TaggingContext context); static CompositeMonitor<?> newObjectMonitor(Object obj); static CompositeMonitor<?> newObjectMonitor(String id, Object obj); static CompositeMonitor<?> newThreadPoolMonitor(String id, ThreadPoolExecutor pool); static CompositeMonitor<?> newCacheMonitor(String id, Cache<?, ?> cache); static void registerObject(Object obj); static void unregisterObject(Object obj); static void unregisterObject(String id, Object obj); static void registerObject(String id, Object obj); static boolean isObjectRegistered(Object obj); static boolean isObjectRegistered(String id, Object obj);  }
@Test public void testAddMonitorFields() throws Exception { List<Monitor<?>> monitors = new ArrayList<>(); ClassWithMonitors obj = new ClassWithMonitors(); TagList tags = BasicTagList.of("abc", "def"); Monitors.addMonitorFields(monitors, null, tags, obj); Monitors.addMonitorFields(monitors, "foo", null, obj); assertEquals(monitors.size(), 8); Monitor<?> m = monitors.get(0); assertEquals(m.getConfig().getTags().getTag("class").getValue(), "ClassWithMonitors"); assertEquals(m.getConfig().getTags().getTag("abc").getValue(), "def"); }