import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Arg {
String name();
Class<?> type();
boolean required() default true;
}
import java.lang.reflect.Field;
public class ArgsHandler {
public static void handle(Object object) throws IllegalAccessException {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Arg.class)) {
Arg annotation = field.getAnnotation(Arg.class);
String name = annotation.name();
Class<?> type = annotation.type();
boolean required = annotation.required();
// ...
}
}
}
}
public class Main {
@Arg(name = "name", type = String.class, required = true)
private String name;
public static void main(String[] args) throws IllegalAccessException {
Main main = new Main();
ArgsHandler.handle(main);
// ...
}
}