felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1815548 - in /felix/trunk/converter/converter/src: main/java/org/osgi/util/converter/ test/java/org/osgi/util/converter/
Date Fri, 17 Nov 2017 05:08:44 GMT
Author: davidb
Date: Fri Nov 17 05:08:44 2017
New Revision: 1815548

URL: http://svn.apache.org/viewvc?rev=1815548&view=rev
Log:
Updates to the Converter to handle name mangling better.

Modified:
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java
    felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
    felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO7.java

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java?rev=1815548&r1=1815547&r2=1815548&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
Fri Nov 17 05:08:44 2017
@@ -436,9 +436,9 @@ class ConvertingImpl extends AbstractSpe
         else if (targetAsDTO || DTOUtil.isDTOType(targetAsClass))
             return convertToDTO(sourceClass, targetAsClass);
         else if (targetAsClass.isInterface())
-            return createInterface(sourceClass, targetAsClass);
+            return convertToInterface(sourceClass, targetAsClass);
         else if (targetAsJavaBean)
-            return createJavaBean(sourceClass, targetAsClass);
+            return convertToJavaBean(sourceClass, targetAsClass);
         throw new ConversionException("Cannot convert " + object + " to " + targetAsClass);
     }
 
@@ -463,7 +463,9 @@ class ConvertingImpl extends AbstractSpe
         }));
     }
 
-    private Object createJavaBean(Class<?> sourceCls, Class<?> targetCls) {
+    private Object convertToJavaBean(Class<?> sourceCls, Class<?> targetCls)
{
+        String prefix = Util.getPrefix(targetCls);
+
         @SuppressWarnings("rawtypes")
         Map m = mapView(object, sourceCls, converter);
         try {
@@ -475,7 +477,10 @@ class ConvertingImpl extends AbstractSpe
                     propName.append(setterName.substring(4));
 
                 Class<?> setterType = setter.getParameterTypes()[0];
-                setter.invoke(res, converter.convert(m.get(propName.toString())).to(setterType));
+                String key = propName.toString();
+                Object val = m
+                        .get(Util.unMangleName(prefix, key));
+                setter.invoke(res, converter.convert(val).to(setterType));
             }
             return res;
         } catch (Exception e) {
@@ -485,7 +490,7 @@ class ConvertingImpl extends AbstractSpe
     }
 
     @SuppressWarnings("rawtypes")
-    private Object createInterface(Class<?> sourceCls, final Class<?> targetCls)
{
+    private Object convertToInterface(Class<?> sourceCls, final Class<?> targetCls)
{
         InternalConverting ic = converter.convert(object);
         ic.sourceAs(sourceAsClass);
         if (sourceAsDTO)

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java?rev=1815548&r1=1815547&r2=1815548&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java Fri Nov
17 05:08:44 2017
@@ -126,7 +126,8 @@ class Util {
         if (propStr.length() > 1)
             propName.append(propStr.substring(1));
 
-        return propName.toString();
+		return unMangleName(
+				getPrefix(md.getDeclaringClass()), propName.toString());
     }
 
 
@@ -326,6 +327,7 @@ class Util {
 
         String res = key.replace("_", "__");
         res = res.replace("$", "$$");
+		res = res.replace("-", "$_$");
         res = res.replaceAll("[.]([._])", "_\\$$1");
         res = res.replace('.', '_');
         // TODO handle Java keywords
@@ -333,10 +335,12 @@ class Util {
     }
 
     static String unMangleName(String prefix, String key) {
-        String res = key.replaceAll("_\\$", ".");
+		String res = key;
+		res = res.replace("$$", "\b"); // park double dollar as backspace char
+		res = res.replace("$_$", "-");
+		res = res.replaceAll("_\\$", ".");
         res = res.replace("__", "\f"); // park double underscore as formfeed char
         res = res.replace('_', '.');
-        res = res.replace("$$", "\b"); // park double dollar as backspace char
         res = res.replace("$", "");
         res = res.replace('\f', '_');  // convert formfeed char back to single underscore
         res = res.replace('\b', '$');  // convert backspace char back go dollar

Modified: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java?rev=1815548&r1=1815547&r2=1815548&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
(original)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
Fri Nov 17 05:08:44 2017
@@ -824,6 +824,8 @@ public class ConverterTest {
         m.put("three_.prop", "hi ha ho");
         m.put("four._prop", "");
         m.put("five..prop", "test");
+        m.put("six-prop", "987");
+        m.put("seven$.prop", "3.141");
 
         MyDTO7 dto = converter.convert(m).to(MyDTO7.class);
         assertEquals("test123", dto.org_osgi_framework_uuid);
@@ -835,6 +837,8 @@ public class ConverterTest {
         assertEquals("hi ha ho", dto.three___prop);
         assertEquals("", dto.four_$__prop);
         assertEquals("test", dto.five_$_prop);
+        assertEquals((short) 987, dto.six$_$prop);
+        dto.seven$$_$prop = 3.141;
 
         // And convert back
         Map<String, String> m2 = converter.convert(dto).to(new TypeReference<Map<String,String>>()
{});

Modified: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO7.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO7.java?rev=1815548&r1=1815547&r2=1815548&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO7.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO7.java Fri
Nov 17 05:08:44 2017
@@ -26,4 +26,6 @@ public class MyDTO7 {
     public String three___prop;
     public String four_$__prop;
     public String five_$_prop;
+    public short six$_$prop;
+    public double seven$$_$prop;
 }



Mime
View raw message