diff --git a/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java b/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java index 499bd198..a93dec60 100644 --- a/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java +++ b/xbean-reflect/src/main/java/org/apache/xbean/recipe/ObjectRecipe.java @@ -24,11 +24,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.EnumSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import org.apache.xbean.propertyeditor.PropertyEditorRegistry; import org.apache.xbean.recipe.ReflectionUtil.*; @@ -270,7 +273,11 @@ protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throw // // clone the properties so they can be used again - Map propertyValues = new LinkedHashMap(properties); + Map propertyValues = options.contains(Option.CASE_INSENSITIVE_PROPERTIES) + ? new TreeMap<>(Comparator.comparing(property -> property.name, String.CASE_INSENSITIVE_ORDER)) + : new LinkedHashMap<>(); + + propertyValues.putAll(properties); // // create the instance @@ -536,6 +543,13 @@ private void setProperty(Object instance, Class clazz, Property propertyName, Ob } private Factory findFactory(Type expectedType) { + Set availableProperties = getProperties().keySet(); + if (options.contains(Option.CASE_INSENSITIVE_PROPERTIES)) { + Set caseInsensitiveProperties = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + caseInsensitiveProperties.addAll(availableProperties); + availableProperties = caseInsensitiveProperties; + } + Class type = getType(); // @@ -547,7 +561,7 @@ private Factory findFactory(Type expectedType) { factoryMethod, constructorArgNames, constructorArgTypes, - getProperties().keySet(), + availableProperties, options); return staticFactory; } catch (MissingFactoryMethodException ignored) { @@ -571,7 +585,7 @@ private Factory findFactory(Type expectedType) { consturctorClass, constructorArgNames, constructorArgTypes, - getProperties().keySet(), + availableProperties, options); return constructor; diff --git a/xbean-reflect/src/test/java/org/apache/xbean/recipe/XBean353Test.java b/xbean-reflect/src/test/java/org/apache/xbean/recipe/XBean353Test.java new file mode 100644 index 00000000..3ad31fa5 --- /dev/null +++ b/xbean-reflect/src/test/java/org/apache/xbean/recipe/XBean353Test.java @@ -0,0 +1,55 @@ +/** + * + * 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 java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class XBean353Test { + + @Test + public void testCreateObjectCaseInsensitive() { + final Map availableProperties = new HashMap<>(); + availableProperties.put("PoolSize", 10); + + final String[] paramNames = new String[]{"poolSize"}; + ObjectRecipe recipe = new ObjectRecipe(Constructor.class, paramNames); + recipe.setAllProperties(availableProperties); + recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES); + + final Object o = recipe.create(); + assertNotNull(o); + assertTrue(o instanceof Constructor); + assertEquals(10, ((Constructor) o).poolSize); + } + + public static class Constructor { + + int poolSize; + + public Constructor(int poolSize) { + this.poolSize = poolSize; + } + } +} \ No newline at end of file