Java Basic source Code (4)--reflect Packet-annotatedelement interface

Source: Internet
Author: User

Interface:annotatedelement

* Represents an annotated element of the program currently running in this * VM.  This interface allows annotations to be read reflectively. All * Annotations returned by methods in this interface is immutable and * serializable. The arrays returned by methods of this interface is modified * by callers without affecting the arrays returned to Oth  ER callers. A comment element that represents the program currently running in this virtual machine, which allows reflection to read comments, all comments returned through the interface method
are immutable and serializable. The display returned by the method of this interface can be modified by calling, without affecting other callers returning the array.
*/ Public Interfaceannotatedelement {/*** Returns True if the annotation for the specified type * are <em>present</em> on this element, Els E false.    This method * was designed primarily for convenient access to marker annotations. Returns true if a comment of the specified type exists on this element, otherwise false, which is primarily used to facilitate
Access tag Annotations
*/ default BooleanIsannotationpresent (class<?extendsAnnotation>Annotationclass) { returnGetannotation (annotationclass)! =NULL; } /*** Returns This element ' s annotation for the specified type if * Such a annotation is <em>present</e   M>, else null. If such an annotation exists, it returns the annotation, otherwise null is returned*/<textendsAnnotation> T getannotation (class<t>Annotationclass); /*** Returns annotations that is <em>present</em> on the This element. * Returns annotations that exist on this element*/annotation[] Getannotations (); /*** Returns annotations that is <em>associated</em> with this element. Returns the annotations associated with this annotation*/ default<textendsAnnotation> t[] Getannotationsbytype (class<t>Annotationclass) {//Default call Getdeclaredannotationsbyte incoming Annotationclass as parametert[] Result=Getdeclaredannotationsbytype (Annotationclass); If the returned array is longer than 0, the array is returned,
If the returned array is 0 length and this annotationelement is a class,
and the parameter type is an inheritable annotation type, and the annotatedelement of the annotatedelement is non-empty
The result returned is the result of calling Getannotationsbytype on the parent class, with Annotationclass as argument
Otherwise returns an array of length 0
if(Result.length = = 0 &&//neither directly nor indirectly present This instanceofClass &&//The element is a classAnnotationtype.getinstance (Annotationclass). isinherited ()) {//inheritableClass<?> superclass = ((class<?>) This). Getsuperclass (); if(Superclass! =NULL) { //determine if the annotation is associated with the//Superclassresult =Superclass.getannotationsbytype (Annotationclass); } } returnresult; } /*** Returns This element ' s annotation for the specified type if * Such a annotation is <em>directly pre    Sent</em>, else null. If such annotations exist directly, the element annotations of the specified type are returned, otherwise null is returned, and this method ignores the inherited annotations*/ default<textendsAnnotation> T getdeclaredannotation (class<t>Annotationclass) {objects.requirenonnull (Annotationclass); //Loop over all directly-present annotations looking for a matching one for(Annotation annotation:getdeclaredannotations ()) {if(Annotationclass.equals (Annotation.annotationtype ())) {//More powerful, dynamic conversion during compilation returnannotationclass.cast (annotation); } } return NULL; } default<textendsAnnotation> t[] Getdeclaredannotationsbytype (class<t>Annotationclass) {objects.requirenonnull (Annotationclass); returnAnnotationsupport. Getdirectlyandindirectlypresent (Arrays.stream (Getdeclaredannotations ()). Collect (Collectors.tomap (Annotation::annotationtype, F Unction.identity (), ((First,second)-First ), Linkedhashmap::New) ), annotationclass); } /*** Returns annotations that is <em>directly present</em> on the This element.    * This method ignores inherited annotations. Returns a comment that exists directly on this element*/annotation[] Getdeclaredannotations ();}

Example of the Isannotationpresent method:

@Retention (retentionpolicy.runtime) @Interfacecus{ PublicString name ();  PublicString value ();} @Cus (Name= "SampleClass", value = "Sample Class Annotation")classsampclass{PrivateString Samplefileld; @Cus (name= "SampleMethod", value = "Sample Method Annotation")     PublicString SampleMethod () {return"Sample"; }     PublicString Getsamplefileld () {returnSamplefileld; }     Public voidSetsamplefileld (String sa) { This. samplefileld=sa; }} Public classAccessibleobjectdemo { Public Static voidMain (String [] args)throwsnosuchmethodexception {accessibleobject SampleMethod= Sampclass.class. GetMethod ("SampleMethod"); System.out.println ("Samplemethod.isannotationpresent:" +samplemethod.isannotationpresent (Cus.class)); //output Result: SampleMethod.isAnnotationPresent:true    }}
Getannotations () Example:
@Retention (retentionpolicy.runtime) @Interfacett{ PublicString name ();  PublicString value ();} @Tt (Name= "SampleClass", value = "Sample Class Annotation")classsampclass2{PrivateString Samplefileld; @Tt (Name= "SampleMethod", value = "Sample Method Annotation")     PublicString SampleMethod () {return"Sample"; }     PublicString Getsamplefileld () {returnSamplefileld; }     Public voidSetsamplefileld (String sa) { This. samplefileld=sa; }} Public classAccessibleObjectDemo2 { Public Static voidMain (String [] args)throwsnosuchmethodexception {accessibleobject SampleMethod= SampClass2.class. GetMethod ("SampleMethod"); annotation[] Annotations=samplemethod.getannotations ();  for(inti=0;i<annotations.length;i++){            if(Annotations[i]instanceofTt) {Tt Cus2=(Tt) annotations[i];                System.out.println (Cus2.name ());                System.out.println (Cus2.value ()); /*Output Result: SampleMethod sample Method Annotation **/            }        }    }}

Java Basic source Code (4)--reflect Packet-annotatedelement interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.