PipelineOptionsReflector { static Set<PipelineOptionSpec> getOptionSpecs( Class<? extends PipelineOptions> optionsInterface, boolean skipHidden) { Iterable<Method> methods = ReflectHelpers.getClosureOfMethodsOnInterface(optionsInterface); Multimap<String, Method> propsToGetters = getPropertyNamesToGetters(methods); ImmutableSet.Builder<PipelineOptionSpec> setBuilder = ImmutableSet.builder(); for (Map.Entry<String, Method> propAndGetter : propsToGetters.entries()) { String prop = propAndGetter.getKey(); Method getter = propAndGetter.getValue(); @SuppressWarnings("unchecked") Class<? extends PipelineOptions> declaringClass = (Class<? extends PipelineOptions>) getter.getDeclaringClass(); if (!PipelineOptions.class.isAssignableFrom(declaringClass)) { continue; } if (skipHidden && declaringClass.isAnnotationPresent(Hidden.class)) { continue; } setBuilder.add(PipelineOptionSpec.of(declaringClass, prop, getter)); } return setBuilder.build(); } private  PipelineOptionsReflector();   }
@Test public void testGetOptionSpecs() throws NoSuchMethodException { Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(SimpleOptions.class, true); assertThat( properties, Matchers.hasItems( PipelineOptionSpec.of( SimpleOptions.class, "foo", SimpleOptions.class.getDeclaredMethod("getFoo")))); }
@Test public void testFiltersNonGetterMethods() { Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(OnlyTwoValidGetters.class, true); assertThat(properties, not(hasItem(hasName(isOneOf("misspelled", "hasParameter", "prefix"))))); }
@Test public void testBaseClassOptions() { Set<PipelineOptionSpec> props = PipelineOptionsReflector.getOptionSpecs(ExtendsSimpleOptions.class, true); assertThat(props, hasItem(allOf(hasName("foo"), hasClass(SimpleOptions.class)))); assertThat(props, hasItem(allOf(hasName("foo"), hasClass(ExtendsSimpleOptions.class)))); assertThat(props, hasItem(allOf(hasName("bar"), hasClass(ExtendsSimpleOptions.class)))); }
@Test public void testExcludesNonPipelineOptionsMethods() { Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(ExtendsNonPipelineOptions.class, true); assertThat(properties, not(hasItem(hasName("foo")))); }
@Test public void testExcludesHiddenInterfaces() { Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(HiddenOptions.class, true); assertThat(properties, not(hasItem(hasName("foo")))); }
@Test public void testIncludesHiddenInterfaces() { Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(HiddenOptions.class, false); assertThat(properties, hasItem(hasName("foo"))); }
@Test public void testShouldSerialize() { Set<PipelineOptionSpec> properties = PipelineOptionsReflector.getOptionSpecs(JsonIgnoreOptions.class, true); assertThat(properties, hasItem(allOf(hasName("notIgnored"), shouldSerialize()))); assertThat(properties, hasItem(allOf(hasName("ignored"), not(shouldSerialize())))); }
@Test public void testMultipleInputInterfaces() { Set<Class<? extends PipelineOptions>> interfaces = ImmutableSet.of(BaseOptions.class, ExtendOptions1.class, ExtendOptions2.class); Set<PipelineOptionSpec> props = PipelineOptionsReflector.getOptionSpecs(interfaces); assertThat(props, hasItem(allOf(hasName("baseOption"), hasClass(BaseOptions.class)))); assertThat(props, hasItem(allOf(hasName("extendOption1"), hasClass(ExtendOptions1.class)))); assertThat(props, hasItem(allOf(hasName("extendOption2"), hasClass(ExtendOptions2.class)))); }