public static byte [ ] instrument ( byte [ ] originalBytes , Class < ? > classBeingRedefined ) { try { ClassReader cr = new ClassReader ( originalBytes ) ; ClassWriter cw = new ClassWriter ( cr , ClassWriter . COMPUTE_MAXS ) ; VerifyingClassAdapter vcw = new VerifyingClassAdapter ( cw , originalBytes , cr . getClassName ( ) ) ; ClassVisitor adapter = new ConstructorClassAdapter ( vcw , classBeingRedefined ) ; cr . accept ( adapter , ClassReader . SKIP_FRAMES ) ; return vcw . toByteArray ( ) ; } catch ( RuntimeException e ) { logger . log ( Level . WARNING , "Failed to instrument class." , e ) ; throw e ; } catch ( Error e ) { logger . log ( Level . WARNING , "Failed to instrument class." , e ) ; throw e ; } }
Given the bytes representing a class add invocations of the ConstructorCallback method to the constructor .