You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2017/10/03 09:50:34 UTC

svn commit: r1810825 - in /felix/trunk/gogo/runtime/src: main/java/org/apache/felix/gogo/runtime/Reflective.java test/java/org/apache/felix/gogo/runtime/ReflectiveTest.java

Author: gnodet
Date: Tue Oct  3 09:50:34 2017
New Revision: 1810825

URL: http://svn.apache.org/viewvc?rev=1810825&view=rev
Log:
[FELIX-5706] Unable to access DTO fields using reflection

Modified:
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
    felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/ReflectiveTest.java

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java?rev=1810825&r1=1810824&r2=1810825&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Reflective.java Tue Oct  3 09:50:34 2017
@@ -20,6 +20,7 @@ package org.apache.felix.gogo.runtime;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -148,6 +149,26 @@ public final class Reflective
         }
         else
         {
+            if (args.isEmpty())
+            {
+                Field[] fields;
+                if (target instanceof Class<?>)
+                {
+                    fields = ((Class<?>) target).getFields();
+                }
+                else
+                    {
+                    fields = target.getClass().getFields();
+                }
+                for (Field f : fields)
+                {
+                    String mname = f.getName().toLowerCase(Locale.ENGLISH);
+                    if (mname.equals(name))
+                    {
+                        return f.get(target);
+                    }
+                }
+            }
             ArrayList<String> list = new ArrayList<>();
             for (Class<?>[] types : possibleTypes)
             {

Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/ReflectiveTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/ReflectiveTest.java?rev=1810825&r1=1810824&r2=1810825&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/ReflectiveTest.java (original)
+++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/ReflectiveTest.java Tue Oct  3 09:50:34 2017
@@ -33,6 +33,15 @@ import org.junit.Test;
 public class ReflectiveTest {
 
     @Test
+    public void testDtoAccess() throws Exception {
+        InputStream in = new ByteArrayInputStream(new byte[0]);
+        OutputStream out = new ByteArrayOutputStream();
+        CommandProcessorImpl processor = new CommandProcessorImpl(null);
+        Object result = Reflective.invoke(new CommandSessionImpl(processor, in, out, out), new TheDTO("foo"), "name", Collections.emptyList());
+        assertEquals("foo", result);
+    }
+
+    @Test
     public void testArrayInvocation() throws Exception {
         assertEquals(new Object[] { 1, "ab" }, invoke("test1", Arrays.<Object>asList(1, "ab")));
         assertEquals(new String[] { "1", "ab" }, invoke("test2", Arrays.<Object>asList(1, "ab")));
@@ -101,4 +110,11 @@ public class ReflectiveTest {
         }
     }
 
+    static class TheDTO {
+        public String name;
+
+        public TheDTO(String name) {
+            this.name = name;
+        }
+    }
 }