Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/bytecode/javassist/FastClass.java
package org.hibernate.bytecode.javassist;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.io.Serializable;

/**
 @author Muga Nishizawa
 */
public class FastClass implements Serializable {

  private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];

  private Class type;

  private FastClass() {
  }

  private FastClass(Class type) {
    this.type = type;
  }

  public Object invoke(
      String name,
          Class[] parameterTypes,
          Object obj,
          Object[] argsthrows InvocationTargetException {
    return this.invokethis.getIndexname, parameterTypes ), obj, args );
  }

  public Object invoke(
      int index,
          Object obj,
          Object[] argsthrows InvocationTargetException {
    Method[] methods = this.type.getMethods();
    try {
      return methods[index].invokeobj, args );
    }
    catch ArrayIndexOutOfBoundsException e ) {
      throw new IllegalArgumentException(
          "Cannot find matching method/constructor"
      );
    }
    catch IllegalAccessException e ) {
      throw new InvocationTargetException);
    }
  }

  public Object newInstance() throws InvocationTargetException {
    return this.newInstancethis.getIndexEMPTY_CLASS_ARRAY )null );
  }

  public Object newInstance(
      Class[] parameterTypes,
          Object[] argsthrows InvocationTargetException {
    return this.newInstancethis.getIndexparameterTypes ), args );
  }

  public Object newInstance(
      int index,
          Object[] argsthrows InvocationTargetException {
    Constructor[] conss = this.type.getConstructors();
    try {
      return conss[index].newInstanceargs );
    }
    catch ArrayIndexOutOfBoundsException e ) {
      throw new IllegalArgumentException"Cannot find matching method/constructor" );
    }
    catch InstantiationException e ) {
      throw new InvocationTargetException);
    }
    catch IllegalAccessException e ) {
      throw new InvocationTargetException);
    }
  }

  public int getIndex(String name, Class[] parameterTypes) {
    Method[] methods = this.type.getMethods();
    boolean eq = true;
    for int i = 0; i < methods.length; ++i ) {
      if !Modifier.isPublicmethods[i].getModifiers() ) ) {
        continue;
      }
      if !methods[i].getName().equalsname ) ) {
        continue;
      }
      Class[] params = methods[i].getParameterTypes();
      if params.length != parameterTypes.length ) {
        continue;
      }
      eq = true;
      for int j = 0; j < params.length; ++j ) {
        if !params[j].equalsparameterTypes[j] ) ) {
          eq = false;
          break;
        }
      }
      if eq ) {
        return i;
      }
    }
    return -1;
  }

  public int getIndex(Class[] parameterTypes) {
    Constructor[] conss = this.type.getConstructors();
    boolean eq = true;
    for int i = 0; i < conss.length; ++i ) {
      if !Modifier.isPublicconss[i].getModifiers() ) ) {
        continue;
      }
      Class[] params = conss[i].getParameterTypes();
      if params.length != parameterTypes.length ) {
        continue;
      }
      eq = true;
      for int j = 0; j < params.length; ++j ) {
        if !params[j].equalsparameterTypes[j] ) ) {
          eq = false;
          break;
        }
      }
      if eq ) {
        return i;
      }
    }
    return -1;
  }

  public int getMaxIndex() {
    Method[] methods = this.type.getMethods();
    int count = 0;
    for int i = 0; i < methods.length; ++i ) {
      if Modifier.isPublicmethods[i].getModifiers() ) ) {
        count++;
      }
    }
    return count;
  }

  public String getName() {
    return this.type.getName();
  }

  public Class getJavaClass() {
    return this.type;
  }

  public String toString() {
    return this.type.toString();
  }

  public int hashCode() {
    return this.type.hashCode();
  }

  public boolean equals(Object o) {
    if !instanceof FastClass ) ) {
      return false;
    }
    return this.type.equals( ( ( FastClass ).type );
  }

  public static FastClass create(Class type) {
    FastClass fc = new FastClasstype );
    return fc;
  }
}