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

Groovy's annotation support doesn't deal with default values correctly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Won't Fix
    • 1.5.4
    • 1.5.5
    • class generator
    • None

    Description

      Given a Java annotation like this:

      public @interface Foo {
          String bar() default "";
      }
      

      When used in Groovy on a method:

      class Bar {
           @Foo
           def myMethod() {}
      }
      

      The value of the "bar" attribute does not take into account the default value of a blank string and instead returns null when used via reflection. Hence Java code like this will throw an exception:

      String foo = ann.bar().length() == 0 ? null : ann.bar();
      

      As the Groovy annotated method returns null. The workaround is to do this:

      class Bar {
           @Foo(bar="")
           def myMethod() {}
      }
      

      But that is nasty

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            graemerocher Graeme Rocher
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: