JAva注釈理論と注釈解析の例
4424 ワード
@Overrideは親のメソッドを上書きし、@Deprecatedタグの古いメソッドを書き換えます.@SuppressWarnings("deprecation"); deprecationの警告を無視
Spingの@Autowired@Service@Repository
Mybatisの@InsertProvider@UpdateProvider@Options
注記の分類1.実行メカニズムに従ってソースコード注釈(注釈は実際のソースコードに存在し、.classファイルにコンパイルすると存在しません)を分けてコンパイルするときの注釈(注釈はソースコードと.classファイルに存在します.)実行時注記(実行フェーズでも機能し、実行ロジックに影響を与える注記)2.ソースによってJDKからの注釈を第三者からの注釈に分け、私たちが定義した注釈3.メタ注釈
カスタム注記1.カスタム注釈の構文要件
2.注記の注記(メタ注記)
3.カスタム注釈の使用
4.解析注記
Spingの@Autowired@Service@Repository
Mybatisの@InsertProvider@UpdateProvider@Options
注記の分類1.実行メカニズムに従ってソースコード注釈(注釈は実際のソースコードに存在し、.classファイルにコンパイルすると存在しません)を分けてコンパイルするときの注釈(注釈はソースコードと.classファイルに存在します.)実行時注記(実行フェーズでも機能し、実行ロジックに影響を与える注記)2.ソースによってJDKからの注釈を第三者からの注釈に分け、私たちが定義した注釈3.メタ注釈
カスタム注記1.カスタム注釈の構文要件
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description{
// @interface
// , String,Class,Annotation,Enumeration
// , value(), (=)
// ,
String desc();//
String author();
int age() default 18;// default
}
2.注記の注記(メタ注記)
/*CONSTRUCTOR
FIELD
LOCAL_VARIABLE
METHOD
PACKAGE
PARAMETER
TYPE , */
@Target({ElementType.METHOD,ElementType.TYPE})
/*
SOURCE ,
CLASS class ,
RUNTIME ,
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited // ,
@Documented // javadoc
public @interface Description{
String desc();
String author();
int age() default 18;
}
3.カスタム注釈の使用
:
@(=,=,......)
@Description(desc="I am eyeColor",author="Mooc boy",age=18)
public String eyeColor(){
return "red";
}
// , @Description eyeColor() 。
4.解析注記
: 、 " " , 。
package util;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) // RUNTIME ,
@Inherited // , , 。
@Documented
public @interface Description{
// @interface
// , String,Class,Annotation,Enumeration
// , value(), (=)
// ,
/*String desc();// , , String,Class,Annotation,Enumeration
String author();
int age() default 18;// default */
String value();
}
package util;
@Description("I am annotation")
public class Child {
@Description("I am method")
public String name(){
return null;
}
}
package util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Iterator;
public class ParseAnn {
public static void main(String[] args) {
try {
//1.
Class c=Class.forName("util.Child");
//2.
boolean isExist=c.isAnnotationPresent(Description.class);
if(isExist){
//3.
Description d=(Description)c.getAnnotation(Description.class);
System.out.println(d.value());
}
//4.
Method[] ms=c.getMethods();
for (Method method : ms) {
boolean isMExist=method.isAnnotationPresent(Description.class);
if(isMExist){
Description d=method.getAnnotation(Description.class);
System.out.println(d.value());
}
}
//
for (Method method : ms) {
Annotation[] as=method.getAnnotations();
for (Annotation annotation : as) {
if(annotation instanceof Description){
Description d=(Description) annotation;
System.out.println(d.value());
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}