java - 能否使用注解找到所有被此注解注解的类?
大家讲道理
大家讲道理 2017-04-18 09:45:18
0
2
309

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

    }

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
左手右手慢动作

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.

迷茫

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template