title: How to get class level annotation value using Java reflect?
body: <p>I am writing a function which takes Class instance as a parameter. I want to get value of a specific annotation defined over a class.
  Class: </p>
  
  <pre><code>@AllArgConstructor
  @MyAnnotation(tableName = "MyTable")
  public class MyClass {
      String field1;
  }
  </code></pre>
  
  <p>Function that want to retrieve annotation value.</p>
  
  <pre><code>public class AnnotationValueGetter{
  
      public String getTableName-1(Class reflectClass){
          if(reflectClass.getAnnotation(MyAnnotation.class)!=null){
              return reflectClass.getAnnotation(MyAnnotation.class).tableName();
  //This does not work. I am not allowed to do .tableName(). Java compilation error
  
  
          }
      }
  
      public String getTableName-2{
           Class reflectClass = MyClass.class;
           return reflectClass.getAnnotation(MyAnnotation.class).tableName()
          //This works fine.`enter code here`
      }
  }
  </code></pre>
  
  <p>MyAnnotation:</p>
  
  <pre><code>@DynamoDB
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.TYPE)
  @Inherited
  public @interface MyAnnotation {
  
      /**
       * The name of the table to use for this class.
       */
      String tableName();
  
  }
  </code></pre>
  
  <p>Function getTableName-1 shows me compilation error whereas getTableName-2 works just fine. What am I doing wrong here? I want to implement function similar to getTableName-1.</p>