Enhanced Java basics-reflection, annotation, and java basic reflection Annotation

Source: Internet
Author: User

Enhanced Java basics-reflection, annotation, and java basic reflection Annotation

  • Enhanced infrastructure
    • 1. Generic
    • Binary reflection
    • Three annotations

Basic enhancement I. Review generic
Basic concepts of generics:
To ArrayList<E>For example:
ArrayList<E>E In is called the type parameter variable
ArrayList<Integer>The Integer in is called the actual type parameter.
② Whole ArrayList<E>Generic Type
Whole ArrayList<Integer>ParameterizedType)
Define a generic method:
:
// <T>: Declaration of the generic definition, before the return value. Public <T> T findOne (Class <T> clazz) {}// for static generic methods, you must define them before using public static <T> void findTwo () {}// defines multiple generic public static <K, V> 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 <T> {}
Ii. Generic reflection
// Obtain the parent Class with generic information, for example, BaseDao <User>, which is called ParameterizedType and Type is the Class interface Type = XXX. class. getGenericsSuperclass (); ParameterizedType ptype = type; Clazz clazz = ptype. getActualTypeArguments () [0];
Iii. Annotations
Introduction
① Since 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 annotations
① SupperssWarnings: Alarm
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(){}}
Annotation reflection
Involved APIs: 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 <T> aType); // returns all Annotation [] getAnnotations (); // determines whether you have specified Annotation type boolean isAnnotationPresent (Class <? Extends Annotation> 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.

Lifecycle of Annotation
① 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 of Annotation
① Put it 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.