felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1787776 - in /felix/trunk/converter/converter/src: main/java/org/apache/felix/converter/impl/ test/java/org/apache/felix/converter/impl/
Date Mon, 20 Mar 2017 14:09:15 GMT
Author: davidb
Date: Mon Mar 20 14:09:14 2017
New Revision: 1787776

URL: http://svn.apache.org/viewvc?rev=1787776&view=rev
Log:
Felix Converter: support for prefixes with DTOs

Added:
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/PrefixDTO.java
Modified:
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/Util.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=1787776&r1=1787775&r2=1787776&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 Mar 20 14:09:14 2017
@@ -295,17 +295,19 @@ public class ConvertingImpl implements C
         if (targetAsDTO)
             cls = targetClass;
         try {
+            String prefix = Util.getPrefix(cls);
+
             T dto = (T) targetClass.newInstance();
 
             for (Map.Entry entry : (Set<Map.Entry>) m.entrySet()) {
                 Field f = null;
                 try {
-                    f = cls.getDeclaredField(Util.mangleName(entry.getKey().toString()));
-                } catch (NoSuchFieldException e) {
+                    f = cls.getDeclaredField(Util.mangleName(prefix, entry.getKey().toString()));
+                } catch (NoSuchFieldException | NullPointerException e) {
                     try {
-                        f = cls.getField(Util.mangleName(entry.getKey().toString()));
-                    } catch (NoSuchFieldException e1) {
-                        // There is not field with this name
+                        f = cls.getField(Util.mangleName(prefix, entry.getKey().toString()));
+                    } catch (NoSuchFieldException | NullPointerException e1) {
+                        // There is no field with this name
                     }
                 }
 

Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/Util.java?rev=1787776&r1=1787775&r2=1787776&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/Util.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/Util.java
Mon Mar 20 14:09:14 2017
@@ -137,7 +137,7 @@ class Util {
         if (!Modifier.isPublic(f.getModifiers()))
             return null;
 
-        return unMangleName(f.getName());
+        return unMangleName(getPrefix(f.getDeclaringClass()), f.getName());
     }
 
     static Map<String, Set<Method>> getInterfaceKeys(Class<?> intf) {
@@ -212,7 +212,26 @@ class Util {
         return md.invoke(obj);
     }
 
-    static String mangleName(String key) {
+    static String getPrefix(Class<?> cls) {
+        try {
+            Field prefixField = cls.getDeclaredField("PREFIX_");
+            if (prefixField.getType().equals(String.class)) {
+                if ((prefixField.getModifiers() & (Modifier.PUBLIC | Modifier.FINAL |
Modifier.STATIC)) > 0) {
+                    return (String) prefixField.get(null);
+                }
+            }
+        } catch (Exception ex) {
+            // LOG no prefix field
+        }
+        return "";
+    }
+
+    static String mangleName(String prefix, String key) {
+        if (!key.startsWith(prefix))
+            return null;
+
+        key = key.substring(prefix.length());
+
         String res = key.replace("_", "__");
         res = res.replace("$", "$$");
         res = res.replaceAll("[.]([._])", "_\\$$1");
@@ -221,7 +240,7 @@ class Util {
         return res;
     }
 
-    static String unMangleName(String key) {
+    static String unMangleName(String prefix, String key) {
         String res = key.replaceAll("_\\$", ".");
         res = res.replace("__", "\f"); // park double underscore as formfeed char
         res = res.replace('_', '.');
@@ -230,6 +249,6 @@ class Util {
         res = res.replace('\f', '_');  // convert formfeed char back to single underscore
         res = res.replace('\b', '$');  // convert backspace char back go dollar
         // TODO handle Java keywords
-        return res;
+        return prefix + res;
     }
 }

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=1787776&r1=1787775&r2=1787776&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 Mar 20 14:09:14 2017
@@ -878,6 +878,23 @@ public class ConverterTest {
         assertEquals(Long.valueOf(Long.MIN_VALUE), m.get(SomeEnum.VALUE));
     }
 
+    @Test
+    public void testPrefixDTO() {
+        Map<String, String> m = new HashMap<>();
+        m.put("org.foo.bar.width", "327");
+        m.put("length", "12");
+
+        PrefixDTO dto = converter.convert(m).to(PrefixDTO.class);
+        assertEquals(327, dto.width);
+        assertEquals("This one should not be set", 0, dto.length);
+
+        Map<String, String> m2 = converter.convert(dto).to(new TypeReference<HashMap<String,String>>()
{});
+        Map<String, String> expected = new HashMap<>();
+        expected.put("org.foo.bar.width", "327");
+        expected.put("org.foo.bar.length", "0");
+        assertEquals(expected, 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/PrefixDTO.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/PrefixDTO.java?rev=1787776&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/PrefixDTO.java
(added)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/PrefixDTO.java
Mon Mar 20 14:09:14 2017
@@ -0,0 +1,25 @@
+/*
+ * 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 PrefixDTO {
+    public static final String PREFIX_ = "org.foo.bar.";
+
+    public long width;
+
+    public int length;
+}



Mime
View raw message