注释中不支持动态值作为属性 - AspectJ Android [AOP Android]

Dynamic values are not supported as attributes in annotation- AspectJ Android [AOP Android]

我正在使用自定义注释来记录用户点击的 ID。但我收到错误消息“属性值必须是常量”。我的代码片段如下。

mAssetId= Asset.getContentId();

  @TrackEvent("track_event")
    @ArrayParams({@Params(key = "content_id",value = mAssetId)})
    protected void attributeMethod() {
    }

提前致谢。有什么方法可以在注释中传递动态值而不是静态变量或常量。我正在为 android.

使用 AspectJ

无法在注释属性中指定运行时值,只能指定常量,甚至可能的类型集也仅限于几种类型:

primitives, String, Class, enums, annotations, and arrays of the preceding types

根据 Java 5 Language Guide - Annotations.

是的,没错,我找到了使用 annotation.Also 记录 运行 时间值的解决方案,我对 anootation 进行了轻微更改以支持此

 @TrackEvent("track_event")
    protected void attributeMethod(@Params("id") String value) {
    }

在我的情况下它成功了,希望任何人都可以帮助这个。

在 aspectj 本机语法中,您可以捕获所需的任何运行时变量:

pointcut rtVar(int id): set(* int SomeClass.mAssetId) && args(id) && within(SomeClass);

然后将这个 id 变量处理到 joinpoint 中。