Showing posts with label annotations. Show all posts
Showing posts with label annotations. Show all posts

Tuesday, 29 October 2013

Droidcon 2013: Boundbox & Memento + Annotation Processing

Stéphane Nicolas, Octo Technology

Matthias Käppler, Soundcloud @mttkay

boundbox

Slides available online

https://github.com/stephanenicolas/boundbox

  • break encapsulation to ease testing (with Android in mind)
  • let’s you access things from Android SDK that you wouldn’t normally access
  • also lets you add fields (e.g. for stub Android jars that only have methods available)
  • can indicate how far up the hierarchy you want to expose
  • don’t need to change your code — just adjust your tests
  • access using reflection, but access checked at compile time
  • open source on github
  • compares with WhiteBox from PowerMock
  • intended for helping test UI layer
    • find views by field name
    • invoke methods & inner classes directly
    • faster and more direct than robotium & UIAutomator
    • as fast as espresso

memento

https://github.com/mttkay/memento

  • dealing with keeping state when the screen rotates
  • the activity gets destroyed and recreated and all your state is thrown away…
  • can use onRetainNonConfigurationInstance to pass objects through configuration changes
    • but what if you need to retain more than one object
    • deprecated in place of fragments — these keep their configuration using setRetainInstance
    • but watch out: setRetainInstance doesn’t work for child fragments and is super-prone for data leaks
  • but what if you have multiple fragments sharing the same state?
  • mark a package private field as @Retain
  • override the new onLaunch method (used to indicate first time initialisation of the class i.e. not after a config change)
  • then use Memento.retain(this)

annotation processing in general

  • use JavaWriter from squareup
  • look at how square does it for dagger dependency injection
  • Google has a library so you can assert over java source files
    • lets you compile with a processor and check the output
  • want to split up the annotation processor into two:
    • one for the client-side annotation itself and any required classes
    • one for the processor (don’t want it in your app)

Monday, 26 October 2009

BarCampLondon7: Project Lombok - the end of Java boilerplate?

Reinier Zwitserloot - @surial

Project Lombok is an extension to Java that allows you to write less boilerplate code by using annotations.

  • examples:
    • @Data to provide automatic getters and setters for private fields, equality and hashCode, and a constructor for final fields
    • @Cleanup to tidy up inputstreams when block ends
    • @Synchronized to lock on private Object instance rather than this
    • @SneakyThrows() hides a checked exception from javac, but leaves it for JVM
    • (in JVM all exceptions are unchecked)
  • integrates with Eclipse (e.g. getters/setters provided without you having to type)
  • does mucking around through its jar, so can work easily with Ant & Maven etc
    • use com.sun...ProcessingEnvironment to
  • Java Posse — podcast
    • inventor of annotations happy that lombok exists, but not so happy that private APIs being used…