import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;

class Main {

    /**
     * Dichiarazione, assegnazione e navigazione di {@link <a href=
     * "https://[Log in to view URL]"
     * target="_blank">Lettura di un annotazione custom in run-time</a>}
     * 
     * @author itammb ( Italia Massimiliano Buscati )
     * @version JDK 1.15
     * 
     */
    public static class Example_002_01_Annotation {
      	/**
    	 * @see AnnotatedElement
    	 * @see Annotation
    	 *
    	 */  
    	public static void readAnnotationElement(AnnotatedElement element) {
    		try {
    			Annotation[] classAnnotations = element.getAnnotations();
    			for (Annotation annotation : classAnnotations)
    				parserAnnotation(annotation);
    		} catch (Exception exception) {
    			exception.printStackTrace();
    		}
    	}
    
    	private static void parserAnnotation(Annotation annotation) {
    		if (annotation instanceof Book) {
    			Book book = (Book) annotation;
    
    			System.out.println("Casa editrice: " + book.publishing_house().name());
    			System.out.println("Missione aziendale: " + book.publishing_house().description());
    
    			for (Author author : book.arrayAuthors())
    				System.out.println("Autore: " + author.name() + " " + author.surname());
    
    			System.out.println("Pubblicazione per: " + book.livel());
    		}
    	}
    
    	@Retention(RetentionPolicy.RUNTIME)
    	@Target(value = { ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR })
    	public @Documented @Inherited @interface Book {
    		public enum LIVEL {
    			BEGGINER, ADVANCED, GURU
    		};
    
    		LIVEL livel() default LIVEL.BEGGINER;
    
    		PublishingHouse publishing_house() default @PublishingHouse(name = "Apogeo", description = "editoria per l\'informatica");
    
    		Author[] arrayAuthors() default { @Author(name = "Massimiliano", surname = "Buscati") };
    	}
    
    	public @interface PublishingHouse {
    		String name();
    
    		String description();
    	}
    
    	public @interface Author {
    		String name();
    
    		String surname();
    	}
    
    	// @Book()
    	@Book(arrayAuthors = { @Author(name = "Massimiliano", surname = "Buscati"),
    			@Author(name = "Italo", surname = "Persechino") })
    	public class Libro {
    		private String title;
    
    		public String getTitle() {
    			return title;
    		}
    
    		protected void setTitle(String title) {
    			this.title = title;
    		}
    	}
    }

    public static void main(String[] args) {
        // unit test - lettura di un annotazione in run-time
        Example_002_01_Annotation.readAnnotationElement(Example_002_01_Annotation.Libro.class);
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: