felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1788269 - in /felix/trunk/converter/converter/src: main/java/org/apache/felix/converter/impl/ConvertingImpl.java main/java/org/apache/felix/converter/impl/Util.java test/java/org/apache/felix/converter/impl/ConverterMapTest.java
Date Thu, 23 Mar 2017 15:26:43 GMT
Author: davidb
Date: Thu Mar 23 15:26:43 2017
New Revision: 1788269

URL: http://svn.apache.org/viewvc?rev=1788269&view=rev
Log:
Felix Converter - Support converting to SingleElementAnnotations

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/ConverterMapTest.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=1788269&r1=1788268&r2=1788269&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
Thu Mar 23 15:26:43 2017
@@ -16,6 +16,7 @@
  */
 package org.apache.felix.converter.impl;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -450,6 +451,24 @@ public class ConvertingImpl implements C
             new InvocationHandler() {
                 @Override
                 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
+                    Class<?> mdDecl = method.getDeclaringClass();
+                    if (mdDecl.equals(Object.class))
+                        switch (method.getName()) {
+                        case "equals":
+                            return proxy == args[0];
+                        case "hashCode":
+                            return System.identityHashCode(proxy);
+                        case "toString":
+                            return "Proxy for " + targetCls;
+                        default:
+                            throw new UnsupportedOperationException("Method " + method +
" not supported on proxy for " + targetCls);
+                        }
+                    if (mdDecl.equals(Annotation.class)) {
+                        if ("annotationType".equals(method.getName()) && method.getParameterTypes().length
== 0) {
+                            return targetCls;
+                        }
+                    }
+
                     String propName = Util.getInterfacePropertyName(method, Util.getSingleElementAnnotationKey(targetCls,
proxy), proxy);
                     if (propName == null)
                         return null;

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=1788269&r1=1788268&r2=1788269&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
Thu Mar 23 15:26:43 2017
@@ -244,7 +244,7 @@ class Util {
             Annotation.class.equals(md.getDeclaringClass()))
             return null; // do not use any methods on the Object or Annotation class as a
accessor
 
-        if ("annotationType".equals(md.getName())) {
+        if ("annotationType".equals(md.getName()) && md.getParameterTypes().length
== 0) {
             try {
                 Object cls = md.invoke(object);
                 if (cls instanceof Class && ((Class<?>) cls).isAnnotation())

Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java?rev=1788269&r1=1788268&r2=1788269&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
(original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
Thu Mar 23 15:26:43 2017
@@ -40,6 +40,7 @@ import org.osgi.util.converter.TypeRefer
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
@@ -260,7 +261,7 @@ public class ConverterMapTest {
     }
 
     @Test
-    @SuppressWarnings({ "unchecked", "rawtypes" })
+    @SuppressWarnings({ "rawtypes", "unchecked" })
     public void testSingleElementAnnotation() {
     	class MySingleElementAnnotation implements SingleElementAnnotation {
             @Override
@@ -284,7 +285,10 @@ public class ConverterMapTest {
     	assertArrayEquals(new String[] {"hi", "there"}, (String []) m.get("single.element.annotation"));
     	assertEquals(42L, m.get("somethingElse"));
 
-    	// TODO support conversion in the other direction too.
+    	m.put("somethingElse", 51.0);
+    	SingleElementAnnotation sea2 = converter.convert(m).to(SingleElementAnnotation.class);
+    	assertArrayEquals(new String[] {"hi", "there"}, sea2.value());
+    	assertEquals(51L, sea2.somethingElse());
     }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
@@ -298,6 +302,25 @@ public class ConverterMapTest {
         assertSame(m.get("key"), cm.get("key"));
     }
 
+    @Test
+    public void testProxyObjectMethodsInterface() {
+        Map<String, String> m = new HashMap<>();
+        TestInterface ti = converter.convert(m).to(TestInterface.class);
+        assertTrue(ti.equals(ti));
+        assertFalse(ti.equals(new Object()));
+        assertFalse(ti.equals(null));
+
+        assertNotNull(ti.toString());
+        assertTrue(ti.hashCode() != 0);
+    }
+
+    @Test
+    public void testProxyObjectMethodsAnnotation() {
+        Map<String, String> m = new HashMap<>();
+        TestAnnotation ta = converter.convert(m).to(TestAnnotation.class);
+        assertTrue(ta.equals(ta));
+    }
+
     interface TestInterface {
         String foo();
         int bar();



Mime
View raw message