Annotations

combine metadata with source-code files,instead of keeping it in external documents

They provide information that you need to fully describe your program,but that cannot be expressed in Java.


javal.lang

          @Override

          @Deprecated

          @SuppressWarnings

Basic syntax

   Syntactically,annotations are used in much the same way a modifiers.

Defining annotations

  compile to class files
 @Target defines where you can apply this annotation(a method or a field,for example)
@Retention defines whether the annotations are available in the source code(SOUCE),in the class files(CLASS),or at run time(RUNTIME).

Meta-annotations

@Target
@Retention
@Documented
@Inherited

Writing annotation processors

 
public <T extends Annotation>  java.lang.reflect.Method.getAnnotation(Class<T> annotationClass)
public Method[] getDeclaredMethods()
                            throws SecurityException


Annotation elements

Here is a list of the allowed types for annotation elements:
All primitives(int,float,boolean etc.)
String
Class
enums
Annotations
Arrays of any of the above

Default value constraints

elements must either have default values or values provided by the class that uses the annotation.
none of the non-primitive type elements are allowed to take null as a value.

Generating external files

If you define an element on an annotation with the name value,then as long as it is the only element type specified you don't need to use the name-value pair syntax:you can just specify the value in parentheses.

Alternative solutions 

when using multiple annotations,you cannot use the same annotation twice.

how not to use the rather long-winded name-value pair form for this nested annotation,respecifying the element name and the @interface name

Annotations don't support inheritance

Implementing the processor

public Annotation[] java.lang.reflect.Field.getDeclaredAnnotations()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值