java反射以及自定义注解二者的使用

使用Java的反射机制,可以在运行时,动态地获取一个类的成员以及方法的信息。之前也看了一段时间,有点理解,今天来和新手们分享一下。有不对的地方请指出


那么注解和反射如何搭配使用呢?那么这里又要说明注解。大家可以在别的地方看到许多注解的资料,我就不写太多废话,我把我使用时一些容易错的地方再强调一下,

这里是我写好的一个demo,SelfDefinitionAnnotation 就是咱们自定义的一注解注解,那么来看看自定义注解的代码

package com.AnnotationDemo.test;


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


@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SelfDefinitionAnnotation
{
int id();
}
}

可以看到,@target这个注解的意思是,咱们自定义注解在后面的使用范围,这里的filed是类成员,method自然是类的方法,这里要强调的是,类成员必须是Public类型的

,@Retention是说明注解的作用范围,这里的参数表明此注解运行时起作用,很重要,记得写上。

接下来我们看到了@interface这里是对注解的声明,注解内部可以声明注解的内容,格式为:类型  名称();可以加入defalut来设置默认值,这些点有许多资源大家都可以找到。注解的声明看完了,我们来看看它的使用,这里我们建一个新类AnnotationTest,来使用我们自定义的注解这个类的声明如下

package com.AnnotationDemo.test;


import java.lang.reflect.Field;
import java.lang.reflect.Method;




public class AnnotationTest
{
@SelfDefinitionAnnotation(id =(int) 2)
public  int a;
public AnnotationTest()
{
this.a=0;
}
@SelfDefinitionAnnotation(id=(int) 1)
public int getA()
{
return a;
}


}

我们可以看到,我们给这个类的成员a上打上了注释,id是2(这里的a在java可以称为字段,后面会用field来调用a),还给getA方法打上了一个注解,id为1(这个getA方法一会我们会用Method方法来反射使用),接下来看一下我们的测试类,对注解以及反射进行测试。

public class Test
{
public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
AnnotationTest annotationTest = new AnnotationTest();
Class<? extends AnnotationTest> ClassType = annotationTest.getClass();
Method method = ClassType.getMethod("getA");
SelfDefinitionAnnotation selfDefinitionAnnotation = method.getAnnotation(SelfDefinitionAnnotation.class);
if(selfDefinitionAnnotation.id() == 1)
{
int a = (int) method.invoke(annotationTest);
System.out.println(a);
}
System.out.println(selfDefinitionAnnotation.id());


Field field = annotationTest.getClass().getField("a");

SelfDefinitionAnnotation selfDefinitionAnnotation2 = field.getAnnotation(SelfDefinitionAnnotation.class);
if(selfDefinitionAnnotation2.id() == 2)
System.out.println((int)field.get(annotationTest));


}
}

这里我们可以看到,使用了Method和Field这两个类,用来遍历AnnotationTest这个类的方法和成员变量,如上图的Method method = ClassType.getMethod("getA");和Field field = annotationTest.getClass().getField("a");,但是在使用反射之前,必须要做到的第一点就是:

获取被反射类的class对象,而获得这个对象有三种方法:

1.类名.class

2.类实例对象.getClass()

3.class.forName()方法

上面的getMethod和getField方法大家自己去查看java的API文档,注意,这里可以反射的filed成员必须是public声明的,否则无法反射到。invoke这个函数可能有点复杂,大家看API或者在搜索一些与这个方法相关的资料。我写的是只能反射特定方法的语句,Java中也有Method methods[]=classtype。getmethods;方法。大家可以自己动手查API,有不对或者说得不清楚的地方请大家指出。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值