java通过反射获取注解信息

该博客演示了Java中注解的使用,包括获取类、字段上的注解信息,并通过反射调用注解方法。主要涉及`getAnnotations()`, `getAnnotation()`, `getDeclaredField()`等API,展示了如何解析自定义注解`g`和`filedg`的属性值。
摘要由CSDN通过智能技术生成

关键字:getAnnotations getAnnotation getDeclaredField value

package com.g.demo;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

import com.g.demo.student2.filedg;
import com.g.demo.student2.g;

public class Test12 {
	
	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
		Class<?> c1 = Class.forName("com.g.demo.student2");
		Annotation[] annotations = c1.getAnnotations();
		for (Annotation annotation : annotations) {
			System.out.println(annotation);
		}
		g annotation = (g)c1.getAnnotation(g.class);
		
		String value = annotation.value();
		System.out.println(value);
		Field f = c1.getDeclaredField("name");
		filedg annotation2 = f.getAnnotation(filedg.class);
		System.out.println(annotation2.columnName());
		System.out.println(annotation2.length());
		System.out.println(annotation2.type());
		Field h = c1.getDeclaredField("age");
		filedg annotation3 = h.getAnnotation(filedg.class);
		System.out.println(annotation2.columnName());
		System.out.println(annotation2.length());
		System.out.println(annotation2.type());
		Field a = c1.getDeclaredField("id");
		filedg annotation4 = a.getAnnotation(filedg.class);
		System.out.println(annotation2.columnName());
		System.out.println(annotation2.length());
		System.out.println(annotation2.type());
	}
	
	
}
@g("kk")
class student2{
	@filedg(columnName = "id", length = 10, type = "int")
	private int id;
	@filedg(columnName = "age", length = 10, type = "int")

	private int age;
	@filedg(columnName = "name", length = 10, type = "string")

	private String name;
	public student2() {
		super();
		// TODO Auto-generated constructor stub
	}
	public student2(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "student2 [id=" + id + ", age=" + age + ", name=" + name + "]";
	}
	@Target(ElementType.TYPE)
	@Retention(RetentionPolicy.RUNTIME)
	@interface g{
		String value();
	}
	@Target(ElementType.FIELD)
	@Retention(RetentionPolicy.RUNTIME)
	@interface filedg{
		String columnName();
		String type();
		int length();
		
	}
}

@com.g.demo.student2$g(value=kk)
kk
name
10
string
name
10
string
name
10
string

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值