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 2007/12/23 04:35:08 UTC

svn commit: r606531 - in /geronimo/xbean/trunk/xbean-reflect/src: main/java/org/apache/xbean/recipe/ObjectRecipe.java main/java/org/apache/xbean/recipe/Option.java test/java/org/apache/xbean/recipe/CaseInsensitivePropertiesTest.java

Author: dblevins
Date: Sat Dec 22 19:35:07 2007
New Revision: 606531

URL: http://svn.apache.org/viewvc?rev=606531&view=rev
Log:
XBEAN-100: Support for case insesnitive property names

Added:
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CaseInsensitivePropertiesTest.java
Modified:
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java?rev=606531&r1=606530&r2=606531&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java Sat Dec 22 19:35:07 2007
@@ -243,12 +243,13 @@
         boolean allowPrivate = options.contains(Option.PRIVATE_PROPERTIES);
         boolean allowStatic = options.contains(Option.STATIC_PROPERTIES);
         boolean ignoreMissingProperties = options.contains(Option.IGNORE_MISSING_PROPERTIES);
+        boolean caseInsesnitive = options.contains(Option.CASE_INSENSITIVE_PROPERTIES);
         // set remaining properties
         for (Map.Entry<Property, Object> entry : RecipeHelper.prioritizeProperties(propertyValues)) {
             Property propertyName = entry.getKey();
             Object propertyValue = entry.getValue();
 
-            setProperty(instance, clazz, propertyName, propertyValue, allowPrivate, allowStatic, ignoreMissingProperties, classLoader);
+            setProperty(instance, clazz, propertyName, propertyValue, allowPrivate, allowStatic, ignoreMissingProperties, caseInsesnitive, classLoader);
         }
 
     }
@@ -263,23 +264,23 @@
         return typeClass;
     }
 
-    private void setProperty(Object instance, Class clazz, Property propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean ignoreMissingProperties, ClassLoader classLoader) {
+    private void setProperty(Object instance, Class clazz, Property propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean ignoreMissingProperties, boolean caseInsesnitive, ClassLoader classLoader) {
         Member member;
         try {
             if (propertyName instanceof SetterProperty){
-                member = new MethodMember(findSetter(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, classLoader));
+                member = new MethodMember(findSetter(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
             } else if (propertyName instanceof FieldProperty){
-                member = new FieldMember(findField(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, classLoader));
+                member = new FieldMember(findField(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
             } else {
                 try {
-                    member = new MethodMember(findSetter(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, classLoader));
+                    member = new MethodMember(findSetter(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
                 } catch (MissingAccessorException noSetter) {
                     if (!options.contains(Option.FIELD_INJECTION)) {
                         throw noSetter;
                     }
 
                     try {
-                        member = new FieldMember(findField(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, classLoader));
+                        member = new FieldMember(findField(clazz, propertyName.name, propertyValue, allowPrivate, allowStatic, caseInsesnitive, classLoader));
                     } catch (MissingAccessorException noField) {
                         throw (noField.getMatchLevel() > noSetter.getMatchLevel())? noField: noSetter;
                     }
@@ -583,13 +584,13 @@
      * @param propertyValue
      * @param allowPrivate
      * @param classLoader
-     * @return
+     * @return field
      */
     public static Method findSetter(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, ClassLoader classLoader) {
-        return findSetter(typeClass, propertyName,  propertyValue, allowPrivate, false, classLoader);
+        return findSetter(typeClass, propertyName,  propertyValue, allowPrivate, false, false, classLoader);
     }
 
-    public static Method findSetter(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, ClassLoader classLoader) {
+    public static Method findSetter(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean caseInsesnitive, ClassLoader classLoader) {
         if (propertyName == null) throw new NullPointerException("name is null");
         if (propertyName.length() == 0) throw new IllegalArgumentException("name is an empty string");
 
@@ -625,7 +626,7 @@
         List<Method> methods = new ArrayList<Method>(Arrays.asList(typeClass.getMethods()));
         methods.addAll(Arrays.asList(typeClass.getDeclaredMethods()));
         for (Method method : methods) {
-            if (method.getName().equals(setterName)) {
+            if (method.getName().equals(setterName) || (caseInsesnitive && method.getName().equalsIgnoreCase(setterName))) {
                 if (method.getParameterTypes().length == 0) {
                     if (matchLevel < 1) {
                         matchLevel = 1;
@@ -720,13 +721,13 @@
      * @param propertyValue
      * @param allowPrivate
      * @param classLoader
-     * @return
+     * @return field
      */
     public static Field findField(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, ClassLoader classLoader) {
-        return findField(typeClass, propertyName,  propertyValue, allowPrivate, false, classLoader);
+        return findField(typeClass, propertyName,  propertyValue, allowPrivate, false, false, classLoader);
     }
 
-    public static Field findField(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, ClassLoader classLoader) {
+    public static Field findField(Class typeClass, String propertyName, Object propertyValue, boolean allowPrivate, boolean allowStatic, boolean caseInsesnitive, ClassLoader classLoader) {
         if (propertyName == null) throw new NullPointerException("name is null");
         if (propertyName.length() == 0) throw new IllegalArgumentException("name is an empty string");
 
@@ -761,7 +762,7 @@
         }
 
         for (Field field : fields) {
-            if (field.getName().equals(propertyName)) {
+            if (field.getName().equals(propertyName) || (caseInsesnitive && field.getName().equalsIgnoreCase(propertyName))) {
 
                 if (!allowPrivate && !Modifier.isPublic(field.getModifiers())) {
                     if (matchLevel < 4) {

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java?rev=606531&r1=606530&r2=606531&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/Option.java Sat Dec 22 19:35:07 2007
@@ -25,6 +25,7 @@
     public static final Option STATIC_PROPERTIES = new Option("STATIC_PROPERTIES");
     public static final Option FIELD_INJECTION = new Option("FIELD_INJECTION");
     public static final Option IGNORE_MISSING_PROPERTIES = new Option("IGNORE_MISSING_PROPERTIES"); 
+    public static final Option CASE_INSENSITIVE_PROPERTIES = new Option("CASE_INSENSITIVE_PROPERTIES"); 
 
     private final String name;
 

Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CaseInsensitivePropertiesTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CaseInsensitivePropertiesTest.java?rev=606531&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CaseInsensitivePropertiesTest.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CaseInsensitivePropertiesTest.java Sat Dec 22 19:35:07 2007
@@ -0,0 +1,38 @@
+/**
+ * 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;
+
+import java.net.URL;
+
+public class CaseInsensitivePropertiesTest extends TestCase {
+
+    public void testSetters() throws Exception {
+        ObjectRecipe objectRecipe = new ObjectRecipe(Person.class);
+        objectRecipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
+        Person expected = new Person("Joe", 21, new URL("http://www.acme.org"));
+
+        objectRecipe.setProperty("naMe", "Joe");
+        objectRecipe.setProperty("aGe", "21");
+        objectRecipe.setProperty("homepage", "http://www.acme.org");
+
+        Person actual = (Person) objectRecipe.create(Person.class.getClassLoader());
+        assertEquals("person", expected, actual);
+    }
+
+}