首页 > 能否使用注解找到所有被此注解注解的类?

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

我现在需要将一个类和一个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)
        }

    }

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


你遇到了一个别人几乎不会遇到的问题,先想想别人为啥没遇到过,再想想自己如何实现,最后想想有没有别的解决办法。


然而没有这种办法,注解也是单向映射的,在类和方法中会记录所使用的注解,但在注解中不会记录被谁使用过。

【热门文章】
【热门文章】