Revision: 1005
http://stripes.svn.sourceforge.net/stripes/?rev=1005&view=rev
Author: bengunter
Date: 2008-11-07 20:46:35 +0000 (Fri, 07 Nov 2008)
Log Message:
-----------
More for STS-614. Deprecated ReflectUtil.getInterfaceInstance() and copied it
to DefaultObjectFactory.newInterfaceInstance(). If the class passed to
DefaultObjectFactory.newInstance() is an interface then it will delegate to
newInterfaceInstance().
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java
trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
Modified:
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
2008-11-07 20:13:33 UTC (rev 1004)
+++
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultActionBeanPropertyBinder.java
2008-11-07 20:46:35 UTC (rev 1005)
@@ -24,7 +24,6 @@
import net.sourceforge.stripes.util.CryptoUtil;
import net.sourceforge.stripes.util.HtmlUtil;
import net.sourceforge.stripes.util.Log;
-import net.sourceforge.stripes.util.ReflectUtil;
import net.sourceforge.stripes.util.bean.BeanUtil;
import net.sourceforge.stripes.util.bean.ExpressionException;
import net.sourceforge.stripes.util.bean.NoSuchPropertyException;
@@ -375,15 +374,8 @@
}
else if (Collection.class.isAssignableFrom(targetType)
&& !Collection.class.isAssignableFrom(valueType)) {
- Collection collection = null;
- if (targetType.isInterface()) {
- collection = (Collection)
ReflectUtil.getInterfaceInstance(targetType);
- }
- else {
- collection = getConfiguration().getObjectFactory().newInstance(
- (Class<? extends Collection>) targetType);
- }
-
+ Collection collection =
getConfiguration().getObjectFactory().newInstance(
+ (Class<? extends Collection>) targetType);
collection.addAll(valueOrValues);
propertyEvaluation.setValue(collection);
}
Modified:
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java
2008-11-07 20:13:33 UTC (rev 1004)
+++
trunk/stripes/src/net/sourceforge/stripes/controller/DefaultObjectFactory.java
2008-11-07 20:46:35 UTC (rev 1005)
@@ -14,6 +14,20 @@
*/
package net.sourceforge.stripes.controller;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
import net.sourceforge.stripes.config.Configuration;
import net.sourceforge.stripes.exception.StripesRuntimeException;
@@ -27,6 +41,23 @@
* @since Stripes 1.5.1
*/
public class DefaultObjectFactory implements ObjectFactory {
+ /**
+ * Holds a map of commonly used interface types (mostly collections) to a
class that implements
+ * the interface and will, by default, be instantiated when an instance of
the interface is
+ * needed.
+ */
+ protected static final Map<Class<?>, Class<?>> interfaceImplementations =
new HashMap<Class<?>, Class<?>>();
+
+ static {
+ interfaceImplementations.put(Collection.class, ArrayList.class);
+ interfaceImplementations.put(List.class, ArrayList.class);
+ interfaceImplementations.put(Set.class, HashSet.class);
+ interfaceImplementations.put(SortedSet.class, TreeSet.class);
+ interfaceImplementations.put(Queue.class, LinkedList.class);
+ interfaceImplementations.put(Map.class, HashMap.class);
+ interfaceImplementations.put(SortedMap.class, TreeMap.class);
+ }
+
private Configuration configuration;
/** Does nothing. */
@@ -47,7 +78,10 @@
*/
public <T> T newInstance(Class<T> clazz) {
try {
- return clazz.newInstance();
+ if (clazz.isInterface())
+ return newInterfaceInstance(clazz);
+ else
+ return clazz.newInstance();
}
catch (InstantiationException e) {
throw new StripesRuntimeException("Could not instantiate " +
clazz, e);
@@ -56,4 +90,46 @@
throw new StripesRuntimeException("Could not instantiate " +
clazz, e);
}
}
+
+ /**
+ * Attempts to determine an implementing class for the interface provided
and instantiate it
+ * using a default constructor.
+ *
+ * @param interfaceType an interface (or abstract class) to make an
instance of
+ * @return an instance of the interface type supplied
+ * @throws InstantiationException if no implementation type has been
configured
+ * @throws IllegalAccessException if thrown by the JVM during class
instantiation
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T newInterfaceInstance(Class<T> interfaceType) throws
InstantiationException,
+ IllegalAccessException {
+ Class impl = getImplementingClass(interfaceType);
+ if (impl == null) {
+ throw new InstantiationException(
+ "Stripes needed to instantiate a property who's declared
type as an " +
+ "interface (which obviously cannot be instantiated. The
interface is not " +
+ "one that Stripes is aware of, so no implementing class
was known. The " +
+ "interface type was: '" + interfaceType.getName() + "'. To
fix this " +
+ "you'll need to do one of three things. 1) Change the
getter/setter methods " +
+ "to use a concrete type so that Stripes can instantiate
it. 2) in the bean's " +
+ "setContext() method pre-instantiate the property so
Stripes doesn't have to. " +
+ "3) Bug the Stripes author ;) If the interface is a JDK
type it can easily be " +
+ "fixed. If not, if enough people ask, a generic way to
handle the problem " +
+ "might get implemented.");
+ }
+ else {
+ return
StripesFilter.getConfiguration().getObjectFactory().newInstance((Class<T>)
impl);
+ }
+ }
+
+ /**
+ * Looks up the default implementing type for the supplied interface. This
is done based on a
+ * static map of known common interface types and implementing classes.
+ *
+ * @param iface an interface for which an implementing class is needed
+ * @return a Class object representing the implementing type, or null if
one is not found
+ */
+ public Class<?> getImplementingClass(Class<?> iface) {
+ return interfaceImplementations.get(iface);
+ }
}
Modified: trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
2008-11-07 20:13:33 UTC (rev 1004)
+++ trunk/stripes/src/net/sourceforge/stripes/util/ReflectUtil.java
2008-11-07 20:46:35 UTC (rev 1005)
@@ -14,6 +14,7 @@
*/
package net.sourceforge.stripes.util;
+import net.sourceforge.stripes.controller.ObjectFactory;
import net.sourceforge.stripes.controller.StripesFilter;
import net.sourceforge.stripes.exception.StripesRuntimeException;
@@ -104,7 +105,9 @@
* @param iface an interface for which an implementing class is needed
* @return a Class object representing the implementing type, or null if
one is
* not found
+ * @deprecated Use [EMAIL PROTECTED] ObjectFactory#newInstance(Class)}
instead.
*/
+ @Deprecated
public static Class<?> getImplementingClass(Class<?> iface) {
return interfaceImplementations.get(iface);
}
@@ -117,7 +120,9 @@
* @return an instance of the interface type supplied
* @throws InstantiationException if no implementation type has been
configured
* @throws IllegalAccessException if thrown by the JVM during class
instantiation
+ * @deprecated Use [EMAIL PROTECTED] ObjectFactory#newInstance(Class)}
instead.
*/
+ @Deprecated
@SuppressWarnings("unchecked")
public static <T> T getInterfaceInstance(Class<T> interfaceType)
throws InstantiationException, IllegalAccessException {
Modified:
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
2008-11-07 20:13:33 UTC (rev 1004)
+++
trunk/stripes/src/net/sourceforge/stripes/util/bean/PropertyExpressionEvaluation.java
2008-11-07 20:46:35 UTC (rev 1005)
@@ -642,9 +642,6 @@
else if (clazz.isEnum()) {
return clazz.getEnumConstants()[0];
}
- else if (clazz.isInterface() ) {
- return ReflectUtil.getInterfaceInstance(clazz);
- }
else {
return
StripesFilter.getConfiguration().getObjectFactory().newInstance(clazz);
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development