Enhanced Java basics-reflection and Annotation

Source: Internet
Author: User

Enhanced Java basics-reflection and Annotation

Basic enhancement 1 review generic and generic reflection 3 annotations

Basic enhancement I. review basic concepts of generic generics: ArrayList For example: ① ArrayList E In is called the type parameter variable ArrayList The Integer in is called the actual type parameter ② the whole ArrayList Called generic type whole ArrayList ParameterizedType defines a generic method ::
//
  
   
: Declaration of the generic definition, before the return value. Public
   
    
T findOne (Class
    
     
Clazz) {}// for static generic methods, you must define them before using public static
     
      
Void findTwo () {}// defines multiple generic public static
      
        V find (K k );
      
     
    
   
  
Define a class with generics:
// Generic definition at the class level. You can directly use the "instance Method" in the class. Public class Demo
  
   
{}
  
Ii. Generic reflection
// Obtain the parent class with generic information, for example, BaseDao
  
   
This is called ParameterizedType, and Type is the Class interface Type = XXX. class. getGenericsSuperclass (); ParameterizedType ptype = type; Clazz clazz = ptype. getActualTypeArguments () [0];
  
Iii. Introduction to annotations ① beginning with JDK5.0, Java has added support for MetaData (MetaData), that is, Annotation (Annotation ). ② Annotation is actually a special mark in the code, which is used to replace the configuration file. In the traditional method, the configuration is used to tell the class how to run. With Annotations, developers can use annotations to tell the class how to run.
③ Annotated in Java Technology TypicalApplication: You can get the annotation in the class through the reflection technology to determine how to run the class. Basic annotation ① SupperssWarnings: Alarm suppression custom Annotation

① Annotation Definition

public @interface MyAnnotation {}

2. annotation attributes: Neither method nor field.
Annotation attributes can use the following types: primitive (basic type), String, Class, Annotation, enum, or a one-dimensional array of the above types.
If you assign values to attributes in the annotation, you can specify only one value or multiple values.

Public @ interface MyAnnotation_1 {// annotation attribute String name (); // int age () default 18 ;}

③ Use annotations and attributes:

public class UseAnnotation {    @MyAnnotation_1(name = "ABC", age = 18)    public void test1(){}}
API involved in Annotation reflection: java. lang. reflect. AnnotatedElement

Class, Constructor, Field, Method, and Packages all implement this interface.

// Obtain the specified annotation type that you own. aType is an annotation type bytecode object. Annotation getAnnotataion (Class
  
   
AType); // returns all Annotation [] getAnnotations (); // determines whether the Annotation type boolean isAnnotationPresent (Class
   AType );
  
Class Three States

SOURCE:. Java File
CLASS:. Class File
RUNTIME: Class in the memory. The class loader loads the. class file into the memory.

Life Cycle ① Annotated DefaultStored RangeYes: bytecode status. RUNTIME does not exist. Use meta annotation to change the survival range of the Annotation

① Annotations can only be used for annotation, that isMeta Annotation.

@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation_1 {}
Use meta annotation to change the place where the annotation is placed on the class: @ Target (ElementType. TYPE)

② Put it on the METHOD: @ Target (ElementType. METHOD)

@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface MyAnnotation_1 {}

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.