Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Annotations modo di recupero a runtime logica

    Ciao,
    Codice PHP:
    import java.lang.annotation.*;
    import java.lang.reflect.*;
    // An annotation type declaration.
    @Retention(RetentionPolicy.RUNTIME)
    @interface 
    MyAnno {
      
    String str();
      
    int val();
    }
    class 
    Meta {
      
    // Annotate a method.
      
    @MyAnno(str "Annotation Example"val 100)
      public static 
    void myMeth() {
        
    Meta ob = new Meta();
        
    // Obtain the annotation for this method
        // and display the values of the members.
        
    try {
          
    // First, get a Class object that represents
          // this class.
          
    Class ob.getClass();
          
    // Now, get a Method object that represents
          // this method.
          
    Method m c.getMethod("myMeth");
          
    // Next, get the annotation for this class.
          
    MyAnno anno m.getAnnotation(MyAnno.class);
          
    // Finally, display the values.
          
    System.out.println(anno.str() + " " anno.val());
        } catch (
    NoSuchMethodException exc) {
           
    System.out.println("Method Not Found.");
        }
      }
      public static 
    void main(String args[]) {
        
    myMeth();
      }

    quello che mi pare strano in questo codice
    è che devo utilizzare m per recuperare l'annotazione MyAnno
    sinceramente non ci vedo il nesso
    (il nesso per me ci sarebbe se fosse solo all'interno del metodo
    e non in tutta la classe)
    potete illuminarmi grazie

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802

    Re: Annotations modo di recupero a runtime logica

    Originariamente inviato da whisher
    quello che mi pare strano in questo codice
    è che devo utilizzare m per recuperare l'annotazione MyAnno
    sinceramente non ci vedo il nesso
    (il nesso per me ci sarebbe se fosse solo all'interno del metodo
    e non in tutta la classe)
    potete illuminarmi grazie

    Cosa annota MyAnno? Il metodo myMeth. Quindi mi pare logico il fatto di dover chiedere a myMeth "ehi, tu che sei annotato con quella annotazione lì, passamela!"
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3

    Re: Re: Annotations modo di recupero a runtime logica

    Originariamente inviato da Alex'87
    Cosa annota MyAnno?
    ah quindi il nesso è qui
    Codice PHP:
    // Annotate a method. 
      
    @MyAnno(str "Annotation Example"val 100
      public static 
    void myMeth() { 
    be si in effetti ci sta anche scritto

    grazie
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.