@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}
public class UserService {
@LogExecutionTime
public void saveUser(User user) {
// ...
}
}
@SupportedAnnotationTypes("LogExecutionTime")
public class LogExecutionTimeProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(LogExecutionTime.class)) {
if (element instanceof ExecutableElement) {
ExecutableElement method = (ExecutableElement) element;
System.out.println("Method " + method.getSimpleName() + " is annotated with LogExecutionTime");
}
}
return true;
}
}
META-INF/services/javax.annotation.processing.Processor
com.example.LogExecutionTimeProcessor