Question

Is there a simple way to intercept methods in java. I need to add an annotation to required methods so that a piece of logic gets called before going through the method.

public void verifyActivity() {
   // Asset if you are on a wrong page
}

@VerifyActivity
public void testLogin() {
   // Login for my automate test
}

@VerifyActivity
public void testSomethingElse() {
   // Test some other UI Automation stuff
}

EDIT:

The recommended guice library for android apps does not contain AOP. Is it possible to achieve this using reflection without adding any libraries?

Was it helpful?

Solution

Guice provides easy way of implementing annotations. Check this out.

http://code.google.com/p/google-guice/wiki/AOP

http://code.google.com/p/google-guice/

OTHER TIPS

As sid malani said Google Guice is great for this. In general you want to read up on aspect oriented programming tutorials ... There is a nice tool called JMangler that may be of use as well

You can use reflection if you've coded to interfaces through dynamic proxies.

I doubt that it can be nicely done without any 3rd party libs.

There is a library called cglib, which is capable of such things.

Basically it will create a subclass of the intercepted class at runtime. You'll be able to "override" methods by implementing an InvocationHandler, which will act as a proxy when any of the superclass methods being called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top