मिनिमल उदाहरण
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.CLASS)
@interface RetentionClass {}
@Retention(RetentionPolicy.RUNTIME)
@interface RetentionRuntime {}
public static void main(String[] args) {
@RetentionClass
class C {}
assert C.class.getAnnotations().length == 0;
@RetentionRuntime
class D {}
assert D.class.getAnnotations().length == 1;
}
अगर हम एनोटेट वर्गों पर javap
उपयोग करते हैं, हम चाहते हैं कि 0,123,614 देखनाएनोटेट वर्ग एक RuntimeInvisible वर्ग विशेषता हो जाता है:
#14 = Utf8 LRetentionRuntime;
[...]
RuntimeVisibleAnnotations:
0: #14()
तो जानकारी बाईटकोड में दोनों ही मामलों पर मौजूद है:
#14 = Utf8 LRetentionClass;
[...]
RuntimeInvisibleAnnotations:
0: #14()
जबकि Retention.RUNTIME
एनोटेशन एक RuntimeVisible वर्ग विशेषता हो जाता है।
इसलिए, Runtime.CLASS
का उपयोग कक्षा में मनमाने ढंग से मेटाडेटा को जोड़ने के लिए किया जा सकता है, जो बाइटकोड मैनिपुलेशन उपकरण रनटाइम-दृश्य व्यवहार में हस्तक्षेप किए बिना उपयोग कर सकते हैं।
आप के साथ खेलने के लिए Examples on GitHub।
स्रोत
2015-05-04 14:36:05
यदि मैं एनोटेशन 'रिटेंशन पॉलिसी क्लास' (उदा। 'गुवा से' VisibleForTesting') वाली कक्षा को अपनाना चाहता हूं तो एनकोटेशन डिकंपील्ड क्लास फ़ाइल में दिखाई नहीं दे रहा है। लेकिन जावाडोक के अनुसार इस प्रकार की एनोटेशन कक्षा फ़ाइल में दर्ज की जाती है, फिर यह डिकंपील्ड जावा फ़ाइल में क्यों दिखाई नहीं दे रही है। कोई विचार? – tuk