1 15 16 package javassist; 17 18 import java.io.DataOutputStream ; 19 import java.io.IOException ; 20 import javassist.bytecode.ClassFile; 21 22 class CtNewClass extends CtClassType { 23 25 protected boolean hasConstructor; 26 27 CtNewClass(String name, ClassPool cp, 28 boolean isInterface, CtClass superclass) { 29 super(name, cp); 30 wasChanged = true; 31 eraseCache(); 32 String superName; 33 if (superclass == null) 34 superName = null; 35 else 36 superName = superclass.getName(); 37 38 classfile = new ClassFile(isInterface, name, superName); 39 40 setModifiers(Modifier.setPublic(getModifiers())); 41 hasConstructor = isInterface; 42 } 43 44 protected void extendToString(StringBuffer buffer) { 45 if (hasConstructor) 46 buffer.append("hasConstructor "); 47 48 super.extendToString(buffer); 49 } 50 51 public void addConstructor(CtConstructor c) 52 throws CannotCompileException 53 { 54 hasConstructor = true; 55 super.addConstructor(c); 56 } 57 58 public void toBytecode(DataOutputStream out) 59 throws CannotCompileException, IOException 60 { 61 if (!hasConstructor) 62 try { 63 inheritAllConstructors(); 64 hasConstructor = true; 65 } 66 catch (NotFoundException e) { 67 throw new CannotCompileException(e); 68 } 69 70 super.toBytecode(out); 71 } 72 73 80 public void inheritAllConstructors() 81 throws CannotCompileException, NotFoundException 82 { 83 CtClass superclazz; 84 CtConstructor[] cs; 85 86 superclazz = getSuperclass(); 87 cs = superclazz.getDeclaredConstructors(); 88 89 int n = 0; 90 for (int i = 0; i < cs.length; ++i) { 91 CtConstructor c = cs[i]; 92 if (Modifier.isPublic(c.getModifiers())) { 93 CtConstructor cons 94 = CtNewConstructor.make(c.getParameterTypes(), 95 c.getExceptionTypes(), this); 96 addConstructor(cons); 97 ++n; 98 } 99 } 100 101 if (n < 1) 102 throw new CannotCompileException( 103 "no public constructor in " + superclazz.getName()); 104 105 } 106 } 107 | Popular Tags |