2 Replies Latest reply: Jan 24, 2010 4:58 AM by 794081 RSS

    Access annotations in a simple class hierarchy

    794081
      Hello folks,

      I am facing a problem in runtime annotation processing (rather, testing) that I can't solve.

      SSCCE:
      package com.sun.forum;
      
      import java.lang.annotation.Annotation;
      
      public final class AnnotationAccess {
      
        @SuppressWarnings("all")
        private static class Base {
      
          private String field1;
          private String field2;
      
          Base(String field1, String field2) {
            this.field1 = field1;
            this.field2 = field2;
          }
        }
      
        @Deprecated
        private static class Extended extends Base {
      
          private Integer field1;
      
          private Extended(Integer field1) {
            super("Field 1", "Field 2");
            this.field1 = field1;
          }
        }
      
        public static void main(String[] args) {
          Annotation suppressWarnings = Base.class.getAnnotation(SuppressWarnings.class);
          Annotation deprecated = Extended.class.getAnnotation(Deprecated.class);
      
          System.out.println("Base annotation: " + suppressWarnings);
          System.out.println("Extended annotation: " + deprecated);
        }
      }
      Question:

      Why is "suppressWarnings" NULL in the above example?