1 16 package net.sf.cglib.proxy; 17 18 import java.lang.reflect.*; 19 import java.util.*; 20 import net.sf.cglib.core.*; 21 import org.objectweb.asm.ClassVisitor; 22 import org.objectweb.asm.Type; 23 24 32 public class InterfaceMaker extends AbstractClassGenerator 33 { 34 private static final Source SOURCE = new Source(InterfaceMaker.class.getName()); 35 private Map signatures = new HashMap(); 36 37 42 public InterfaceMaker() { 43 super(SOURCE); 44 } 45 46 51 public void add(Signature sig, Type[] exceptions) { 52 signatures.put(sig, exceptions); 53 } 54 55 60 public void add(Method method) { 61 add(ReflectUtils.getSignature(method), 62 ReflectUtils.getExceptionTypes(method)); 63 } 64 65 71 public void add(Class clazz) { 72 Method[] methods = clazz.getMethods(); 73 for (int i = 0; i < methods.length; i++) { 74 Method m = methods[i]; 75 if (!m.getDeclaringClass().getName().equals("java.lang.Object")) { 76 add(m); 77 } 78 } 79 } 80 81 84 public Class create() { 85 setUseCache(false); 86 return (Class )super.create(this); 87 } 88 89 protected ClassLoader getDefaultClassLoader() { 90 return null; 91 } 92 93 protected Object firstInstance(Class type) { 94 return type; 95 } 96 97 protected Object nextInstance(Object instance) { 98 throw new IllegalStateException ("InterfaceMaker does not cache"); 99 } 100 101 public void generateClass(ClassVisitor v) throws Exception { 102 ClassEmitter ce = new ClassEmitter(v); 103 ce.begin_class(Constants.V1_2, 104 Constants.ACC_PUBLIC | Constants.ACC_INTERFACE, 105 getClassName(), 106 null, 107 null, 108 Constants.SOURCE_FILE); 109 for (Iterator it = signatures.keySet().iterator(); it.hasNext();) { 110 Signature sig = (Signature)it.next(); 111 Type[] exceptions = (Type[])signatures.get(sig); 112 ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_ABSTRACT, 113 sig, 114 exceptions).end_method(); 115 } 116 ce.end_class(); 117 } 118 } 119 | Popular Tags |