uchicago.src.sim.util
Class ByteCodeBuilder

java.lang.Object
  extended by uchicago.src.sim.util.ByteCodeBuilder

public class ByteCodeBuilder
extends java.lang.Object

A factory class for dynamically creating instances of a few object types. This factory generates the bytecode for the classes, and then instantiates instances of those classes. Reflection is not used, and the created objects should be just as fast as manually coded counterparts.

This class is primarily used by the scheduling mechanism and DataRecorder to dynamically create BasicActions, DataSources, and NumericDataSources.

Version:
$Revision: 1.14 $ $Date: 2004/11/03 19:51:06 $
Author:
Nick Collier

Field Summary
static RSClassLoader loader
           
 
Constructor Summary
ByteCodeBuilder()
           
 
Method Summary
static StatCalculator generateAvgCalculator(java.lang.Class clazz, java.util.List list, java.lang.String methodName)
           
static StatCalculator generateAvgCalculator(java.util.List list, java.lang.String methodName)
           
static BasicAction generateBasicAction(java.lang.Object target, java.lang.String methodName)
          Dynamically creates a BasicAction object whose execute method calls the specified method on the specified object.
static BasicAction generateBasicActionForList(java.util.List list, java.lang.String methodName)
          Dynamically creates a BasicAction object whose execute method will iterate through the specifed list and call the specified method on each object in that list.
static BasicAction generateBasicActionForList(java.util.List list, java.lang.String methodName, java.lang.Class clazz, boolean randomize)
          Dynamically creates a BasicAction object whose execute method will iterate through the specifed list and call the specified method on each object in that list.
static BasicAction generateBasicActionForListRnd(java.util.List list, java.lang.String methodName)
          Dynamically creates a BasicAction object whose execute method will shuffle the specified list with uchicago.src.sim.util.SimUtilites.shuffle and then iterate through the specifed list, calling the specified method on each object in that list.
static BinDataSource generateBinDataSource(java.lang.Object target, java.lang.String methodName)
          Dynamically creates a BinDataSource object whose getBinValue method calls the specified method on the specified object.
static DataSource generateDataSource(java.lang.Object target, java.lang.String methodName)
          Dynamically creates a DataSource object whose execute method calls the specified method on the specified object.
static StatCalculator generateMaxCalculator(java.lang.Class clazz, java.util.List list, java.lang.String methodName)
           
static StatCalculator generateMaxCalculator(java.util.List list, java.lang.String methodName)
           
static ObjectPicker generateMaxObjectPicker(java.lang.Class clazz, java.util.List list, java.lang.String methodName)
           
static ObjectPicker generateMaxObjectPicker(java.util.List list, java.lang.String methodName)
           
static StatCalculator generateMinCalculator(java.lang.Class clazz, java.util.List list, java.lang.String methodName)
           
static StatCalculator generateMinCalculator(java.util.List list, java.lang.String methodName)
           
static ObjectPicker generateMinObjectPicker(java.lang.Class clazz, java.util.List list, java.lang.String methodName)
           
static ObjectPicker generateMinObjectPicker(java.util.List list, java.lang.String methodName)
           
static BinDataSource generateNoTargetBinDataSource(java.lang.Object param, java.lang.String methodName)
          Dynamically creates a BinDataSource object whose getBinValue method is passed an Object of whatever type param is.
static NumericDataSource generateNumericDataSource(java.lang.Object target, java.lang.String methodName)
          Dynamically creates a NumericDataSource object whose execute method calls the specified method on the specified object.
static Sequence generateSequence(java.lang.Object target, java.lang.String methodName)
          Dynamically creates a Sequence object whose execute method calls the specified method on the specified object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

loader

public static RSClassLoader loader
Constructor Detail

ByteCodeBuilder

public ByteCodeBuilder()
Method Detail

generateMinCalculator

