You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by db...@apache.org on 2006/11/27 00:13:42 UTC
svn commit: r479452 - 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/MissingPropertiesTest.java
Author: dblevins
Date: Sun Nov 26 15:13:40 2006
New Revision: 479452
URL: http://svn.apache.org/viewvc?view=rev&rev=479452
Log:
XBEAN-63: Optionally Ignore properties that have no accessors
Added:
geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MissingPropertiesTest.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?view=diff&rev=479452&r1=479451&r2=479452
==============================================================================
--- 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 Sun Nov 26 15:13:40 2006
@@ -43,7 +43,7 @@
private final Class[] constructorArgTypes;
private final LinkedHashMap properties;
private final List options = new ArrayList();
-
+ private final Map unsetProperties = new LinkedHashMap();
public ObjectRecipe(Class type) {
this(type.getName());
}
@@ -143,6 +143,7 @@
}
public Object create(ClassLoader classLoader) throws ConstructionException {
+ unsetProperties.clear();
// load the type class
Class typeClass = null;
try {
@@ -177,6 +178,9 @@
// create the instance
Object instance = createInstance(typeClass, propertyValues);
+ boolean allowPrivate = options.contains(Option.PRIVATE_PROPERTIES);
+ boolean ignoreMissingProperties = options.contains(Option.IGNORE_MISSING_PROPERTIES);
+
// set remaining properties
for (Iterator iterator = propertyValues.entrySet().iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
@@ -186,35 +190,47 @@
MissingAccessorException missingSetter = null;
Method setter = null;
try {
- setter = findSetter(typeClass, propertyName, propertyValue, options.contains(Option.PRIVATE_PROPERTIES));
+ setter = findSetter(typeClass, propertyName, propertyValue, allowPrivate);
propertyValue = convert(setter.getParameterTypes()[0], propertyValue);
setter.invoke(instance, new Object[]{propertyValue});
continue;
} catch (MissingAccessorException e) {
- if (!options.contains(Option.FIELD_INJECTION)){
- throw e;
- }
missingSetter = e;
} catch (Exception e) {
throw new ConstructionException("Error setting property: " + setter);
}
- Field field = null;
- try {
- field = findField(typeClass, propertyName, propertyValue, options.contains(Option.PRIVATE_PROPERTIES));
- propertyValue = convert(field.getType(), propertyValue);
- field.set(instance, propertyValue);
- } catch (MissingAccessorException missingField) {
- if (missingField.getMatchLevel() > missingSetter.getMatchLevel()) {
+ MissingAccessorException missingField = null;
+ if (options.contains(Option.FIELD_INJECTION)){
+ Field field = null;
+ try {
+ field = findField(typeClass, propertyName, propertyValue, allowPrivate);
+ propertyValue = convert(field.getType(), propertyValue);
+ field.set(instance, propertyValue);
+ continue;
+ } catch (MissingAccessorException e) {
+ missingField = e;
+ } catch (Exception e) {
+ throw new ConstructionException("Error setting property: " + field);
+ }
+ }
+
+ if (ignoreMissingProperties){
+ unsetProperties.put(propertyName, propertyValue);
+ } else {
+ if (missingField != null && missingField.getMatchLevel() > missingSetter.getMatchLevel()) {
throw missingField;
} else {
throw missingSetter;
}
- } catch (Exception e) {
- throw new ConstructionException("Error setting property: " + field);
}
+
}
return instance;
+ }
+
+ public Map getUnsetProperties() {
+ return new LinkedHashMap(unsetProperties);
}
private Object[] extractConstructorArgs(Map propertyValues, Class[] constructorArgTypes) {
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?view=diff&rev=479452&r1=479451&r2=479452
==============================================================================
--- 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 Sun Nov 26 15:13:40 2006
@@ -22,7 +22,8 @@
public class Option {
public static final Option PRIVATE_PROPERTIES = new Option("PRIVATE_PROPERTIES");
- public static final Option FIELD_INJECTION = new Option("FIELD_INJECTION");
+ public static final Option FIELD_INJECTION = new Option("FIELD_INJECTION");
+ public static final Option IGNORE_MISSING_PROPERTIES = new Option("IGNORE_MISSING_PROPERTIES");
private final String name;
Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MissingPropertiesTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MissingPropertiesTest.java?view=auto&rev=479452
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MissingPropertiesTest.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MissingPropertiesTest.java Sun Nov 26 15:13:40 2006
@@ -0,0 +1,66 @@
+/**
+ * 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 org.apache.xbean.propertyeditor.PropertyEditors;
+
+import java.util.Map;
+
+public class MissingPropertiesTest extends TestCase {
+
+ protected void setUp() throws Exception {
+ PropertyEditors.class.getName();
+ }
+
+ public void testAll() throws Exception {
+ Widget expected = new Widget("uno", 2, true, "quatro", Object.class, "seis");
+
+ ObjectRecipe objectRecipe = new ObjectRecipe(Widget.class, new String[]{"one", "two"}, new Class[]{String.class, int.class});
+ objectRecipe.allow(Option.PRIVATE_PROPERTIES);
+ objectRecipe.allow(Option.FIELD_INJECTION);
+ objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
+
+ objectRecipe.setProperty("one", "uno");
+ objectRecipe.setProperty("two", "2");
+ objectRecipe.setProperty("three", "true");
+ objectRecipe.setProperty("four", "quatro");
+ objectRecipe.setProperty("five", "java.lang.Object");
+ objectRecipe.setProperty("six", "seis");
+ objectRecipe.setProperty("seven", "7");
+ objectRecipe.setProperty("eight", "8");
+
+ Widget actual = (Widget) objectRecipe.create(Widget.class.getClassLoader());
+ assertEquals("widget", expected, actual);
+
+ Map unset = objectRecipe.getUnsetProperties();
+ assertEquals(2, unset.size());
+ assertNotNull("seven", unset.get("seven"));
+ assertNotNull("eight", unset.get("eight"));
+
+ objectRecipe.setProperty("nine", "9");
+ actual = (Widget) objectRecipe.create(Widget.class.getClassLoader());
+ assertEquals("widget", expected, actual);
+
+ unset = objectRecipe.getUnsetProperties();
+ assertEquals(3, unset.size());
+ assertNotNull("seven", unset.get("seven"));
+ assertNotNull("eight", unset.get("eight"));
+ assertNotNull("nine", unset.get("nine"));
+ }
+
+}