java自定义注解实例

1、创建注解;(至于Retention,Target,Documented的含义自行学习)

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)		// VM将在运行期保留注释,因此可以通过反射机制读取注解的信息
@Target(ElementType.TYPE)		// 表示注解类型,TYPE为类,接口,枚举的声明
@Documented		// 表示此注解将包含在javadoc中
public @interface TypeAnnotation {
	/** 类名 */
	String ClassName();
	/** 描述 */
	String Description();
	/** 作者 */
	String author();
	/** 创建日期 */
	String date();
}

2、测试类及方法;

import com.annotation.TypeAnnotation;

@TypeAnnotation (ClassName="AnnotationTest", Description = "测试自定义类注解", author = "rencht", date = "2013-4-8 上午10:07:54")
public class AnnotationTest {

	public static void main(String[] args) throws Exception {
		AnnotationTest test = new AnnotationTest();
		
		TypeAnnotation annotation = test.getClass().getAnnotation(TypeAnnotation.class);
		System.out.println(annotation.ClassName());
		System.out.println(annotation.Description());
		System.out.println(annotation.author());
		System.out.println(annotation.date());

	}
}

3、输出结果;

AnnotationTest
测试自定义类注解
rencht
2013-4-8 上午10:07:54


Tips:

1、由于使用反射机制取得注解,注解类型为局部变量LOCAL_VARIABLE时无法取得,那该类型注解的意义何在?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的自定义注解是用于定义自己的注解类型,它们可以用于声明在类、方法、变量等代码元素上。自定义注解的基本使用方法包括以下几个步骤: 1. 定义注解类型:使用 `@interface` 关键字定义一个新的注解接口。注解接口中可以定义成员变量,这些成员变量的默认值可以通过 `default` 关键字指定。 ```java public @interface MyAnnotation { String value() default "Default Value"; int number() default 0; } ``` 2. 应用注解:将自定义的注解应用到类、方法或字段等代码元素上。 ```java @MyAnnotation(value = "Example", number = 10) public class MyClass { @MyAnnotation(value = "Field", number = 20) private String myField; @MyAnnotation(value = "Method", number = 30) public void myMethod() { } } ``` 3. 处理注解:注解本身不会对代码的行为产生影响,除非通过反射机制来读取这些注解。可以使用 `java.lang.reflect` 包中的类,如 `AnnotatedElement` 接口的实现类(`Class`、`Method`、`Field` 等),结合 `getAnnotation` 或 `getAnnotations` 方法来获取注解实例。 ```java import java.lang.reflect.Field; import java.lang.reflect.Method; public class AnnotationProcessor { public static void processAnnotations(Class<?> clazz) { // 检查类注解 MyAnnotation classAnnotation = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class); if (classAnnotation != null) { System.out.println("Class annotation value: " + classAnnotation.value()); } // 遍历类中的字段 for (Field field : clazz.getDeclaredFields()) { MyAnnotation fieldAnnotation = field.getAnnotation(MyAnnotation.class); if (fieldAnnotation != null) { System.out.println("Field annotation value: " + fieldAnnotation.value()); } } // 遍历类中的方法 for (Method method : clazz.getDeclaredMethods()) { MyAnnotation methodAnnotation = method.getAnnotation(MyAnnotation.class); if (methodAnnotation != null) { System.out.println("Method annotation value: " + methodAnnotation.value()); } } } } ``` 4. 元注解:在定义注解时,可以使用一些预定义的注解来提供额外的元数据信息。例如 `@Retention` 指定注解的保留策略,`@Target` 指定注解可以应用的元素类型等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值