V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
esolve
V2EX  ›  问与答

注解是如何保证注值的,原理是啥?

  •  
  •   esolve · 2017-04-25 13:01:27 +08:00 · 1562 次点击
    这是一个创建于 2557 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我试了一下这篇文章里的注解 http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html 发现并没有能注值

    7 条回复    2017-04-25 16:30:30 +08:00
    esolve
        1
    esolve  
    OP
       2017-04-25 13:02:24 +08:00
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitName {
    String value() default "";
    }



    public class Apple {

    @FruitName("Apple")
    private String appleName;



    public void setAppleName(String appleName) {
    this.appleName = appleName;
    }
    public String getAppleName() {
    return appleName;
    }

    public void displayName(){
    System.out.println("水果的名字是:"+getAppleName());
    }
    }

    这种没有能够注值啊
    Cbdy
        2
    Cbdy  
       2017-04-25 13:37:09 +08:00
    注解是 java 的一种元数据,本身不提供功能
    esolve
        3
    esolve  
    OP
       2017-04-25 14:08:41 +08:00
    @Cbdy 那注值是如何实现的?
    LaudOak
        4
    LaudOak  
       2017-04-25 14:12:14 +08:00 via Android
    要配合反射注值吧
    esolve
        5
    esolve  
    OP
       2017-04-25 14:24:25 +08:00
    @LaudOak 有没有原理分析的文章啊
    大致是如何通过反射使得注解能够注值的。。
    LaudOak
        6
    LaudOak  
       2017-04-25 15:22:25 +08:00   ❤️ 1
    ```
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.reflect.Field;

    public class Reflect {

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface FruitName {
    String value() default "";
    }

    public static class Apple {
    @FruitName("apple")
    public String name;
    }

    public static void main(String... args) throws Exception {
    Apple apple = new Apple();
    Class clz = apple.getClass();
    Field[] fields = clz.getDeclaredFields();
    for (Field field : fields) {
    if (field.isAnnotationPresent(FruitName.class)) {
    field.set(apple, field.getAnnotation(FruitName.class).value());
    }
    }
    System.out.println(apple.name);
    }

    }

    ```

    http://tutorials.jenkov.com/java-reflection/annotations.html
    https://keyholesoftware.com/2014/09/15/java-annotations-using-reflection/
    http://www.journaldev.com/1789/java-reflection-example-tutorial
    https://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html
    aristotll
        7
    aristotll  
       2017-04-25 16:30:30 +08:00
    RetentionPolicy.RUNTIME 的通过反射来取
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3890 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:25 · PVG 18:25 · LAX 03:25 · JFK 06:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.