An overview of annotations and annotation processors, focused on Android. Presented at the Philly Android Alliance and Philly Cocoaheads joint meetup as part of Philly Tech Week 2017.
@auditty +AudreyTroutt #import // This is method swizzling + (void)replaceOriginalInstanceMethod:(SEL)originalMethod from:(id)original withInstanceMethod:(SEL)replacementMethod from:(id)replacementObject { Method originalMethod = class_getInstanceMethod([original class], originalMethod); Method mockMethod = class_getInstanceMethod([replacementObject class], replacementMethod); method_exchangeImplementations(originalMethod, mockMethod); } // Don’t do this in production code! It’s more polite to always call the original code. Metaprogramming is easy* on iOS
@auditty +AudreyTroutt 1. Annotation(s) ○ are interfaces 2. Processor(s) ○ extend javax.annotation.processing.AbstractProces sor 3. Additional API to leverage generated code Annotation Processor Project Parts
@auditty +AudreyTroutt WatchedMethodProcessor.jar Two files: ● com/audreytroutt/WatchedMethodProcessor.class ● META-INF/services/javax.annotation.processing.Proces sor
@auditty +AudreyTroutt Some good blog posts: ● http://hannesdorfmann.com/annotation-proc essing/annotationprocessing101 ● https://medium.com/@lgvalle/how-butterknif e-actually-works-85be0afbc5ab