public static StatCalculator generateMinCalculator(java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMinCalculator

public static StatCalculator generateMinCalculator(java.lang.Class clazz,
                                                   java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMaxCalculator

public static StatCalculator generateMaxCalculator(java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMaxCalculator

public static StatCalculator generateMaxCalculator(java.lang.Class clazz,
                                                   java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateAvgCalculator

public static StatCalculator generateAvgCalculator(java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateAvgCalculator

public static StatCalculator generateAvgCalculator(java.lang.Class clazz,
                                                   java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMinObjectPicker

public static ObjectPicker generateMinObjectPicker(java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMinObjectPicker

public static ObjectPicker generateMinObjectPicker(java.lang.Class clazz,
                                                   java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMaxObjectPicker

public static ObjectPicker generateMaxObjectPicker(java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateMaxObjectPicker

public static ObjectPicker generateMaxObjectPicker(java.lang.Class clazz,
                                                   java.util.List list,
                                                   java.lang.String methodName)
                                            throws GeneratorException
Throws:
GeneratorException

generateBasicActionForList

public static BasicAction generateBasicActionForList(java.util.List list,
                                                     java.lang.String methodName)
                                              throws GeneratorException
Dynamically creates a BasicAction object whose execute method will iterate through the specifed list and call the specified method on each object in that list. The BasicAction is generated by creating bytecode for a class that extends BasicAction, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name extends BasicAction {

  private List target;

  public a_synthetic_name(List target) {
    this.target = target;
  }

  public void execute() {
    int size = target.size();
    for (int i = 0; i < size; i++) {
      ObjectType x = (ObjectType)target.get(i);
      x.SomeMethod();
    }
  }
 }
 
where ObjectType is the class of the object returned by list.get(0). , and SomeMethod is the method whose name is specified in the parameters below.

Note that the size of the list is computed once. If the specified method call alters the size of the list, the effect of this BasicAction is undefined, but not good.

Parameters:
list - the list containing the objects on which to call the specified method
methodName - the name of the method to call on the objects in the specified list
Returns:
a synthesized BasicAction instance.
Throws:
GeneratorException

generateBasicActionForListRnd

public static BasicAction generateBasicActionForListRnd(java.util.List list,
                                                        java.lang.String methodName)
                                                 throws GeneratorException
Dynamically creates a BasicAction object whose execute method will shuffle the specified list with uchicago.src.sim.util.SimUtilites.shuffle and then iterate through the specifed list, calling the specified method on each object in that list. The BasicAction is generated by creating bytecode for a class that extends BasicAction, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name extends BasicAction {

  private List target;

  public a_synthetic_name(List target) {
    this.target = target;
  }

  public void execute() {
    int size = target.size();
    SimUtilities.shuffle(target);
    for (int i = 0; i < size; i++) {
      ObjectType x = (ObjectType)target.get(i);
      x.SomeMethod();
    }
  }
 }
 
where ObjectType is the class of the object returned by list.get(0). , and SomeMethod is the method whose name is specified in the parameters below.

Note that the size of the list is computed once. If the specified method call alters the size of the list, the effect of this BasicAction is undefined, but not good.

Parameters:
list - the list containing the objects on which to call the specified method
methodName - the name of the method to call on the objects in the specified list
Returns:
a synthesized BasicAction instance.
Throws:
GeneratorException

generateBasicActionForList

public static BasicAction generateBasicActionForList(java.util.List list,
                                                     java.lang.String methodName,
                                                     java.lang.Class clazz,
                                                     boolean randomize)
                                              throws GeneratorException
Dynamically creates a BasicAction object whose execute method will iterate through the specifed list and call the specified method on each object in that list. The BasicAction is generated by creating bytecode for a class that extends BasicAction, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name extends BasicAction {

  private List target;

  public a_synthetic_name(List target) {
    this.target = target;
  }

  public void execute() {
    int size = target.size();
    for (int i = 0; i < size; i++) {
      ObjectType x = (ObjectType)target.get(i);
      x.SomeMethod();
    }
  }
 }
 
where ObjectType is the specified class, and SomeMethod is the method whose name is specified in the parameters below. Specifying the class allows for the generation of bytecode that invokes a super-class or interface method on the objects in the specified list.

Note that the size of the list is computed once. If the specified method call alters the size of the list, the effect of this BasicAction is undefined, but not good.

Parameters:
list - the list containing the objects on which to call the specified method
methodName - the name of the method to call on the objects in the specified list
clazz - the common type of the objects in the specified list
randomize - whether the list should be randomized (shuffled) before iterating through it. Shuffling is done via SimUtilities.shuffle(List)
Returns:
a synthesized BasicAction instance.
Throws:
GeneratorException

generateBasicAction

public static BasicAction generateBasicAction(java.lang.Object target,
                                              java.lang.String methodName)
                                       throws GeneratorException
Dynamically creates a BasicAction object whose execute method calls the specified method on the specified object. The BasicAction is generated by creating bytecode for a Class that extends BasicAction, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name extends BasicAction {

  private ObjectType target

  public a_synthetic_name(ObjectType target) {
    this.target = target;
  }

  public void execute() {
    target.someMethod();
  }
 }
 
where ObjectType is the class of the object specified in the parameters below, and SomeMethod is the method whose name is specified in the parameters below.

Parameters:
target - the object on which to call the method
methodName - the name of the method to call on the specified object
Returns:
a synthesized BasicAction instance.
Throws:
GeneratorException

generateDataSource

public static DataSource generateDataSource(java.lang.Object target,
                                            java.lang.String methodName)
                                     throws GeneratorException
Dynamically creates a DataSource object whose execute method calls the specified method on the specified object. The result of this method call is then returned. The DataSource is generated by creating bytecode for a Class that implements DataSource, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name implements DataSource {

  private ObjectType target

  public a_synthetic_name(ObjectType target) {
    this.target = target;
  }

  public Object execute() {
    return target.someMethod();
  }
 }
 
where ObjectType is the class of the object specified in the parameters below, and SomeMethod is the method whose name is specified in the parameters below.

Parameters:
target - the object on which to call the method
methodName - the name of the method to call on the specified object. The method must return an object
Returns:
a synthesized DataSource instance.
Throws:
GeneratorException

generateNumericDataSource

public static NumericDataSource generateNumericDataSource(java.lang.Object target,
                                                          java.lang.String methodName)
                                                   throws GeneratorException
Dynamically creates a NumericDataSource object whose execute method calls the specified method on the specified object. The result of this method call is then returned. The DataSource is generated by creating bytecode for a Class that implements NumericDataSource, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name implements NumericDataSource {

  private ObjectType target

  public a_synthetic_name(ObjectType target) {
    this.target = target;
  }

  public double execute() {
    return target.someMethod();
  }
 }
 
where ObjectType is the class of the object specified in the parameters below, and SomeMethod is the method whose name is specified in the parameters below.

Parameters:
target - the object on which to call the method
methodName - the name of the method to call on the specified object. The method must return a double
Returns:
a synthesized DataSource instance.
Throws:
GeneratorException

generateNoTargetBinDataSource

public static BinDataSource generateNoTargetBinDataSource(java.lang.Object param,
                                                          java.lang.String methodName)
                                                   throws GeneratorException
Dynamically creates a BinDataSource object whose getBinValue method is passed an Object of whatever type param is. This object is cast to the appropriate type and the specified method is called. method call is then returned. The BinDataSource is generated by creating bytecode for a Class that implements BinDataSource, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name implements BinDataSource {


  public a_synthetic_name() {}

  public double getBinValue(Object obj) {
    return ((param_type)o).someMethod();
  }
 }
 
where SomeMethod is the method whose name is specified in the parameters below. This may also add a cast to the result of target.someMethod if that is a primitive non-double numeric value

Parameters:
param - a prototypical on which to call the method
methodName - the name of the method to call on the specified object. The method must return a numeric value
Returns:
a synthesized BinDataSource instance.
Throws:
GeneratorException

generateBinDataSource

public static BinDataSource generateBinDataSource(java.lang.Object target,
                                                  java.lang.String methodName)
                                           throws GeneratorException
Dynamically creates a BinDataSource object whose getBinValue method calls the specified method on the specified object. The result of this method call is then returned. The BinDataSource is generated by creating bytecode for a Class that implements BinDataSource, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name implements BinDataSource {

  private ObjectType target

  public a_synthetic_name(ObjectType target) {
    this.target = target;
  }

  public double getBinValue(Object obj) {
    return target.someMethod(obj);
  }
 }
 
where ObjectType is the class of the target specified in the parameters below, and SomeMethod is the method whose name is specified in the parameters below. This may also add a cast to the result of target.someMethod if that is a primitive non-double numeric value

Parameters:
target - the object on which to call the method
methodName - the name of the method to call on the specified object. The method must return a numeric value
Returns:
a synthesized BinDataSource instance.
Throws:
GeneratorException

generateSequence

public static Sequence generateSequence(java.lang.Object target,
                                        java.lang.String methodName)
                                 throws GeneratorException
Dynamically creates a Sequence object whose execute method calls the specified method on the specified object. The result of this method call is then returned. The Sequence is generated by creating bytecode for a Class that implements Sequence, dynamically loading this class, and then instantiating an object of this class. The class looks like the following:
 public class a_synthetic_name implements Sequence {

  private ObjectType target

  public a_synthetic_name(ObjectType target) {
    this.target = target;
  }

  public double getSValue() {
    return target.someMethod();
  }
 }
 
where ObjectType is the class of the object specified in the parameters below, and SomeMethod is the method whose name is specified in the parameters below. This may also add a cast to the result of target.someMethod if that is a primitive non-double numeric value

Parameters:
target - the object on which to call the method
methodName - the name of the method to call on the specified object. The method must return a numeric value
Returns:
a synthesized Sequence instance.
Throws:
GeneratorException