【问题标题】:Java - Use annotations and intercept methods?Java - 使用注释和拦截方法?
【发布时间】:2012-01-12 18:30:49
【问题描述】:

有没有简单的方法拦截java中的方法。我需要为所需的方法添加注释,以便在通过方法之前调用一段逻辑。

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
}

编辑:

Android 应用推荐的 guice 库不包含 AOP。 是否可以在不添加任何库的情况下使用反射来实现这一点?

【问题讨论】:

标签: java android annotations interception


【解决方案1】:

Guice 提供了实现注释的简单方法。看看这个。

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

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

【讨论】:

  • 谢谢,我决定使用 Guice,因为我没有直接在我的 android 应用上使用它,所以应该没有任何性能问题。
【解决方案2】:

正如 sid malani 所说,Google Guice 非常适合这个。一般来说,你想阅读aspect oriented programming tutorials ...有一个很好的工具叫做JMangler,它也可能有用

【讨论】:

    【解决方案3】:

    如果您通过dynamic proxies. 对接口进行编码,则可以使用反射

    【讨论】:

      【解决方案4】:

      我怀疑没有任何 3rd 方库可以很好地完成它。

      有一个叫做 cglib 的库,它可以做这些事情。

      基本上它会在运行时创建拦截类的子类。您将能够通过实现 InvocationHandler 来“覆盖”方法,当调用任何超类方法时,它将充当代理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多