@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value(); } public class MyClass { @MyAnnotation("Hello") public void myMethod() { // Method logic here } } @SupportedAnnotationTypes("com.example.MyAnnotation") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class MyAnnotationProcessor extends AbstractProcessor { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) { // Process annotation here } return true; } } <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <compilerArgs> <arg>-proc:none</arg> <arg>-processor com.example.MyAnnotationProcessor</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>


上一篇:
下一篇:
切换中文