search

Home  >  Q&A  >  body text

java - 能否使用注解找到所有被此注解注解的类?

我现在需要将一个类和一个byte绑定在一起 这样他们就能找到彼此(用两个hashmap)
我现在想到的方式是

class A{
    ....
}
class B{
    ....
}
....
HashMap map1=new HashMap();
HashMap map2=new HashMap();
map1.put(A,1)
map2.put(1,A)
map1.put(B,2)
map2.put(2,B)

但是很明显这样写太丑了
如果能通过注解找到类的话就可以

    @record
    class A{
        ....
    }
    @record
    class B{
        ....
    }
    ....
    class map{
    static {
        list= //a way to get all class which be annotation
        for(int i=0;i<list.length();i++){
            map.put(list.get(i),i)
        }
    }
    static byte get(Object k){
        map.get(k)
    }
    }
    
    //用的时候可以
    class A{
        ....
        {
        map.get(this)
        }

    }

注解可以做到这种效果吗?
对于这个问题还有什么更好的方法吗?

大家讲道理大家讲道理2895 days ago313

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 09:47:18

    However, there is no such method. Annotations are also one-way mapped. The annotations used will be recorded in classes and methods, but who has used them will not be recorded in the annotations.

    reply
    0
  • 迷茫

    迷茫2017-04-18 09:47:18

    If you encounter a problem that few others will encounter, first think about why others have never encountered it, then think about how to implement it yourself, and finally think about whether there are other solutions.

    reply
    0
  • Cancelreply