groovy

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

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Won't Fix
  • Affects Version/s: 1.5.4
  • Fix Version/s: 1.5.5
  • Component/s: class generator
  • Labels:
    None
  • Number of attachments :
    0

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

Activity

Hide
blackdrag blackdrag added a comment -

setting fix version to 1.6-beta1, because annotation definitions won't go in 1.5.x for now

Show
blackdrag blackdrag added a comment - setting fix version to 1.6-beta1, because annotation definitions won't go in 1.5.x for now
Hide
blackdrag blackdrag added a comment -

I am sorry, I did misread the issue... it is not about a annotation definition, but it is about the the usage of an annotation, something which is possible in 1.5.x, so I will fix it for there too

Show
blackdrag blackdrag added a comment - I am sorry, I did misread the issue... it is not about a annotation definition, but it is about the the usage of an annotation, something which is possible in 1.5.x, so I will fix it for there too
Hide
blackdrag blackdrag added a comment -

I tested this code in 1.6

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
    String bar() default "";
}

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

int hits = 0
Bar.class.declaredMethods.each {
   if (it.name == "myMethod") {
     hits++
     it.declaredAnnotations[0].bar()==""
   }
}
assert hits==1

and this code works. So it seems 1.6 is not ignoring the annotation. In 1.5 my test showed that it works too.

btw, null is an illegal value for an annotation. And since an annotation can not be changed at runtime I guess that something went very wrong, but not with the annotation code itself.

Show
blackdrag blackdrag added a comment - I tested this code in 1.6
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
    String bar() default "";
}

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

int hits = 0
Bar.class.declaredMethods.each {
   if (it.name == "myMethod") {
     hits++
     it.declaredAnnotations[0].bar()==""
   }
}
assert hits==1
and this code works. So it seems 1.6 is not ignoring the annotation. In 1.5 my test showed that it works too. btw, null is an illegal value for an annotation. And since an annotation can not be changed at runtime I guess that something went very wrong, but not with the annotation code itself.
Hide
blackdrag blackdrag added a comment -

I close this as it seems this was no Groovy issue

Show
blackdrag blackdrag added a comment - I close this as it seems this was no Groovy issue

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: