Java Foundation Enhancement-reflection, annotations

Source: Internet
Author: User

    • Foundation strengthening
      • A review of generics
      • Reflection of two generic types
      • Three annotations

The Foundation strengthens one, reviews the generic type
Basic concepts of generics:
As an
ArrayList<E> example:
an integer in
ArrayList<E> , called a type parameter variable
ArrayList<Integer> , is called an actual type parameter
② The whole is called a ArrayList<E> generic type. /c9> the
whole ArrayList<Integer> called parameterized type (Parameterizedtype)
Define a generic method:
:
// <T>:泛型定义的声明,在返回值前面。publicfindOne// 对于静态泛型方法,都必须自己定义后才能使用publicstaticvoidfindTwo(){}// 定义多个泛型publicstaticfind(K k);
Define a class with a generic type:
// 类级别的泛型定义,类中的"实例方法"就可以直接使用了。publicclass Demo<T> {
Second, the reflection of the generic type
// 获取带有泛型信息的父类,例如:BaseDao<User>,这个整体称为:ParameterizedType,Type是Class类的接口Type type = XXX.class.getGenericsSuperclass(); ParameterizedType ptype = type;Clazz clazz = ptype.getActualTypeArguments()[0];
Third, annotations
Introduced
① starting with JDK5.0, Java adds support for metadata (MetaData), which is annotation (Annotation).
②annotation is actually a special tag in the code that is used instead of a configuration file. In the traditional way, the configuration tells the class how to run, and with annotations, the developer can tell the class how to run with annotations.
The
typical application of ③ in Java technology is that the annotations within the class can be obtained by reflection technology to determine how to run the class.
Basic annotations
①suppersswarnings: Suppress warning
Custom annotations

Definition of ① annotations

public @interface MyAnnotation {}

② Properties of annotations: neither as methods nor as fields.
The type that annotation properties can use: primitive (base type), string,class,annotation,enum or one-dimensional array of the above types.
If you assign a value to an attribute in the annotation, you can specify only one value, or multiple values.

public  @interface  myannotation_1  {     //Annotation's properties  String name ();    //a property with default value  int  Age () default     ; }

③ using annotations and attributes:

publicclass UseAnnotation {    @MyAnnotation"ABC"18)    publicvoidtest1(){}}
Reflection of annotations
the API:java.lang.reflect.AnnotatedElement involved

The interface is implemented by the class,constructor,field,method,packages.

// 获取自己所拥有的指定的注解类型,aType是一个注解类型的字节码对象。// 返回所有的注解// 判断自己有没有指定的注解类型boolean isAnnotationPresent(Class<? extends Annotation> aType);
Three states of the class

SOURCE:. java file
class:. class file
RUNTIME: In-memory class, loaded by the class loader into the memory of the. class file.

Life cycle of annotations used
The
default range of ① annotations is: bytecode state. Run time (runtime) is not.
Change the survival range of annotations using meta annotations

① annotations can only be used for annotations, which are meta annotations .

@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation_1 {}
Changing the placement of annotations using meta annotations
① placed on the class: @Target (Elementtype.type)

② put on Method: @Target (Elementtype.method)

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

Java Foundation Enhancement-reflection, annotations

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.