Repository: reef
Updated Branches:
  refs/heads/master 5f4d12bcd -> 3d8ebd529


[REEF-1276] Improve error message shown when injectable class has [Parameter] 
annotation

This patch throws an Argument Exception with a message stating the class that
should not be defined as a NamedParameter

JIRA:
  [REEF-1276] (https://issues.apache.org/jira/browse/REEF-1276)

Pull Request:
  This closes #913


Project: http://git-wip-us.apache.org/repos/asf/reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/reef/commit/3d8ebd52
Tree: http://git-wip-us.apache.org/repos/asf/reef/tree/3d8ebd52
Diff: http://git-wip-us.apache.org/repos/asf/reef/diff/3d8ebd52

Branch: refs/heads/master
Commit: 3d8ebd52928e9d3c3ad0ef4bc8280ec700627bd4
Parents: 5f4d12b
Author: Sergey Dudoladov <[email protected]>
Authored: Wed Mar 30 11:01:07 2016 +0200
Committer: Markus Weimer <[email protected]>
Committed: Mon Apr 11 15:29:10 2016 -0700

----------------------------------------------------------------------
 .../ClassHierarchy/TestClassHierarchy.cs        | 31 ++++++++++++++++++++
 .../ClassHierarchy/ClassHierarchyImpl.cs        | 11 +++++++
 2 files changed, 42 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/reef/blob/3d8ebd52/lang/cs/Org.Apache.REEF.Tang.Tests/ClassHierarchy/TestClassHierarchy.cs
----------------------------------------------------------------------
diff --git 
a/lang/cs/Org.Apache.REEF.Tang.Tests/ClassHierarchy/TestClassHierarchy.cs 
b/lang/cs/Org.Apache.REEF.Tang.Tests/ClassHierarchy/TestClassHierarchy.cs
index 42a7b50..cb429aa 100644
--- a/lang/cs/Org.Apache.REEF.Tang.Tests/ClassHierarchy/TestClassHierarchy.cs
+++ b/lang/cs/Org.Apache.REEF.Tang.Tests/ClassHierarchy/TestClassHierarchy.cs
@@ -427,6 +427,17 @@ namespace Org.Apache.REEF.Tang.Tests.ClassHierarchy
             Assert.NotNull(b);
             Assert.NotNull(c);
         }
+
+        // See REEF-1276
+        [Fact]
+        public void 
TestInjectableClassWithParameterAnnotationThrowsMeaningfulException()
+        {
+            ITang tang = TangFactory.GetTang();
+            IInjector i = 
tang.NewInjector(tang.NewConfigurationBuilder().Build());
+
+            Action injection = () => i.GetInstance(typeof(ClazzA));
+            Assert.Throws<ArgumentException>(injection);
+        }
     }
 
     [NamedParameter]
@@ -696,4 +707,24 @@ namespace Org.Apache.REEF.Tang.Tests.ClassHierarchy
     class BadParsableDefaultClass : Name<string>
     {        
     }
+
+    // ClazzB is injectable and does not need the Parameter annotation in the 
ClazzA constructor
+    class ClazzA
+    {
+        private ClazzB objectB;
+
+        public class ClazzB 
+        {
+            [Inject]
+            public ClazzB()
+            {
+            }
+        }
+
+        [Inject]
+        public ClazzA([Parameter(typeof(ClazzB))] ClazzB objectB)
+        {
+            this.objectB = objectB;
+        }
+    }
  }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/reef/blob/3d8ebd52/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs
----------------------------------------------------------------------
diff --git 
a/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs
 
b/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs
index cda3660..9599c96 100644
--- 
a/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs
+++ 
b/lang/cs/Org.Apache.REEF.Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs
@@ -167,6 +167,17 @@ namespace 
Org.Apache.REEF.Tang.Implementations.ClassHierarchy
                         RegisterType(constructorArg.Gettype());  // Gettype 
returns param's Type.fullname
                         if (constructorArg.GetNamedParameterName() != null)
                         {
+                            var pn = 
RegisterType(constructorArg.GetNamedParameterName());
+                            
+                            if (!(pn is INamedParameterNode))
+                            {
+                                string message = 
string.Format(CultureInfo.CurrentCulture,
+                                                               "The class {0}, 
used in the constructor of {1}, should not be defined as a NamedParameter.",
+                                                               
constructorArg.GetNamedParameterName(),
+                                                               
constructorDef.GetClassName());
+                                
Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new 
ArgumentException(message), LOGGER);
+                            }
+
                             INamedParameterNode np = 
(INamedParameterNode)RegisterType(constructorArg.GetNamedParameterName());
                             try
                             {

Reply via email to