Browse Source

Added TypeFactory.canCreate() method.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271310 13f79535-47bb-0310-9956-ffa450edef68
master
adammurdoch 23 years ago
parent
commit
b6e062af22
3 changed files with 56 additions and 26 deletions
  1. +30
    -15
      proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java
  2. +17
    -9
      proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java
  3. +9
    -2
      proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java

+ 30
- 15
proposal/myrmidon/src/java/org/apache/myrmidon/components/type/MultiSourceTypeFactory.java View File

@@ -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;
} }


/** /**


+ 17
- 9
proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/DefaultTypeFactory.java View File

@@ -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()


+ 9
- 2
proposal/myrmidon/src/java/org/apache/myrmidon/interfaces/type/TypeFactory.java View File

@@ -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;


Loading…
Cancel
Save