mcconnell 2003/03/16 18:47:53
Modified: merlin/assembly/src/java/org/apache/avalon/assembly/appliance
ContextBuilder.java
Log:
Added support for the declaration of array type to context entry trye criteria. The
update enables an entry declaration to include something like <entry key="whatever"
type="xxxx.yyy.MyClass[]"/>
Revision Changes Path
1.2 +36 -4
avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/ContextBuilder.java
Index: ContextBuilder.java
===================================================================
RCS file:
/home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/ContextBuilder.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ContextBuilder.java 13 Mar 2003 01:03:40 -0000 1.1
+++ ContextBuilder.java 17 Mar 2003 02:47:53 -0000 1.2
@@ -226,7 +226,8 @@
{
try
{
- boolean ok = objectImplementsType( classloader, object,
entry.getType() );
+ boolean ok = objectImplementsType(
+ classloader, object, entry.getType() );
if( ok )
{
map.put( key, object );
@@ -243,7 +244,8 @@
catch( ClassNotFoundException cnfe )
{
final String error =
- "Context criteria for the key '" + key + "' specifies an
unknown type '"
+ "Context criteria for the key '" + key
+ + "' specifies an unknown type '"
+ entry.getType() + "'.";
throw new ContextException( error );
}
@@ -308,10 +310,11 @@
/**
* Check whether the specified value is compatible with specified type.
+ * The supplied type argumement may be a classname or array defintion.
*
* @param value the value
* @param type the desired type
- * @return true if value is compatible with type, false otherwise
+ * @return TRUE if value is compatible with type, FALSE otherwise
*/
private static boolean objectImplementsType(
ClassLoader classloader, final Object value, final String type )
@@ -326,6 +329,35 @@
{
throw new NullPointerException( "value" );
}
+
+ //
+ // if the supplied type argument ends with [] then we are dealing
+ // with an array argument and as such, if the array length is 0
+ // the the type matches anything otherwise we apply a type chack
+ // on the first entry in the array
+ //
+
+ if( type.endsWith( "[]" ) )
+ {
+ if( value instanceof Object[] )
+ {
+ Object[] array = (Object[]) value;
+ if( array.length == 0 )
+ {
+ return true;
+ }
+ else
+ {
+ Object sample = array[0];
+ String subtype = type.substring( type.length() - 2 );
+ return objectImplementsType( classloader, sample, subtype );
+ }
+ }
+ }
+
+ //
+ // otherrwise its a classic object type test
+ //
final Class clazz = value.getClass();
final Class typeClass = classloader.loadClass( type );
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]