Finishing daily study - notes and reflection

@interface

Use this annotation can be customized annotation, the class name is the annotation name. Can define custom annotations in the method, all the methods are no arguments no modifier, the return value is the basic data types, String, Classs, Annotation, Enum or a corresponding array.

Methods default setting default values.

 

Use annotations

The method may direct a single annotation annotation name ( value) method used

Multi-use method is annotated name (name = value method, the method name = value ...)

When there is a default value assignment process may not be displayed.

 

format

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface method name {

    // value as a parameter, meaning the entry statement is to define a string value type parameter , a default value is empty

String value() default “”;

 

    boolean isAlive() default false;

}

 

@Retention

Retention level defined annotations. ---- that is, when to use the annotation

His value is RetentionPolicy type, there are three values

The SOURCE : the presence of coding stage

The CLASS : stage compiling notes remain in the class file, need to keep the VM is running

RUNTIME : stage compiling notes remain in the class file remains comment VM runtime

 

@Target

Define the role of annotation objects. ---- That is where the use of the annotation

It ranges ElementType type, of the type commonly

The TYPE : class, interface (including notes), enumeration

The FIELD : member variables

The METHOD : member method

The PARAMETER : method parameters

ANNOTATION_TYPE : annotated notes

 

 

@Documented

Mark custom annotation tool javadoc should be recorded. ---- description that is marked annotated

This meta-annotation type is not a member of the

 

Related reflection

The class name

use

Class class

On behalf of the class of entities in a Java application running represent classes and interfaces

Field class

On behalf of the class member variables (also known as member variables of the class attribute)

Method class

Method represents a class of

Constructor class

Representative class constructor

Class class

Get the class attribute

getDeclaredFields () : get all the properties of an object

getDeclaredField (String name): of a property objects

 

Obtaining a class method RELATED

getDeclaredMethods () : get all the class methods

 

The method of obtaining the relevant class constructor

getDeclaredConstructors () : get all class constructors

 

The method of obtaining the relevant class notes

getAnnotation (Class <A> annotationClass) : Returns the class object is a public annotation matching the parameter type

 

Obtain the relevant class method

getSimpleName () : get the name of the class

 

isAccessible()setAccessible(true)

1. The isAccessible when () is allowed to access the result of reflection by the field to false

2. When This field private modified value derived isAccessible () is false, must be changed to true can access

3. So setAccessible (true); let's role is to get access to the private variable in the reflection

 

Guess you like

Origin www.cnblogs.com/haiyuan6688/p/11330006.html