1 54 package org.logicalcobwebs.cglib.reflect; 55 56 import org.logicalcobwebs.cglib.core.*; 57 import java.lang.reflect.Constructor ; 58 import java.lang.reflect.InvocationTargetException ; 59 import java.lang.reflect.Method ; 60 import org.logicalcobwebs.asm.ClassVisitor; 61 import org.logicalcobwebs.asm.Type; 62 63 abstract public class FastClass 64 { 65 private Class type; 66 67 protected FastClass(Class type) { 68 this.type = type; 69 } 70 71 public static FastClass create(Class type) { 72 Generator gen = new Generator(); 73 gen.setType(type); 74 return gen.create(); 75 } 76 77 public static class Generator extends AbstractClassGenerator 78 { 79 private static final Source SOURCE = new Source(FastClass.class.getName()); 80 private Class type; 81 82 public Generator() { 83 super(SOURCE); 84 } 85 86 public void setType(Class type) { 87 this.type = type; 88 } 89 90 public FastClass create() { 91 setNamePrefix(type.getName()); 92 return (FastClass)super.create(type.getName()); 93 } 94 95 protected ClassLoader getDefaultClassLoader() { 96 return type.getClassLoader(); 97 } 98 99 public void generateClass(ClassVisitor v) throws Exception { 100 new FastClassEmitter(v, getClassName(), type); 101 } 102 103 protected Object firstInstance(Class type) { 104 return ReflectUtils.newInstance(type, 105 new Class []{ Class .class }, 106 new Object []{ this.type }); 107 } 108 109 protected Object nextInstance(Object instance) { 110 return instance; 111 } 112 } 113 114 public Object invoke(String name, Class [] parameterTypes, Object obj, Object [] args) throws InvocationTargetException { 115 return invoke(getIndex(name, parameterTypes), obj, args); 116 } 117 118 public Object newInstance() throws InvocationTargetException { 119 return newInstance(getIndex(Constants.EMPTY_CLASS_ARRAY), null); 120 } 121 122 public Object newInstance(Class [] parameterTypes, Object [] args) throws InvocationTargetException { 123 return newInstance(getIndex(parameterTypes), args); 124 } 125 126 public FastMethod getMethod(Method method) { 127 return new FastMethod(this, method); 128 } 129 130 public FastConstructor getConstructor(Constructor constructor) { 131 return new FastConstructor(this, constructor); 132 } 133 134 public FastMethod getMethod(String name, Class [] parameterTypes) { 135 try { 136 return getMethod(type.getMethod(name, parameterTypes)); 137 } catch (NoSuchMethodException e) { 138 throw new NoSuchMethodError (e.getMessage()); 139 } 140 } 141 142 public FastConstructor getConstructor(Class [] parameterTypes) { 143 try { 144 return getConstructor(type.getConstructor(parameterTypes)); 145 } catch (NoSuchMethodException e) { 146 throw new NoSuchMethodError (e.getMessage()); 147 } 148 } 149 150 public String getName() { 151 return type.getName(); 152 } 153 154 public Class getJavaClass() { 155 return type; 156 } 157 158 public String toString() { 159 return type.toString(); 160 } 161 162 public int hashCode() { 163 return type.hashCode(); 164 } 165 166 public boolean equals(Object o) { 167 if (o == null || !(o instanceof FastClass)) { 168 return false; 169 } 170 return type.equals(((FastClass)o).type); 171 } 172 173 183 abstract public int getIndex(String name, Class [] parameterTypes); 184 185 192 abstract public int getIndex(Class [] parameterTypes); 193 194 202 abstract public Object invoke(int index, Object obj, Object [] args) throws InvocationTargetException ; 203 204 211 abstract public Object newInstance(int index, Object [] args) throws InvocationTargetException ; 212 213 abstract public int getIndex(Signature sig); 214 215 218 abstract public int getMaxIndex(); 219 220 protected static String getSignatureWithoutReturnType(String name, Class [] parameterTypes) { 221 StringBuffer sb = new StringBuffer (); 222 sb.append(name); 223 sb.append('('); 224 for (int i = 0; i < parameterTypes.length; i++) { 225 sb.append(Type.getDescriptor(parameterTypes[i])); 226 } 227 sb.append(')'); 228 return sb.toString(); 229 } 230 } 231 | Popular Tags |