Java反射移除注解,Java反射 - 注解

使用Java反射,您可以在运行时访问附加到Java类的注解。

什么是Java注释?

注释是Java 5中的一项新功能。注释是一种可以在Java代码中插入的注释或元数据。 这些注释可以在编译时通过预编译工具进行处理,也可以在运行时通过Java Reflection进行处理。 这是一个类注释的例子:

@MyAnnotation(name="someName", value = "Hello World")

public class TheClass {

}

类TheClass的注释@MyAnnotation写在自己类上。 注释被定义为接口。 这是MyAnnotation定义:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface MyAnnotation {

public String name();

public String value();

}

前面的@标记为注释。 一旦定义了注释,就可以在代码中使用它,如前面的示例所示。

注解定义中的两条指令@Retention(RetentionPolicy.RUNTIME)和@Target(ElementType.TYPE)指定了注释的使用方式。

@Retention(RetentionPolicy.RUNTIME)意味着注释可以在运行时通过反射来访问。 如果你没有设置这个指令,注释将不会在运行时被保留下来,因此不能通过反射来获得。

@Target(ElementType.TYPE)意味着注释只能在类型(通常是类和接口)的类上使用。 您也可以指定METHOD或FIELD,或者可以将目标放在一起,以便注释可以用于类,方法和字段。

类注解

您可以在运行时访问类,方法或字段的注释。 以下是访问类注释的示例:

Class aClass = TheClass.class;

Annotation[] annotations = aClass.getAnnotations();

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

您也可以像这样访问特定的类注解:

Class aClass = TheClass.class;

Annotation annotation = aClass.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

方法注解

以下是带注解的方法的示例:

public class TheClass {

@MyAnnotation(name="someName", value = "Hello World")

public void doSomething(){}

}

您可以像这样访问方法注释:

Method method = ... //obtain method object

Annotation[] annotations = method.getDeclaredAnnotations();

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

您也可以像这样访问特定的方法注释:

Method method = ... // obtain method object

Annotation annotation = method.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

参数注释

也可以为方法参数声明添加注释。

public class TheClass {

public static void doSomethingElse(

@MyAnnotation(name="aName", value="aValue") String parameter){

}

}

您可以像这样访问Method对象的参数注释:

Method method = ... //obtain method object

Annotation[][] parameterAnnotations = method.getParameterAnnotations();

Class[] parameterTypes = method.getParameterTypes();

int i=0;

for(Annotation[] annotations : parameterAnnotations){

Class parameterType = parameterTypes[i++];

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("param: " + parameterType.getName());

System.out.println("name : " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

}

请注意Method.getParameterAnnotations()方法如何返回一个二维Annotation数组,其中包含每个方法参数的注释数组。

字段注解

以下是带注释的字段示例:

public class TheClass {

@MyAnnotation(name="someName", value = "Hello World")

public String myField = null;

}

您可以像这样访问字段注释:

Field field = ... //obtain field object

Annotation[] annotations = field.getDeclaredAnnotations();

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

您也可以像这样访问特定的字段注释:

Field field = ... // obtain method object

Annotation annotation = field.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

实战

package com.reflection.detail;

import java.lang.annotation.Annotation;

/**

* Created by Fant.J.

* 2018/2/7 16:13

*/

public class Reflection_Annotations {

public static void main(String[] args) {

//获取对象

Class aClass = People.class;

Annotation[] annotations = aClass.getAnnotations();

//获取类注解

for(Annotation annotation : annotations){

if(annotation instanceof MyAnnotation){

MyAnnotation myAnnotation = (MyAnnotation) annotation;

System.out.println("name: " + myAnnotation.name());

System.out.println("value: " + myAnnotation.value());

}

}

}

}

name: someName

value: Hello World

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
移除Java代码注解,可以使用反射API的Annotation的方法来实现。具体步骤如下: 1. 获取需要移除注解或方法等元素的Class对象; 2. 使用Class对象的getDeclaredAnnotations()方法获取所有的注解对象; 3. 遍历注解对象数组,判断需要移除注解型,如果匹配,则通过反射API的AnnotatedElement接口的方法来移除注解。 示例代码如下: ```java import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; public class AnnotationUtils { /** * 移除指定型的注解 * * @param element 需要移除注解的元素 * @param clazz 需要移除注解型 */ public static void removeAnnotation(AnnotatedElement element, Class<? extends Annotation> clazz) { Annotation[] annotations = element.getDeclaredAnnotations(); for (Annotation annotation : annotations) { if (annotation.annotationType().equals(clazz)) { try { // 使用反射API的方法移除注解 Class<?> annotationType = annotation.annotationType(); Method method = annotationType.getDeclaredMethod("value"); method.setAccessible(true); Object value = method.invoke(annotation); InvocationHandler handler = (proxy, method1, args) -> value; Annotation proxyAnnotation = (Annotation) Proxy.newProxyInstance(annotationType.getClassLoader(), new Class[]{annotationType}, handler); Method setMethod = element.getClass().getMethod("set" + annotationType.getSimpleName(), annotationType); setMethod.invoke(element, proxyAnnotation); } catch (Exception e) { throw new RuntimeException("移除注解失败", e); } } } } } ``` 使用示例: ```java @MyAnnotation("test") public class Test { @MyAnnotation("test") public void test() { } } // 移除注解 Class<Test> clazz = Test.class; AnnotationUtils.removeAnnotation(clazz, MyAnnotation.class); // 移除方法注解 Method method = clazz.getMethod("test"); AnnotationUtils.removeAnnotation(method, MyAnnotation.class); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值