private void addNewDeferConstructor ( final CtClass clazz ) throws CannotCompileException { final CtClass superClass ; try { superClass = clazz . getSuperclass ( ) ; } catch ( NotFoundException e1 ) { throw new IllegalArgumentException ( "Internal error: Failed to get superclass for " + clazz . getName ( ) + " when about to create a new default constructor." ) ; } ClassPool classPool = clazz . getClassPool ( ) ; /* * To make a unique defer constructor we create a new constructor * with one argument (IndicateReloadClass). So we get this class a * Javassist class below. */ final CtClass constructorType ; try { constructorType = classPool . get ( IndicateReloadClass . class . getName ( ) ) ; } catch ( NotFoundException e ) { throw new IllegalArgumentException ( "Internal error: failed to get the " + IndicateReloadClass . class . getName ( ) + " when added defer constructor." ) ; } clazz . defrost ( ) ; if ( superClass . getName ( ) . equals ( Object . class . getName ( ) ) ) { try { clazz . addConstructor ( CtNewConstructor . make ( new CtClass [ ] { constructorType } , new CtClass [ 0 ] , "{super();}" , clazz ) ) ; } catch ( DuplicateMemberException e ) { // OK, the constructor has already been added. } } else { addNewDeferConstructor ( superClass ) ; try { clazz . addConstructor ( CtNewConstructor . make ( new CtClass [ ] { constructorType } , new CtClass [ 0 ] , "{super($$);}" , clazz ) ) ; } catch ( DuplicateMemberException e ) { // OK, the constructor has already been added. } } }
Create a defer constructor in the class which will be called when the constructor is suppressed .