小编Key*_*Key的帖子

Byte Buddy 可以在运行时创建字段和方法注释吗?

我想实现这个 3rd-party 注释以将我的类的字段/属性映射到我的数据库表列。我可以在编译时轻松实现注释(如下面的示例代码所示),但我找不到在运行时执行此操作的方法。(我在运行时使用反射加载库。)

我的问题是如何在运行时加载库时实现相同的映射注释?Byte Buddy 可以为 Android 处理这个吗?

//3rd party annotation code
package weborb.service;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MapToProperty {
    String property();
}
Run Code Online (Sandbox Code Playgroud)

///////////////////////////////////////////////// ///

//Here is the implementation using non-reflection
import weborb.service;
    Class Person
    {
        @MapToProperty(property="Person_Name")
        String name;

        @MapToProperty(property="Person_Age")
        int age;

        @MaptoProperty(property="Person_Name")
        public String getName()
        {
            return this.name;
        }

        @MaptoProperty(property="Person_Name")
        public void setName(String name)
        {
            this.name = name;
        }

        @MaptoProperty(property="Person_Age")
        public int getAge()
        {
             return this.age;
        }

        @MaptoProperty(property="Person_Age")
        public void …
Run Code Online (Sandbox Code Playgroud)

java android annotations runtime byte-buddy

1
推荐指数
1
解决办法
2515
查看次数

标签 统计

android ×1

annotations ×1

byte-buddy ×1

java ×1

runtime ×1