import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.RecordComponent;

public class EsploraReflection {
    @Retention(RetentionPolicy.RUNTIME)
    @interface Etichetta {
        String value();
    }

    @Etichetta("catalogo")
    public record Libro(String titolo, int pagine) {
        public String descrizione() {
            return titolo + " (" + pagine + " pagine)";
        }
    }

    public static void main(String[] args) throws ReflectiveOperationException {
        Class<Libro> tipo = Libro.class;
        System.out.println("tipo: " + tipo.getName());
        System.out.println("record: " + tipo.isRecord());
        for (RecordComponent componente : tipo.getRecordComponents()) {
            System.out.println("componente: " + componente.getName());
        }
        Etichetta etichetta = tipo.getAnnotation(Etichetta.class);
        System.out.println("etichetta: " + etichetta.value());
        Constructor<Libro> costruttore = tipo.getConstructor(String.class, int.class);
        Libro libro = costruttore.newInstance("Java", 320);
        Method metodo = tipo.getMethod("descrizione");
        System.out.println(metodo.invoke(libro));
    }
}
