geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1712321 - in /geronimo/xbean/trunk/xbean-reflect/src: main/java/org/apache/xbean/recipe/RecipeHelper.java test/java/org/apache/xbean/recipe/ObjectRecipeTest.java test/java/org/apache/xbean/recipe/RecipeHelperTest.java
Date Tue, 03 Nov 2015 16:29:50 GMT
Author: rmannibucau
Date: Tue Nov  3 16:29:50 2015
New Revision: 1712321

URL: http://svn.apache.org/viewvc?rev=1712321&view=rev
Log:
XBEAN-290 yes String are char[] but it is not natural to write a char[] as c,h,a,r,a,c,t,e,r
so making a shortcut for these common types

Added:
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java
Modified:
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java?rev=1712321&r1=1712320&r2=1712321&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
(original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
Tue Nov  3 16:29:50 2015
@@ -129,7 +129,8 @@ public final class RecipeHelper {
             Recipe recipe = (Recipe) propertyValue;
             return recipe.canCreate(type);
         }
-        return (propertyValue instanceof String && PropertyEditors.canConvert(toClass(type)));
+        return (propertyValue instanceof String && PropertyEditors.canConvert(toClass(type)))
+            || (type == String.class && char[].class.isInstance(propertyValue));
     }
 
     public static boolean isAssignableFrom(Class expected, Class actual) {
@@ -167,6 +168,14 @@ public final class RecipeHelper {
             value = recipe.create(expectedType, lazyRefAllowed);
         }
 
+        // some shortcuts for common string operations
+        if (char[].class == expectedType && String.class.isInstance(value)) {
+            return String.class.cast(value).toCharArray();
+        }
+        if (String.class == expectedType && char[].class.isInstance(value)) {
+            return new String(char[].class.cast(value));
+        }
+
         if (value instanceof String && (expectedType != Object.class)) {
             String stringValue = (String) value;
             value = PropertyEditors.getValue(expectedType, stringValue);

Modified: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java?rev=1712321&r1=1712320&r2=1712321&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java
(original)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java
Tue Nov  3 16:29:50 2015
@@ -165,6 +165,25 @@ public class ObjectRecipeTest extends Te
         assertEquals(10, component.getComponent().getBox().getHeight());
         assertEquals(20, component.getComponent().getBox().getWidth());
     }
+
+    public void testStringCharArray() {
+        {
+            final ObjectRecipe recipe = new ObjectRecipe(StringCharArray.class);
+            recipe.setProperty("chars", "v1");
+            recipe.setProperty("string", "v2");
+            final StringCharArray v = StringCharArray.class.cast(recipe.create());
+            assertEquals("v1", new String(v.chars));
+            assertEquals("v2", v.string);
+        }
+        {
+            final ObjectRecipe recipe = new ObjectRecipe(StringCharArray.class);
+            recipe.setProperty("chars", "v1".toCharArray());
+            recipe.setProperty("string", "v2".toCharArray());
+            final StringCharArray v = StringCharArray.class.cast(recipe.create());
+            assertEquals("v1", new String(v.chars));
+            assertEquals("v2", v.string);
+        }
+    }
     
     public static class Component {
                 
@@ -199,4 +218,17 @@ public class ObjectRecipeTest extends Te
         }                
             
     }
+
+    public static class StringCharArray {
+        private char[] chars;
+        private String string;
+
+        public void setChars(final char[] chars) {
+            this.chars = chars;
+        }
+
+        public void setString(final String string) {
+            this.string = string;
+        }
+    }
 }

Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java?rev=1712321&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java
(added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java
Tue Nov  3 16:29:50 2015
@@ -0,0 +1,37 @@
+/**
+ * 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.xbean.recipe;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class RecipeHelperTest {
+    @Test
+    public void stringCharArrayIsConvertable() {
+        assertTrue(RecipeHelper.isConvertable(char[].class, "foo"));
+        assertTrue(RecipeHelper.isConvertable(String.class, "foo".toCharArray()));
+    }
+
+    @Test
+    public void stringCharArrayConvert() {
+        assertArrayEquals("foo".toCharArray(), char[].class.cast(RecipeHelper.convert(char[].class,
"foo", false)));
+        assertEquals("foo", RecipeHelper.convert(String.class, "foo".toCharArray(), false));
+    }
+}



Mime
View raw message