Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6705

Compiler Doesn't Enforce Required Annotation Attributes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.1.9
    • None
    • Compiler

    Description

      I don't know if this is the same as GROOVY-3454 or not. That issue is marked resolved but I am seeing the problem with 2.1.9.

      Code from the attached app...

      src/main/java/com/demo/MyAnnotation.java
      package com.demo;
      
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.FIELD)
      public @interface MyAnnotation {
          String value();
          String code() default "";
      }
      
      src/test/groovy/com/demo/MyAnnotationSpec.groovy
      package com.demo
      
      class MyAnnotationSpec extends spock.lang.Specification {
      
          void 'test code'() {
              expect:
              Widget.getDeclaredField('name').getAnnotation(MyAnnotation).code() == 'alpha'
          }
      }
      
      class Widget {
          @MyAnnotation(code='alpha')
          String name
      }
      

      That test compiles and passes. I expected the compiler to fail on the Widget class since the annotation does not provide a value for the "value" attribute.

      Am I overlooking something simple or is this broken?

      Attachments

        1. annotation.zip
          51 kB
          Jeff Brown

        Issue Links

          Activity

            People

              Unassigned Unassigned
              brownj Jeff Brown
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: