You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2016/11/28 21:33:46 UTC
svn commit: r1771818 - in /felix/trunk/converter/converter/src:
main/java/org/apache/felix/converter/impl/ConvertingImpl.java
test/java/org/apache/felix/converter/impl/ConverterTest.java
test/java/org/apache/felix/converter/impl/MyDTO7.java
Author: davidb
Date: Mon Nov 28 21:33:46 2016
New Revision: 1771818
URL: http://svn.apache.org/viewvc?rev=1771818&view=rev
Log:
Felix converter - handle name mangling for DTOs
Added:
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/MyDTO7.java
Modified:
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1771818&r1=1771817&r2=1771818&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java Mon Nov 28 21:33:46 2016
@@ -296,10 +296,10 @@ public class ConvertingImpl implements C
for (Map.Entry entry : (Set<Map.Entry>) m.entrySet()) {
Field f = null;
try {
- f = targetAsClass.getDeclaredField(entry.getKey().toString());
+ f = targetAsClass.getDeclaredField(mangleName(entry.getKey().toString()));
} catch (NoSuchFieldException e) {
try {
- f = targetAsClass.getField(entry.getKey().toString());
+ f = targetAsClass.getField(mangleName(entry.getKey().toString()));
} catch (NoSuchFieldException e1) {
// There is not field with this name
}
@@ -763,13 +763,13 @@ public class ConvertingImpl implements C
if (Modifier.isStatic(field.getModifiers()))
return;
- String fn = field.getName();
+ String fn = unMangleName(field.getName());
if (handledFields.contains(fn))
return; // Field with this name was already handled
try {
List<Object> ks = new ArrayList<>(keys);
- ks.add(field.getName());
+ ks.add(fn);
Object[] ka = ks.toArray();
Object fVal = field.get(obj);
@@ -777,12 +777,32 @@ public class ConvertingImpl implements C
fVal = converter.convert(fVal).key(ka).to(Map.class);
}
- result.put(field.getName(), fVal);
+ result.put(fn, fVal);
handledFields.add(fn);
} catch (Exception e) {
}
}
+ private String mangleName(String key) {
+ String res = key.replace("_", "__");
+ res = res.replace("$", "$$");
+ res = res.replaceAll("[.]([._])", "_\\$$1");
+ res = res.replace('.', '_');
+ // TODO handle Java keywords
+ return res;
+ }
+
+ private String unMangleName(String key) {
+ String res = key.replaceAll("_\\$", ".");
+ res = res.replace("__", "\f"); // parkl 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
+ return res;
+ }
+
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void handleBeanMethod(Object obj, Method md, Set<String> invokedMethods, Map res) {
if (Modifier.isStatic(md.getModifiers()))
Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1771818&r1=1771817&r2=1771818&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java Mon Nov 28 21:33:46 2016
@@ -614,6 +614,35 @@ public class ConverterTest {
converter.convert(mb).source().asBean().to(Map.class));
}
+ @Test
+ public void testDTONameMangling() {
+ Map<String,String> m = new HashMap<>();
+ m.put("org.osgi.framework.uuid", "test123");
+ m.put("myProperty143", "true");
+ m.put("my$prop", "42");
+ m.put("dot.prop", "456");
+ m.put(".secret", " ");
+ m.put("another_prop", "lalala");
+ m.put("three_.prop", "hi ha ho");
+ m.put("four._prop", "");
+ m.put("five..prop", "test");
+
+ MyDTO7 dto = converter.convert(m).to(MyDTO7.class);
+ assertEquals("test123", dto.org_osgi_framework_uuid);
+ assertTrue(dto.myProperty143);
+ assertEquals(42, dto.my$$prop);
+ assertEquals(Long.valueOf(456L), dto.dot_prop);
+ assertEquals(' ', dto._secret);
+ assertEquals("lalala", dto.another__prop);
+ assertEquals("hi ha ho", dto.three___prop);
+ assertEquals("", dto.four_$__prop);
+ assertEquals("test", dto.five_$_prop);
+
+ // And convert back
+ Map<String, String> m2 = converter.convert(dto).to(new TypeReference<Map<String,String>>() {});
+ assertEquals(m, m2);
+ }
+
static class MyClass2 {
private final String value;
public MyClass2(String v) {
Added: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/MyDTO7.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/MyDTO7.java?rev=1771818&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/MyDTO7.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/MyDTO7.java Mon Nov 28 21:33:46 2016
@@ -0,0 +1,29 @@
+/*
+ * 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.felix.converter.impl;
+
+public class MyDTO7 {
+ public String org_osgi_framework_uuid;
+ public boolean myProperty143;
+ public int my$$prop;
+ public Long dot_prop;
+ public char _secret;
+ public String another__prop;
+ public String three___prop;
+ public String four_$__prop;
+ public String five_$_prop;
+}