felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: r1692034 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/helper/Coercions.java test/java/org/apache/felix/scr/impl/helper/AnnotationTest.java
Date Mon, 20 Jul 2015 22:32:50 GMT
Author: djencks
Date: Mon Jul 20 22:32:49 2015
New Revision: 1692034

URL: http://svn.apache.org/r1692034
Log:
FELIX-4967 handle char and char types in config annotations, and fix boolean handling

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/Coercions.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/AnnotationTest.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/Coercions.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/Coercions.java?rev=1692034&r1=1692033&r2=1692034&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/Coercions.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/Coercions.java Mon Jul
20 22:32:49 2015
@@ -37,6 +37,7 @@ public class Coercions
 //    | Double | Byte | Short
 //| Character | Boolean
     private static final byte byte0 = 0;
+    private static final char char0 = 0;
     private static final double double0 = 0;
     private static final float float0 = 0;
     private static final int int0 = 0;
@@ -53,6 +54,10 @@ public class Coercions
         {
             return coerceToBoolean(raw);
         }
+        if (type == Character.class || type == char.class)
+        {
+            return coerceToChar(raw);
+        }
         if (type == Class.class)
         {
             return coerceToClass(raw, bundle);
@@ -123,7 +128,37 @@ public class Coercions
         {
             return 0;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
+    }
+    
+    public static char coerceToChar(Object o)
+    {
+        o = multipleToSingle( o, byte0 );
+        if (o instanceof Character)
+        {
+            return (Character)o;
+        }
+        if (o instanceof String)
+        {
+            if (((String)o).length() > 0) 
+            {
+                return ((String)o).charAt(0);
+            }
+            return char0;
+        }
+        if (o instanceof Boolean)
+        {
+            return (Boolean)o? 1: char0;
+        }
+        if (o instanceof Number)
+        {
+            return (char)((Number)o).intValue();
+        }
+        if (o == null) 
+        {
+            return char0;
+        }
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static double coerceToDouble(Object o)
@@ -160,7 +195,7 @@ public class Coercions
         {
             return 0;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static float coerceToFloat(Object o)
@@ -197,7 +232,7 @@ public class Coercions
         {
             return 0;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static int coerceToInteger(Object o)
@@ -234,7 +269,7 @@ public class Coercions
         {
             return 0;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static long coerceToLong(Object o)
@@ -271,7 +306,7 @@ public class Coercions
         {
             return 0;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static short coerceToShort(Object o)
@@ -308,7 +343,7 @@ public class Coercions
         {
             return 0;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static String coerceToString(Object o)
@@ -350,13 +385,13 @@ public class Coercions
         }
         if (o instanceof Number)
         {
-            return ((Number)o).intValue() != 0;
+            return ((Number)o).doubleValue() != 0D;
         }
         if (o == null) 
         {
             return false;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static Class<?> coerceToClass(Object o, Bundle b)
@@ -377,7 +412,7 @@ public class Coercions
                 throw new ComponentException(e);
             }
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     public static <T extends Enum<T>> T coerceToEnum(Object o, Class<T>
clazz)
@@ -398,7 +433,7 @@ public class Coercions
         {
             return null;
         }
-        throw new ComponentException( "Unrecognized input type: " + o);
+        throw new ComponentException( "Unrecognized input value: " + o + " of type: " + o.getClass());
     }
     
     private static Object multipleToSingle(Object o, Object defaultValue)

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/AnnotationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/AnnotationTest.java?rev=1692034&r1=1692033&r2=1692034&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/AnnotationTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/impl/helper/AnnotationTest.java Mon
Jul 20 22:32:49 2015
@@ -56,6 +56,7 @@ public class AnnotationTest extends Test
     public @interface A1 {
         boolean bool();
         byte byt();
+        char cha();
         Class<?> clas();
         E1 e1();
         double doubl();
@@ -90,6 +91,7 @@ public class AnnotationTest extends Test
     {
         assertEquals(true, a.bool());
         assertEquals((byte)12, a.byt());
+        assertEquals('c', a.cha());
         assertEquals(String.class, a.clas());
         assertEquals(E1.a, a.e1());
         assertEquals(3.14d, a.doubl());
@@ -122,9 +124,10 @@ public class AnnotationTest extends Test
 
     private Map<String, Object> allValues()
     {
-        Map<String, Object> values = new HashMap();
+        Map<String, Object> values = new HashMap<String, Object>();
         values.put("bool", "true");
         values.put("byt", 12l);
+        values.put("cha", 'c');
         values.put("clas", String.class.getName());
         values.put("e1", E1.a.toString());
         values.put("doubl", "3.14");
@@ -146,6 +149,7 @@ public class AnnotationTest extends Test
         A1 a = (A1) o;
         assertEquals(false, a.bool());
         assertEquals((byte)0, a.byt());
+        assertEquals((char)0, a.cha());
         assertEquals(null, a.clas());
         assertEquals(null, a.e1());
         assertEquals(0d, a.doubl());
@@ -159,6 +163,7 @@ public class AnnotationTest extends Test
     public @interface A2 {
         boolean bool() default true;
         byte byt() default 5;
+        char cha() default 'a';
         Class<?> clas() default Integer.class;
         E1 e1() default E1.b;
         double doubl() default -2;
@@ -179,6 +184,7 @@ public class AnnotationTest extends Test
         A2 a = (A2) o;
         assertEquals(true, a.bool());
         assertEquals((byte)12, a.byt());
+        assertEquals('c', a.cha());
         assertEquals(String.class, a.clas());
         assertEquals(E1.a, a.e1());
         assertEquals(3.14d, a.doubl());
@@ -192,6 +198,7 @@ public class AnnotationTest extends Test
     public @interface A1Arrays {
         boolean[] bool();
         byte[] byt();
+        char[] cha();
         Class<?>[] clas();
         E1[] e1();
         double[] doubl();
@@ -212,6 +219,7 @@ public class AnnotationTest extends Test
         A1Arrays a = (A1Arrays) o;
         assertEquals(null, a.bool());
         assertEquals(null, a.byt());
+        assertEquals(null, a.cha());
         assertEquals(null, a.clas());
         assertEquals(null, a.e1());
         assertEquals(null, a.doubl());
@@ -232,6 +240,7 @@ public class AnnotationTest extends Test
         A1Arrays a = (A1Arrays) o;
         assertArrayEquals(new boolean[] {true}, a.bool());
         assertArrayEquals(new byte[] {(byte)12}, a.byt());
+        assertArrayEquals(new char[] {'c'}, a.cha());
         assertArrayEquals(new Class<?>[] {String.class}, a.clas());
         assertArrayEquals(new E1[] {E1.a}, a.e1());
         assertArrayEquals(new double[] {3.14d}, a.doubl());
@@ -260,6 +269,7 @@ public class AnnotationTest extends Test
         Map<String, Object> values = new HashMap();
         values.put("bool", new boolean[] {true, false});
         values.put("byt", new byte[] {12, 3});
+        values.put("cha", new char[] {'c', 'h', 'a', 'r'});
         values.put("clas", new String[] {String.class.getName(), Integer.class.getName()});
         values.put("e1", new String[] {E1.a.name(), E1.b.name()});
         values.put("doubl", new double[] {3.14, 2.78, 9});
@@ -308,6 +318,7 @@ public class AnnotationTest extends Test
         A1Arrays a = (A1Arrays) o;
         assertArrayEquals(new boolean[] {true, false}, a.bool());
         assertArrayEquals(new byte[] {12, 3}, a.byt());
+        assertArrayEquals(new char[] {'c', 'h', 'a', 'r'}, a.cha());
         assertArrayEquals(new Class<?>[] {String.class, Integer.class}, a.clas());
         assertArrayEquals(new E1[] {E1.a, E1.b}, a.e1());
         assertArrayEquals(new double[] {3.14, 2.78, 9}, a.doubl());



Mime
View raw message