001: package org.hibernate.bytecode.javassist;
002:
003: import java.lang.reflect.Constructor;
004: import java.lang.reflect.InvocationTargetException;
005: import java.lang.reflect.Method;
006: import java.lang.reflect.Modifier;
007: import java.io.Serializable;
008:
009: /**
010: * @author Muga Nishizawa
011: */
012: public class FastClass implements Serializable {
013:
014: private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
015:
016: private Class type;
017:
018: private FastClass() {
019: }
020:
021: private FastClass(Class type) {
022: this .type = type;
023: }
024:
025: public Object invoke(String name, Class[] parameterTypes,
026: Object obj, Object[] args) throws InvocationTargetException {
027: return this .invoke(this .getIndex(name, parameterTypes), obj,
028: args);
029: }
030:
031: public Object invoke(int index, Object obj, Object[] args)
032: throws InvocationTargetException {
033: Method[] methods = this .type.getMethods();
034: try {
035: return methods[index].invoke(obj, args);
036: } catch (ArrayIndexOutOfBoundsException e) {
037: throw new IllegalArgumentException(
038: "Cannot find matching method/constructor");
039: } catch (IllegalAccessException e) {
040: throw new InvocationTargetException(e);
041: }
042: }
043:
044: public Object newInstance() throws InvocationTargetException {
045: return this .newInstance(this .getIndex(EMPTY_CLASS_ARRAY), null);
046: }
047:
048: public Object newInstance(Class[] parameterTypes, Object[] args)
049: throws InvocationTargetException {
050: return this .newInstance(this .getIndex(parameterTypes), args);
051: }
052:
053: public Object newInstance(int index, Object[] args)
054: throws InvocationTargetException {
055: Constructor[] conss = this .type.getConstructors();
056: try {
057: return conss[index].newInstance(args);
058: } catch (ArrayIndexOutOfBoundsException e) {
059: throw new IllegalArgumentException(
060: "Cannot find matching method/constructor");
061: } catch (InstantiationException e) {
062: throw new InvocationTargetException(e);
063: } catch (IllegalAccessException e) {
064: throw new InvocationTargetException(e);
065: }
066: }
067:
068: public int getIndex(String name, Class[] parameterTypes) {
069: Method[] methods = this .type.getMethods();
070: boolean eq = true;
071: for (int i = 0; i < methods.length; ++i) {
072: if (!Modifier.isPublic(methods[i].getModifiers())) {
073: continue;
074: }
075: if (!methods[i].getName().equals(name)) {
076: continue;
077: }
078: Class[] params = methods[i].getParameterTypes();
079: if (params.length != parameterTypes.length) {
080: continue;
081: }
082: eq = true;
083: for (int j = 0; j < params.length; ++j) {
084: if (!params[j].equals(parameterTypes[j])) {
085: eq = false;
086: break;
087: }
088: }
089: if (eq) {
090: return i;
091: }
092: }
093: return -1;
094: }
095:
096: public int getIndex(Class[] parameterTypes) {
097: Constructor[] conss = this .type.getConstructors();
098: boolean eq = true;
099: for (int i = 0; i < conss.length; ++i) {
100: if (!Modifier.isPublic(conss[i].getModifiers())) {
101: continue;
102: }
103: Class[] params = conss[i].getParameterTypes();
104: if (params.length != parameterTypes.length) {
105: continue;
106: }
107: eq = true;
108: for (int j = 0; j < params.length; ++j) {
109: if (!params[j].equals(parameterTypes[j])) {
110: eq = false;
111: break;
112: }
113: }
114: if (eq) {
115: return i;
116: }
117: }
118: return -1;
119: }
120:
121: public int getMaxIndex() {
122: Method[] methods = this .type.getMethods();
123: int count = 0;
124: for (int i = 0; i < methods.length; ++i) {
125: if (Modifier.isPublic(methods[i].getModifiers())) {
126: count++;
127: }
128: }
129: return count;
130: }
131:
132: public String getName() {
133: return this .type.getName();
134: }
135:
136: public Class getJavaClass() {
137: return this .type;
138: }
139:
140: public String toString() {
141: return this .type.toString();
142: }
143:
144: public int hashCode() {
145: return this .type.hashCode();
146: }
147:
148: public boolean equals(Object o) {
149: if (!(o instanceof FastClass)) {
150: return false;
151: }
152: return this .type.equals(((FastClass) o).type);
153: }
154:
155: public static FastClass create(Class type) {
156: FastClass fc = new FastClass(type);
157: return fc;
158: }
159: }
|