// Built-in annotations
@Override
public String toString() {
return "Custom toString";
}
@Deprecated
public void oldMethod() {
// Deprecated method
}
@SuppressWarnings("unchecked")
public void suppressWarning() {
// Code with unchecked warnings
}
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
// Using custom annotations
@Author(name = "Alice", date = "2024-01-15", tags = {"api", "v1"})
public class ApiService {
@LogExecution
@Scheduled(priority = Priority.HIGH)
public void processData() {
// Method implementation
}
@MaxLength(255)
private String apiKey;
}
// Processing annotations at runtime
Class<?> clazz = ApiService.class;
if (clazz.isAnnotationPresent(Author.class)) {
Author author = clazz.getAnnotation(Author.class);
System.out.println("Author: " + author.name());
System.out.println("Tags: " + Arrays.toString(author.tags()));
}
// Get method annotations
for (var method : clazz.getMethods()) {
if (method.isAnnotationPresent(Scheduled.class)) {
Scheduled scheduled = method.getAnnotation(Scheduled.class);
System.out.println("Method " + method.getName() + " priority: " + scheduled.priority());
}
}