org.cojen.util
Class QuickConstructorGenerator

java.lang.Object
  extended by org.cojen.util.QuickConstructorGenerator

public class QuickConstructorGenerator
extends Object

Generates code to invoke constructors. This is a replacement for Constructor which is easier to use and performs better. In one tested situation, overall performance was improved by about 10%.

QuickConstructorGenerator is not general purpose however, as the parameters to the constructor must be known, and the constructor must be public. It is intended to be used for constructing instances of auto-generated classes. The exact parameters may be known at compile time, but the actual object type is not.

Since:
2.1
Author:
Brian S O'Neill

Constructor Summary
QuickConstructorGenerator()
           
 
Method Summary
static
<F> F
getInstance(Class<?> objectType, Class<F> factory)
          Returns a factory instance for one type of object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QuickConstructorGenerator

public QuickConstructorGenerator()
Method Detail

getInstance

public static <F> F getInstance(Class<?> objectType,
                                Class<F> factory)
Returns a factory instance for one type of object. Each method in the interface defines a constructor via its parameters. Any checked exceptions declared thrown by the constructor must also be declared by the method. The method return types can be the same type as the constructed object or a supertype.

Here is a contrived example for constructing strings. In practice, such a string factory is is useless, since the "new" operator can be invoked directly.

 public interface StringFactory {
     String newEmptyString();

     String newStringFromChars(char[] chars);

     String newStringFromBytes(byte[] bytes, String charsetName)
         throws UnsupportedEncodingException;
 }
 
Here's an example of it being used:
 StringFactory sf = QuickConstructorGenerator.getInstance(String.class, StringFactory.class);
 ...
 String str = sf.newStringFromChars(new char[] {'h', 'e', 'l', 'l', 'o'});
 

Parameters:
objectType - type of object to construct
factory - interface defining which objects can be constructed
Throws:
IllegalArgumentException - if factory type is not an interface or if it is malformed


Copyright © 2004-2008 Brian S O'Neill. All Rights Reserved.