From e84cb0911129757e42716cce5c75b1d64e619e31 Mon Sep 17 00:00:00 2001 From: sammcveety Date: Fri, 14 Oct 2016 13:41:33 -0400 Subject: [PATCH] Add property name to RuntimeValueProvider --- .../sdk/options/ProxyInvocationHandler.java | 7 +++++-- .../apache/beam/sdk/options/ValueProvider.java | 17 ++++++++++++++--- .../beam/sdk/options/ValueProviderTest.java | 7 +++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java index 47d7cee8b6c2f..801e2f059e677 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java @@ -488,12 +488,15 @@ private Object getDefault(PipelineOptions proxy, Method method) { } } if (method.getReturnType().equals(ValueProvider.class)) { + String propertyName = gettersToPropertyNames.get(method.getName()); return defaultObject == null ? new RuntimeValueProvider( - method.getName(), (Class) method.getDeclaringClass(), + method.getName(), propertyName, + (Class) method.getDeclaringClass(), proxy.getOptionsId()) : new RuntimeValueProvider( - method.getName(), (Class) method.getDeclaringClass(), + method.getName(), propertyName, + (Class) method.getDeclaringClass(), defaultObject, proxy.getOptionsId()); } else if (defaultObject != null) { return defaultObject; diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java index e4502fc42a110..9b3d44ee90035 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java @@ -108,6 +108,7 @@ public static class RuntimeValueProvider implements ValueProvider, Seriali private final Class klass; private final String methodName; + private final String propertyName; @Nullable private final T defaultValue; private final Long optionsId; @@ -116,9 +117,10 @@ public static class RuntimeValueProvider implements ValueProvider, Seriali * Creates a {@link RuntimeValueProvider} that will query the provided * {@code optionsId} for a value. */ - RuntimeValueProvider(String methodName, Class klass, - Long optionsId) { + RuntimeValueProvider(String methodName, String propertyName, + Class klass, Long optionsId) { this.methodName = methodName; + this.propertyName = propertyName; this.klass = klass; this.defaultValue = null; this.optionsId = optionsId; @@ -128,9 +130,11 @@ public static class RuntimeValueProvider implements ValueProvider, Seriali * Creates a {@link RuntimeValueProvider} that will query the provided * {@code optionsId} for a value, or use the default if no value is available. */ - RuntimeValueProvider(String methodName, Class klass, + RuntimeValueProvider(String methodName, String propertyName, + Class klass, T defaultValue, Long optionsId) { this.methodName = methodName; + this.propertyName = propertyName; this.klass = klass; this.defaultValue = defaultValue; this.optionsId = optionsId; @@ -167,6 +171,13 @@ public boolean isAccessible() { PipelineOptions options = optionsMap.get(optionsId); return options != null; } + + /** + * Returns the property name that corresponds to this provider. + */ + public String propertyName() { + return propertyName; + } } /** diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java index 0cde6154867bb..8261028463817 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java @@ -97,6 +97,13 @@ public void testNoDefaultRuntimeProvider() { provider.get(); } + @Test + public void testRuntimePropertyName() { + TestOptions options = PipelineOptionsFactory.as(TestOptions.class); + ValueProvider provider = options.getFoo(); + assertEquals("foo", ((RuntimeValueProvider) provider).propertyName()); + } + @Test public void testDefaultRuntimeProvider() { TestOptions options = PipelineOptionsFactory.as(TestOptions.class);