代码编织梦想

 

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String value();
    int[] numbers();
}

This creates an annotation called MyAnnotation that can be applied to methods. The annotation has two properties: value (a string) and numbers (an array of integers). The @Retention annotation specifies that the annotation should be retained at runtime, and the @Target annotation specifies that the annotation can only be applied to methods.

public class MyClass {
    @MyAnnotation(value = "hello", numbers = {1, 2, 3})
    public void myMethod() {
        // method body
    }
}

In this example, MyAnnotation is applied to the myMethod() method with a value of "hello" and an array of numbers [1, 2, 3]. You can access the values of the annotation at runtime using reflection:

MyAnnotation annotation = MyClass.class.getMethod("myMethod").getAnnotation(MyAnnotation.class);
System.out.println(annotation.value()); // prints "hello"
System.out.println(Arrays.toString(annotation.numbers())); // prints "[1, 2, 3]"

This code retrieves the MyAnnotation annotation from the myMethod() method of the MyClass class and prints its value and numbers properties.

You can also create a custom annotation processor to generate code based on your annotations. Here's an example:

import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.tools.*;
import java.io.*;
import java.util.*;

@SupportedAnnotationTypes("MyAnnotation")
public class MyAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
            MyAnnotation annotation = element.getAnnotation(MyAnnotation.class);
            String value = annotation.value();
            int[] numbers = annotation.numbers();
            String methodName = element.getSimpleName().toString();

            String generatedClassName = methodName + "Generated";
            String generatedClassContent = "public class " + generatedClassName + " {\n";
            generatedClassContent += "  public static void main(String[] args) {\n";
            generatedClassContent += "    System.out.println(\"" + value + "\");\n";
            generatedClassContent += "    System.out.println(Arrays.toString(new int[] {" + Arrays.toString(numbers).replaceAll("\\[|\\]", "") + "}));\n";
            generatedClassContent += "  }\n";
            generatedClassContent += "}\n";

            try {
                JavaFileObject javaFileObject = processingEnv.getFiler().createSourceFile(generatedClassName);
                Writer writer = javaFileObject.openWriter();
                writer.write(generatedClassContent);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
}

This annotation processor generates a new Java class for each method annotated with MyAnnotation. The generated class simply prints the value and numbers properties of the annotation. To use this annotation processor, you need to create a file called javax.annotation.processing.Processor in the META-INF/services directory of your project, containing the fully qualified name of your annotation processor:

com.example.MyAnnotationProcessor

When you compile your project, the annotation processor will automatically generate the necessary code for each method annotated with MyAnnotation.

To use the MyAnnotationProcessor, you would typically compile your Java code with the -processor option, like this:

javac -processor com.example.MyAnnotationProcessor MyClass.java

This will run the MyAnnotationProcessor on the MyClass file and generate the necessary code.

Keep in mind that this is just a simple example to demonstrate how annotations and annotation processors work in Java. In real-world scenarios, you would typically use annotations and annotation processors to generate more complex code, such as code for dependency injection or serialization.

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qfljg/article/details/129630901

java annotation 入门-爱代码爱编程

摘要:   本文针对java初学者或者annotation初次使用者全面地说明了annotation的使用方法、定义方式、分类。初学者可以通过以上的说明制作简单的annotation程序,但是对于一些高级的annotation应用(例如使用自定义annotation生成javabean映射xml文件)还需要进一步的研究和探讨。涉及到深入annotati

java annotation 手册-爱代码爱编程

一、Annotation究竟是什么?   Annotation 提供了一条与程序元素关联任何信息或者任何元数据(metadata)的途径。从某些方面看,annotation就像修饰符一样被使用,并应用于包、类 型、构造方法、方法、成员变量、参数、本地变量的声明中。这些信息被存储在annotation的“name=value”结构对中。 annota

java annotation 原理_学习Java Annotation的原理和应用-爱代码爱编程

看了java matrix版主cleverpig的三篇annotation的介绍文章后,对annotaion有了一定的了解: 试验代码: 客户端代码TestAnnotaion, 这个客户端可以是一个AOP的advisor,也可以是框架的Controlller package org.airlinkmatrix.sample; import

java annotation入门_Java Annotation入门-爱代码爱编程

Java Annotation入门作者:cleverpig 版权声明:本文可以自由转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明 作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig) 原 文:[http://www.matrix.org.cn/resour

java 新增注解_java annotation(如何创建新的注解)小结-爱代码爱编程

packagesample;importjava.lang.annotation.Annotation;importjava.lang.reflect.Field;importjava.lang.reflect.Method;public classClient {public static voidmain(String[] args){ Syst

java annotation评价_Java Annotation 浅析-爱代码爱编程

1. 定义 annotate 英文里面注解的意思。Java annotation 又称为java 内注( 内建) 的意思。从sun 官方网站上面看到关于java 内注的定义:java 内注提供了关于代码的一些数据( 注解), 它本身不是java 代码的一部分。它不能直接影响它注释的代码的运行。java 在jdk5 之前本身提供了一些专门的注解机制(

java annotation 开发_java Annotation 简单理解-爱代码爱编程

代码解释 : 获取类SampleClass 中 getSampleField 方法上被CustomAnnotation的注解,并打印出注解的属性的name和value值 package com.qimh.anotation; import java.lang.annotation.Annotation; import java.lang.ref

java 单侧 忽略异常注解,java annotation 注解 异常处理-爱代码爱编程

package com.le; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.annotation.ElementType; import java.lang.annotation.Ret

如何优雅的用poi导入excel文件-爱代码爱编程

在企业级项目开发中,要经常涉及excel文件和程序之间导入导出的业务要求,那么今天来讲一讲excel文件导入的实现。java实现对excel的操作有很多种方式,例如EasyExcel等,今天我们使用的是POI技术实现excel文件的导入。 POI技术简介 1.POI概念 Apache POI 是用Java编写的免费开源的跨平台的Java AP

jvm监控搭建-爱代码爱编程

文章目录 JVM监控搭建整体架构JolokiaTelegrafInfluxdbGrafana JVM监控搭建 整体架构 JVM 的各种内存信息,会通过 JMX 接口进行暴露。 Jolokia

redisson项目地址-爱代码爱编程

github 项目 https://github.com/redisson/redisson 整合 spring boot 使用 https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter#spring-boot-starter 对应配置类以