Repository: reef Updated Branches: refs/heads/master d8cb35e49 -> 365f36ab2
[REEF-1876] Support enums in named parameters in Tang. Summary of changes: * Add support for simple Java enums in named parameters in Tang. Invoke a static method `.valueOf(String)` of the corresponding enum to instantiate an object, if necessary. * Add unit tests to check the new functionality for explicit and default enum parameters. JIRA: [REEF-1876](https://issues.apache.org/jira/browse/REEF-1876) Pull request: This closes #1370 Project: http://git-wip-us.apache.org/repos/asf/reef/repo Commit: http://git-wip-us.apache.org/repos/asf/reef/commit/365f36ab Tree: http://git-wip-us.apache.org/repos/asf/reef/tree/365f36ab Diff: http://git-wip-us.apache.org/repos/asf/reef/diff/365f36ab Branch: refs/heads/master Commit: 365f36ab2e93a4ef09a09a15f419c6b16e82eaa7 Parents: d8cb35e Author: Sergiy Matusevych <[email protected]> Authored: Thu Aug 31 18:19:27 2017 -0700 Committer: Byung-Gon Chun <[email protected]> Committed: Fri Sep 1 12:07:45 2017 +0900 ---------------------------------------------------------------------- .../reef/tang/formats/ParameterParser.java | 19 +++++++ .../java/org/apache/reef/tang/TestEnum.java | 57 ++++++++++++++++++++ 2 files changed, 76 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/reef/blob/365f36ab/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ParameterParser.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ParameterParser.java b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ParameterParser.java index 9396b96..0ea0619 100644 --- a/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ParameterParser.java +++ b/lang/java/reef-tang/tang/src/main/java/org/apache/reef/tang/formats/ParameterParser.java @@ -24,6 +24,8 @@ import org.apache.reef.tang.util.MonotonicTreeMap; import org.apache.reef.tang.util.ReflectionUtilities; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Collections; import java.util.HashSet; @@ -87,6 +89,9 @@ public class ParameterParser { } public <T> T parse(final Class<T> c, final String s) { + if (c.isEnum()) { + return parseEnum(c, s); + } final Class<?> d = ReflectionUtilities.boxClass(c); for (final Type e : ReflectionUtilities.classAndAncestors(d)) { final String name = ReflectionUtilities.getFullName(e); @@ -103,6 +108,20 @@ public class ParameterParser { } @SuppressWarnings("unchecked") + private <T> T parseEnum(final Class<T> c, final String s) { + try { + final Method valueOf = c.getMethod("valueOf", String.class); + return (T) valueOf.invoke(null, s); + } catch (final NoSuchMethodException e) { + throw new RuntimeException("Static .valueOf(String) method not found at " + c, e); + } catch (final InvocationTargetException e) { + throw new RuntimeException("Cannot invoke .valueOf(String) method of " + c, e); + } catch (final IllegalAccessException e) { + throw new RuntimeException("Cannot create instance of " + c + " - is it public?", e); + } + } + + @SuppressWarnings("unchecked") public <T> T parse(final String name, final String value) { if (parsers.containsKey(name)) { try { http://git-wip-us.apache.org/repos/asf/reef/blob/365f36ab/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestEnum.java ---------------------------------------------------------------------- diff --git a/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestEnum.java b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestEnum.java new file mode 100644 index 0000000..46245d7 --- /dev/null +++ b/lang/java/reef-tang/tang/src/test/java/org/apache/reef/tang/TestEnum.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.reef.tang; + +import org.apache.reef.tang.annotations.Name; +import org.apache.reef.tang.annotations.NamedParameter; +import org.apache.reef.tang.exceptions.InjectionException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestEnum { + + public enum FooEnum { BAR, BAZ } + + @NamedParameter(default_value = "BAR") + private final class FooParam implements Name<FooEnum> { } + + protected Tang tang; + + @Before + public void setUp() throws Exception { + tang = Tang.Factory.getTang(); + } + + @Test + public void testEnumDefault() throws InjectionException { + final Configuration config = tang.newConfigurationBuilder().build(); + final Injector injector = tang.newInjector(config); + Assert.assertEquals(FooEnum.BAR, injector.getNamedInstance(FooParam.class)); + } + + @Test + public void testEnumFromString() throws InjectionException { + final Configuration config = tang.newConfigurationBuilder() + .bindNamedParameter(FooParam.class, "BAZ") + .build(); + final Injector injector = tang.newInjector(config); + Assert.assertEquals(FooEnum.BAZ, injector.getNamedInstance(FooParam.class)); + } +}
