Class Instrumenter
- java.lang.Object
-
- com.tonic.analysis.instrumentation.Instrumenter
-
public class Instrumenter extends Object
Fluent entry point for registering instrumentation hooks and applying them to a set of classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classInstrumenter.ArrayLoadHookBuilderFluent builder for an array load hook.static classInstrumenter.ArrayStoreHookBuilderFluent builder for an array store hook.static classInstrumenter.ExceptionHookBuilderFluent builder for an exception handler hook.static classInstrumenter.FieldReadHookBuilderFluent builder for a field read hook.static classInstrumenter.FieldWriteHookBuilderFluent builder for a field write hook.static classInstrumenter.InstrumentationReportImmutable counts from one instrumentation run.static classInstrumenter.MethodCallHookBuilderFluent builder for a method call hook.static classInstrumenter.MethodEntryHookBuilderFluent builder for a method entry hook.static classInstrumenter.MethodExitHookBuilderFluent builder for a method exit hook.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intapply()Applies all registered hooks to the target classes.Instrumenter.InstrumentationReportapplyWithReport()Applies all registered hooks and records the result as the last report.InstrumenterfailOnError(boolean fail)Sets whether a failure to instrument a method is rethrown instead of counted.static InstrumenterforClass(ClassFile classFile)Creates an instrumenter targeting one class.static InstrumenterforClasses(ClassFile... classes)Creates an instrumenter targeting the given classes.static InstrumenterforClasses(List<ClassFile> classes)Creates an instrumenter targeting a list of classes.static InstrumenterforClassPool(ClassPool pool)Creates an instrumenter targeting every class in a pool.Instrumenter.InstrumentationReportgetLastReport()Instrumenter.ArrayLoadHookBuilderonArrayLoad()Instrumenter.ArrayStoreHookBuilderonArrayStore()Instrumenter.ExceptionHookBuilderonException()Instrumenter.FieldReadHookBuilderonFieldRead()Instrumenter.FieldWriteHookBuilderonFieldWrite()Instrumenter.MethodCallHookBuilderonMethodCall()Instrumenter.MethodEntryHookBuilderonMethodEntry()Instrumenter.MethodExitHookBuilderonMethodExit()InstrumenterskipAbstract(boolean skip)Sets whether abstract methods are left alone.InstrumenterskipBridge(boolean skip)Sets whether bridge methods are left alone.InstrumenterskipConstructors(boolean skip)Sets whether constructors are left alone.InstrumenterskipNative(boolean skip)Sets whether native methods are left alone.InstrumenterskipStaticInitializers(boolean skip)Sets whether static initializers are left alone.InstrumenterskipSynthetic(boolean skip)Sets whether synthetic methods are left alone.Instrumenterverbose(boolean verbose)Sets whether instrumentation failures are printed to stderr.
-
-
-
Method Detail
-
getLastReport
public Instrumenter.InstrumentationReport getLastReport()
- Returns:
- the last report
-
forClassPool
public static Instrumenter forClassPool(ClassPool pool)
Creates an instrumenter targeting every class in a pool.- Parameters:
pool- the class pool- Returns:
- a new instrumenter
-
forClass
public static Instrumenter forClass(ClassFile classFile)
Creates an instrumenter targeting one class.- Parameters:
classFile- the class file to instrument- Returns:
- a new instrumenter
-
forClasses
public static Instrumenter forClasses(List<ClassFile> classes)
Creates an instrumenter targeting a list of classes.- Parameters:
classes- the class files to instrument- Returns:
- a new instrumenter
-
forClasses
public static Instrumenter forClasses(ClassFile... classes)
Creates an instrumenter targeting the given classes.- Parameters:
classes- the class files to instrument- Returns:
- a new instrumenter
-
onMethodEntry
public Instrumenter.MethodEntryHookBuilder onMethodEntry()
- Returns:
- a builder for a hook that runs on entry to matching methods
-
onMethodExit
public Instrumenter.MethodExitHookBuilder onMethodExit()
- Returns:
- a builder for a hook that runs at every return of matching methods
-
onFieldWrite
public Instrumenter.FieldWriteHookBuilder onFieldWrite()
- Returns:
- a builder for a hook that runs at matching field writes
-
onFieldRead
public Instrumenter.FieldReadHookBuilder onFieldRead()
- Returns:
- a builder for a hook that runs at matching field reads
-
onArrayStore
public Instrumenter.ArrayStoreHookBuilder onArrayStore()
- Returns:
- a builder for a hook that runs at matching array stores
-
onArrayLoad
public Instrumenter.ArrayLoadHookBuilder onArrayLoad()
- Returns:
- a builder for a hook that runs at matching array loads
-
onMethodCall
public Instrumenter.MethodCallHookBuilder onMethodCall()
- Returns:
- a builder for a hook that runs around matching call sites
-
onException
public Instrumenter.ExceptionHookBuilder onException()
- Returns:
- a builder for a hook that runs in matching exception handlers
-
skipAbstract
public Instrumenter skipAbstract(boolean skip)
Sets whether abstract methods are left alone.- Parameters:
skip- true to skip them- Returns:
- this instrumenter
-
skipNative
public Instrumenter skipNative(boolean skip)
Sets whether native methods are left alone.- Parameters:
skip- true to skip them- Returns:
- this instrumenter
-
skipConstructors
public Instrumenter skipConstructors(boolean skip)
Sets whether constructors are left alone.- Parameters:
skip- true to skip them- Returns:
- this instrumenter
-
skipStaticInitializers
public Instrumenter skipStaticInitializers(boolean skip)
Sets whether static initializers are left alone.- Parameters:
skip- true to skip them- Returns:
- this instrumenter
-
skipSynthetic
public Instrumenter skipSynthetic(boolean skip)
Sets whether synthetic methods are left alone.- Parameters:
skip- true to skip them- Returns:
- this instrumenter
-
skipBridge
public Instrumenter skipBridge(boolean skip)
Sets whether bridge methods are left alone.- Parameters:
skip- true to skip them- Returns:
- this instrumenter
-
failOnError
public Instrumenter failOnError(boolean fail)
Sets whether a failure to instrument a method is rethrown instead of counted.- Parameters:
fail- true to rethrow- Returns:
- this instrumenter
-
verbose
public Instrumenter verbose(boolean verbose)
Sets whether instrumentation failures are printed to stderr.- Parameters:
verbose- true to print- Returns:
- this instrumenter
-
apply
public int apply()
Applies all registered hooks to the target classes.- Returns:
- the total number of instrumentation points applied
- Throws:
RuntimeException- if a method fails to instrument and failOnError is set
-
applyWithReport
public Instrumenter.InstrumentationReport applyWithReport()
Applies all registered hooks and records the result as the last report.- Returns:
- counts of points, classes, methods, and failures
- Throws:
RuntimeException- if a method fails to instrument and failOnError is set
-
-