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
git-svn-id: https://svn.apache.org/repos/asf/geronimo/xbean/trunk@1712321 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java b/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
index 791d2fe..c1fded0 100644
--- a/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
+++ b/xbean-reflect/src/main/java/org/apache/xbean/recipe/RecipeHelper.java
@@ -129,7 +129,8 @@
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 @@
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);
diff --git a/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java b/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java
index e74a346..a7e091f 100644
--- a/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java
+++ b/xbean-reflect/src/test/java/org/apache/xbean/recipe/ObjectRecipeTest.java
@@ -165,6 +165,25 @@
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 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;
+ }
+ }
}
diff --git a/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java b/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java
new file mode 100644
index 0000000..21efbe6
--- /dev/null
+++ b/xbean-reflect/src/test/java/org/apache/xbean/recipe/RecipeHelperTest.java
@@ -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));
+ }
+}