You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xbean-scm@geronimo.apache.org by db...@apache.org on 2008/03/11 05:21:48 UTC
svn commit: r635797 - in /geronimo/xbean/trunk/xbean-reflect/src:
main/java/org/apache/xbean/propertyeditor/ test/java/org/apache/xbean/recipe/
Author: dblevins
Date: Mon Mar 10 21:21:41 2008
New Revision: 635797
URL: http://svn.apache.org/viewvc?rev=635797&view=rev
Log:
XBEAN-104: Enum support
Added:
geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/EnumConverter.java
geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/EnumTest.java
Modified:
geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/PropertyEditors.java
geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/GenericCollectionsTest.java
Added: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/EnumConverter.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/EnumConverter.java?rev=635797&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/EnumConverter.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/EnumConverter.java Mon Mar 10 21:21:41 2008
@@ -0,0 +1,49 @@
+/**
+ * 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.propertyeditor;
+
+import java.lang.reflect.Method;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class EnumConverter extends AbstractConverter {
+
+ public EnumConverter(Class type) {
+ super(type);
+ }
+
+ protected Object toObjectImpl(String text) {
+ Class type = getType();
+
+ try {
+ return Enum.valueOf(type, text);
+ } catch (Exception cause) {
+ try {
+ int index = Integer.parseInt(text);
+ Method method = type.getMethod("values");
+ Object[] values = (Object[]) method.invoke(null);
+ return values[index];
+ } catch (NumberFormatException e) {
+ } catch (Exception e) {
+ cause = e;
+ }
+
+ throw new PropertyEditorException("Value \"" + text + "\" cannot be converted to enum type " + type.getName(), cause);
+ }
+ }
+}
Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/PropertyEditors.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/PropertyEditors.java?rev=635797&r1=635796&r2=635797&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/PropertyEditors.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/PropertyEditors.java Mon Mar 10 21:21:41 2008
@@ -316,11 +316,16 @@
return null;
}
+
Converter converter = (Converter) registry.get(type);
// we're outta here if we got one.
if (converter != null) {
return converter;
+ }
+
+ if (Enum.class.isAssignableFrom(clazz)){
+ return new EnumConverter(clazz);
}
Class[] declaredClasses = clazz.getDeclaredClasses();
Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/EnumTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/EnumTest.java?rev=635797&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/EnumTest.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/EnumTest.java Mon Mar 10 21:21:41 2008
@@ -0,0 +1,52 @@
+/**
+ * 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 junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class EnumTest extends TestCase {
+
+ public void test() throws Exception {
+
+ Canvas expected = new Canvas();
+ expected.color = Color.RED;
+
+ ObjectRecipe recipe = new ObjectRecipe(Canvas.class);
+ recipe.setProperty("color", "RED");
+
+ Canvas actual = (Canvas) recipe.create();
+
+ assertEquals("Color", expected.getColor(), actual.getColor());
+
+ }
+
+ public static class Canvas {
+
+ public Color color;
+
+ public Color getColor() {
+ return color;
+ }
+ }
+
+ public static enum Color {
+ RED, GREEN, BLUE;
+ }
+}
Modified: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/GenericCollectionsTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/GenericCollectionsTest.java?rev=635797&r1=635796&r2=635797&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/GenericCollectionsTest.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/GenericCollectionsTest.java Mon Mar 10 21:21:41 2008
@@ -55,6 +55,11 @@
expected.listOfURI.add(new URI("green://verde"));
expected.listOfURI.add(new URI("blue://azul"));
+ expected.listOfEnums = new ArrayList<Color>();
+ expected.listOfEnums.add(Color.RED);
+ expected.listOfEnums.add(Color.GREEN);
+ expected.listOfEnums.add(Color.BLUE);
+
expected.mapOfClass = new LinkedHashMap<String, Class>();
expected.mapOfClass.put("Rojo", Red.class);
expected.mapOfClass.put("Verde", Green.class);
@@ -65,26 +70,39 @@
expected.mapOfURI.put(new URI("green://verde"), "Verde");
expected.mapOfURI.put(new URI("blue://azul"), "Azul");
+ expected.mapOfEnums = new LinkedHashMap<String, Color>();
+ expected.mapOfEnums.put("RED", Color.RED);
+ expected.mapOfEnums.put("GREEN", Color.GREEN);
+ expected.mapOfEnums.put("BLUE", Color.BLUE);
+
expected.setOfClass = new LinkedHashSet<Class>();
expected.setOfClass.add(Red.class);
expected.setOfClass.add(Green.class);
expected.setOfClass.add(Blue.class);
- expected.setOfURI = new LinkedHashSet();
+ expected.setOfURI = new LinkedHashSet<URI>();
expected.setOfURI.add(new URI("red://rojo"));
expected.setOfURI.add(new URI("green://verde"));
expected.setOfURI.add(new URI("blue://azul"));
+ expected.setOfEnums = new LinkedHashSet<Color>();
+ expected.setOfEnums.add(Color.RED);
+ expected.setOfEnums.add(Color.GREEN);
+ expected.setOfEnums.add(Color.BLUE);
+
ObjectRecipe recipe = new ObjectRecipe(Something.class);
recipe.setProperty("plainList", toString(expected.plainList));
recipe.setProperty("plainMap", toString(expected.plainMap));
recipe.setProperty("listOfClass", toString(expected.listOfClass));
recipe.setProperty("listOfURI", toString(expected.listOfURI));
+ recipe.setProperty("listOfEnums", toString(expected.listOfEnums));
recipe.setProperty("mapOfClass", toString(expected.mapOfClass));
recipe.setProperty("mapOfURI", toString(expected.mapOfURI));
+ recipe.setProperty("mapOfEnums", toString(expected.mapOfEnums));
recipe.setProperty("setOfClass", toString(expected.setOfClass));
recipe.setProperty("setOfURI", toString(expected.setOfURI));
+ recipe.setProperty("setOfEnums", toString(expected.setOfEnums));
Something actual = (Something) recipe.create();
@@ -140,14 +158,20 @@
public List<URI> listOfURI;
+ public List<Color> listOfEnums;
+
public Map<String,Class> mapOfClass;
public Map<URI, String> mapOfURI;
+ public Map<String, Color> mapOfEnums;
+
public Set<Class> setOfClass;
public Set<URI> setOfURI;
+ public Set<Color> setOfEnums;
+
public List getPlainList() {
return plainList;
}
@@ -180,9 +204,26 @@
return setOfURI;
}
+ public List<Color> getListOfEnums() {
+ return listOfEnums;
+ }
+
+ public Map<String, Color> getMapOfEnums() {
+ return mapOfEnums;
+ }
+
+ public Set<Color> getSetOfEnums() {
+ return setOfEnums;
+ }
}
public static class Red {}
public static class Green {}
public static class Blue {}
+
+
+ public static enum Color {
+ RED, GREEN, BLUE;
+ }
+
}