From 0fdacdfc1a598078da48d77a24f2b7479e9bffb3 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sun, 27 Jan 2002 23:41:18 +0000 Subject: [PATCH] Removed "nasty hack-o-rama" by re-catching and re-throwing ConfigurationExceptions up call stack. Added support for reflection picking up typed adders Added suppot for adders that tak a Configuration object git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270968 13f79535-47bb-0310-9956-ffa450edef68 --- .../configurer/DefaultConfigurer.java | 118 +++++++++++++----- 1 file changed, 85 insertions(+), 33 deletions(-) diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java b/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java index 4203b7bd8..d398e305d 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/components/configurer/DefaultConfigurer.java @@ -70,16 +70,7 @@ public class DefaultConfigurer final Context context ) throws ConfigurationException { - try - { - configureObject( object, configuration, context ); - } - catch( InvocationTargetException ite ) - { - // A configuration exception thrown from a nested object. Unpack - // and re-throw - throw (ConfigurationException)ite.getTargetException(); - } + configureObject( object, configuration, context ); } /** @@ -88,7 +79,7 @@ public class DefaultConfigurer private void configureObject( final Object object, final Configuration configuration, final Context context ) - throws ConfigurationException, InvocationTargetException + throws ConfigurationException { if( object instanceof Configurable ) { @@ -122,6 +113,11 @@ public class DefaultConfigurer REZ.getString( "no-such-attribute.error", elemName, name ); throw new ConfigurationException( message, nspe ); } + catch( final ConfigurationException ce ) + { + ce.fillInStackTrace(); + throw ce; + } catch( final CascadingException ce ) { final String message = @@ -146,6 +142,11 @@ public class DefaultConfigurer REZ.getString( "no-content.error", elemName ); throw new ConfigurationException( message, nspe ); } + catch( final ConfigurationException ce ) + { + ce.fillInStackTrace(); + throw ce; + } catch( final CascadingException ce ) { final String message = @@ -170,6 +171,11 @@ public class DefaultConfigurer REZ.getString( "no-such-element.error", elemName, name ); throw new ConfigurationException( message, nspe ); } + catch( final ConfigurationException ce ) + { + ce.fillInStackTrace(); + throw ce; + } catch( final CascadingException ce ) { final String message = @@ -230,7 +236,7 @@ public class DefaultConfigurer private void configureElement( final ConfigurationState state, final Configuration element, final Context context ) - throws CascadingException, InvocationTargetException + throws CascadingException { final String elementName = element.getName(); if( elementName.toLowerCase().endsWith( "-ref" ) ) @@ -251,46 +257,92 @@ public class DefaultConfigurer private void configureInline( final ConfigurationState state, final Configuration element, final Context context ) - throws CascadingException, InvocationTargetException + throws CascadingException { - final String elementName = element.getName(); + final String name = element.getName(); // Locate the configurer for the child element - final PropertyConfigurer childConfigurer = state.getConfigurer().getProperty( elementName ); + final PropertyConfigurer childConfigurer = state.getConfigurer().getProperty( name ); + + // Create & configure the child element + final Object child = + setupChild( state, element, context, childConfigurer ); - // Create the child element + // Set the child element + childConfigurer.addValue( state, child ); + } + + private Object setupChild( final ConfigurationState state, + final Configuration element, + final Context context, + final PropertyConfigurer childConfigurer ) + throws ConfigurationException + { + final String name = element.getName(); + final Class type = childConfigurer.getType(); Object child = childConfigurer.createValue( state ); - if( child == null ) + + if( null == child && Configuration.class == type ) + { + //special case where you have add(Configuration) + return element; + } + else if( null == child ) { // Create an instance using the default constructor - try + if( type.isInterface() ) { - child = childConfigurer.getType().newInstance(); + child = createdTypedObject( name, type ); + configureObject( child, element, context ); } - catch( final Exception e ) + else { - final String message = - REZ.getString( "create-object.error", - childConfigurer.getType().getName() ); - throw new ConfigurationException( message, e ); + child = createObject( type ); + configureObject( child, element, context ); } } + configureObject( child, element, context ); + return child; + } - // Configure the child element + /** + * Utility method to create an instance of the + * specified type that satisfied supplied interface. + */ + private Object createdTypedObject( final String name, + final Class type ) + throws ConfigurationException + { try { - configureObject( child, element, context ); + return type.newInstance(); } - catch( final ConfigurationException ce ) + catch( final Exception e ) { - // Nasty hack-o-rama, used to get this exception up through - // the stack of doConfigure() calls. This is unpacked by the - // top-most configure() call, and rethrown. - throw new InvocationTargetException( ce ); + final String message = + REZ.getString( "create-object.error", + type.getName() ); + throw new ConfigurationException( message, e ); } + } - // Set the child element - childConfigurer.addValue( state, child ); + /** + * Utility method to instantiate an instance of the specified class. + */ + private Object createObject( final Class type ) + throws ConfigurationException + { + try + { + return type.newInstance(); + } + catch( final Exception e ) + { + final String message = + REZ.getString( "create-object.error", + type.getName() ); + throw new ConfigurationException( message, e ); + } } /**