Revision: 681
http://stripes.svn.sourceforge.net/stripes/?rev=681&view=rev
Author: bengunter
Date: 2007-12-12 11:18:51 -0800 (Wed, 12 Dec 2007)
Log Message:
-----------
Was not properly checking all the superclasses of every interface implemented
by the class. This is fixed and a test for it has been added.
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
trunk/tests/src/net/sourceforge/stripes/format/DefaultFormatterFactoryTest.java
Modified:
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
2007-12-12 15:49:44 UTC (rev 680)
+++
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
2007-12-12 19:18:51 UTC (rev 681)
@@ -120,13 +120,29 @@
/**
* Search for a formatter class that best matches the requested class,
first checking the
- * specified class, then it's interfaces, then superclasses, and then the
superclasses of its
- * interfaces.
+ * specified class, then all the interfaces it implements, then all its
superclasses and the
+ * interfaces they implement, and finally all the superclasses of the
interfaces implemented by
+ * [EMAIL PROTECTED] targetClass}.
*
* @param targetClass the class of the object that needs to be formatted
* @return the first applicable formatter found or null if no match could
be found
*/
protected Class<? extends Formatter<?>> findFormatterClass(Class<?>
targetClass) {
+ Class<? extends Formatter<?>> formatterClass =
findInSuperclasses(targetClass);
+ if (formatterClass == null)
+ formatterClass = findInInterfaces(targetClass,
targetClass.getInterfaces());
+ return formatterClass;
+ }
+
+ /**
+ * Called first by [EMAIL PROTECTED] #findFormatterClass(Class)}. Search
for a formatter class that best
+ * matches the requested class, first checking the specified class, then
all the interfaces it
+ * implements. If no match is found, repeat the process for each
superclass.
+ *
+ * @param targetClass the class of the object that needs to be formatted
+ * @return the first applicable formatter found or null if no match could
be found
+ */
+ protected Class<? extends Formatter<?>> findInSuperclasses(Class<?>
targetClass) {
// Check for a known formatter for the class
if (formatters.containsKey(targetClass))
return formatters.get(targetClass);
@@ -150,14 +166,33 @@
}
}
- // Check superclasses of implemented interfaces
- for (Class<?> iface : targetClass.getInterfaces()) {
- for (Class<?> superiface : iface.getInterfaces()) {
- if ((formatterClass = formatters.get(superiface)) != null)
- return cacheFormatterClass(targetClass, formatterClass);
- else if ((formatterClass = classCache.get(superiface)) != null)
- return cacheFormatterClass(targetClass, formatterClass);
+ // Nothing found, so cache null
+ return cacheFormatterClass(targetClass, null);
+ }
+
+ /**
+ * Called second by [EMAIL PROTECTED] #findFormatterClass(Class)}, after
+ * [EMAIL PROTECTED] #findInSuperclasses(Class)}. Search for a formatter
class that best matches the
+ * requested class by checking the superclasses of every interface
implemented by
+ * [EMAIL PROTECTED] targetClass}.
+ *
+ * @param targetClass the class of the object that needs to be formatted
+ * @param ifaces an array of interfaces to search
+ * @return the first applicable formatter found or null if no match could
be found
+ */
+ protected Class<? extends Formatter<?>> findInInterfaces(Class<?>
targetClass,
+ Class<?>... ifaces) {
+ Class<? extends Formatter<?>> formatterClass = null;
+ for (Class<?> iface : ifaces) {
+ if ((formatterClass = formatters.get(iface)) != null) {
+ return cacheFormatterClass(targetClass, formatterClass);
}
+ else if ((formatterClass = classCache.get(iface)) != null) {
+ return cacheFormatterClass(targetClass, formatterClass);
+ }
+ else if ((formatterClass = findInInterfaces(targetClass,
iface.getInterfaces())) != null) {
+ return cacheFormatterClass(targetClass, formatterClass);
+ }
}
// Nothing found, so cache null
Modified:
trunk/tests/src/net/sourceforge/stripes/format/DefaultFormatterFactoryTest.java
===================================================================
---
trunk/tests/src/net/sourceforge/stripes/format/DefaultFormatterFactoryTest.java
2007-12-12 15:49:44 UTC (rev 680)
+++
trunk/tests/src/net/sourceforge/stripes/format/DefaultFormatterFactoryTest.java
2007-12-12 19:18:51 UTC (rev 681)
@@ -148,6 +148,28 @@
Assert.assertEquals(YFormatter.class, formatter.getClass());
}
+ public static void main(String[] args) throws Exception {
+ DefaultFormatterFactoryTest test = new DefaultFormatterFactoryTest();
+ test.testFormatterSuperclass();
+ test.testFormatterInterface();
+ test.testNullFormatterIsNeverBestMatch();
+ test.testFormatterSuperclassImplementsInterface();
+ test.testFormatterForInterfaceSuperclass();
+ }
+
+ public void testFormatterForInterfaceSuperclass() throws Exception {
+ DefaultFormatterFactory factory = new DefaultFormatterFactory();
+ factory.init(new DefaultConfiguration());
+
+ Locale locale = Locale.getDefault();
+ Formatter<?> formatter;
+
+ factory.add(IfaceLevel1.class, IfaceLevel1Formatter.class);
+
+ formatter =
factory.getFormatter(ImplementsIfaceWithSuperclasses.class, locale, null, null);
+ Assert.assertEquals(IfaceLevel1Formatter.class, formatter.getClass());
+ }
+
public static class A {
}
@@ -225,4 +247,19 @@
public static class ZFormatter extends DummyFormatter<Z> {
}
+
+ public static interface IfaceLevel1 {
+ }
+
+ public static interface IfaceLevel2 extends IfaceLevel1 {
+ }
+
+ public static interface IfaceLevel3 extends IfaceLevel2 {
+ }
+
+ public static class IfaceLevel1Formatter extends
DummyFormatter<IfaceLevel1> {
+ }
+
+ public static class ImplementsIfaceWithSuperclasses implements IfaceLevel3
{
+ }
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development