git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271310 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -54,6 +54,14 @@ public class MultiSourceTypeFactory | |||||
| m_factorys.put( name, factory ); | m_factorys.put( name, factory ); | ||||
| } | } | ||||
| /** | |||||
| * Determines if this factory can create instances of a particular type. | |||||
| */ | |||||
| public boolean canCreate( final String name ) | |||||
| { | |||||
| return ( findFactory( name ) != null ); | |||||
| } | |||||
| /** | /** | ||||
| * Create a type instance based on name. | * Create a type instance based on name. | ||||
| * | * | ||||
| @@ -64,30 +72,37 @@ public class MultiSourceTypeFactory | |||||
| public Object create( final String name ) | public Object create( final String name ) | ||||
| throws TypeException | throws TypeException | ||||
| { | { | ||||
| TypeFactory factory = getTypeFactory( name ); | |||||
| if( null == factory && null != m_parent ) | |||||
| { | |||||
| factory = m_parent.getTypeFactory( name ); | |||||
| } | |||||
| // Locate the factory to use | |||||
| TypeFactory factory = findFactory( name ); | |||||
| if( null == factory ) | if( null == factory ) | ||||
| { | { | ||||
| final String message = REZ.getString( "no-factory.error", name ); | final String message = REZ.getString( "no-factory.error", name ); | ||||
| throw new TypeException( message ); | throw new TypeException( message ); | ||||
| } | } | ||||
| else | |||||
| // Create the object | |||||
| final Object object = factory.create( name ); | |||||
| if( !m_type.isInstance( object ) ) | |||||
| { | { | ||||
| final Object object = factory.create( name ); | |||||
| final String message = REZ.getString( "mismatched-type.error", name, object.getClass().getName() ); | |||||
| throw new TypeException( message ); | |||||
| } | |||||
| if( !m_type.isInstance( object ) ) | |||||
| { | |||||
| final String message = REZ.getString( "mismatched-type.error", name, object.getClass().getName() ); | |||||
| throw new TypeException( message ); | |||||
| } | |||||
| return object; | |||||
| } | |||||
| return object; | |||||
| /** | |||||
| * Locates the type factory to use for a particular type. | |||||
| */ | |||||
| private TypeFactory findFactory( final String name ) | |||||
| { | |||||
| TypeFactory factory = getTypeFactory( name ); | |||||
| if( null == factory && null != m_parent ) | |||||
| { | |||||
| factory = m_parent.getTypeFactory( name ); | |||||
| } | } | ||||
| return factory; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -58,6 +58,14 @@ public class DefaultTypeFactory | |||||
| m_classNames.put( name, className ); | m_classNames.put( name, className ); | ||||
| } | } | ||||
| /** | |||||
| * Determines if this factory can create instances of a particular type. | |||||
| */ | |||||
| public boolean canCreate( String name ) | |||||
| { | |||||
| return ( getClassName( name ) != null ); | |||||
| } | |||||
| /** | /** | ||||
| * Create a type instance with appropriate name. | * Create a type instance with appropriate name. | ||||
| * | * | ||||
| @@ -68,7 +76,15 @@ public class DefaultTypeFactory | |||||
| public Object create( final String name ) | public Object create( final String name ) | ||||
| throws TypeException | throws TypeException | ||||
| { | { | ||||
| // Determine the name of the class to instantiate | |||||
| final String className = getClassName( name ); | final String className = getClassName( name ); | ||||
| if( null == className ) | |||||
| { | |||||
| final String message = REZ.getString( "no-mapping.error", name ); | |||||
| throw new TypeException( message ); | |||||
| } | |||||
| // Instantiate the object | |||||
| try | try | ||||
| { | { | ||||
| final ClassLoader classLoader = getClassLoader(); | final ClassLoader classLoader = getClassLoader(); | ||||
| @@ -83,16 +99,8 @@ public class DefaultTypeFactory | |||||
| } | } | ||||
| private String getClassName( final String name ) | private String getClassName( final String name ) | ||||
| throws TypeException | |||||
| { | { | ||||
| final String className = (String)m_classNames.get( name ); | |||||
| if( null == className ) | |||||
| { | |||||
| final String message = REZ.getString( "no-mapping.error", name ); | |||||
| throw new TypeException( message ); | |||||
| } | |||||
| return className; | |||||
| return (String)m_classNames.get( name ); | |||||
| } | } | ||||
| protected ClassLoader getClassLoader() | protected ClassLoader getClassLoader() | ||||
| @@ -15,12 +15,19 @@ package org.apache.myrmidon.interfaces.type; | |||||
| */ | */ | ||||
| public interface TypeFactory | public interface TypeFactory | ||||
| { | { | ||||
| /** | |||||
| * Determines if this factory can create instances of a particular type. | |||||
| * | |||||
| * @param name the type name. | |||||
| */ | |||||
| boolean canCreate( String name ); | |||||
| /** | /** | ||||
| * Create a type instance based on name. | * Create a type instance based on name. | ||||
| * | * | ||||
| * @param name the name | |||||
| * @param name the type name | |||||
| * @return the type instance | * @return the type instance | ||||
| * @exception TypeException if an error occurs | |||||
| * @exception TypeException if the type is unknown, or an error occurs. | |||||
| */ | */ | ||||
| Object create( String name ) | Object create( String name ) | ||||
| throws TypeException; | throws TypeException; | ||||