You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Damir Murat <da...@gmail.com> on 2021/03/09 11:30:22 UTC

How to specify a default array value for annotation attribute with static compilation?

Without static compilation, annotation defined as bellow works as expected:

@interface SomeAnnitation {
 Class<?>[] someAttribute default []
}

However, with static compilation,

@CompileStatic
@interface SomeAnnitation {
 Class<?>[] someAttribute default []
}

I'm getting an error saying "Cannot return value of type java.util.List <E extends java.lang.Object> on method returning type java.lang.Class <?>[]"

I tried several things but failed. Is there a way to do this in Groovy, or should I just create the annotation in Java?

Tnx,
Damir Murat

Re: How to specify a default array value for annotation attribute with static compilation?

Posted by Bill James <ta...@gmail.com>.
Reading the error, it seems the issue is the default value "[]" which a
List, not an Array...
is there a way to change the default value to an Array?  Seems like that
would be a more useful solution.



<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Tue, Mar 9, 2021 at 12:08 PM Damir Murat <da...@gmail.com> wrote:

> Tnx Szymon. I opted for the first solution without CompileStatic.
>
>

Re: How to specify a default array value for annotation attribute with static compilation?

Posted by Damir Murat <da...@gmail.com>.
Tnx Szymon. I opted for the first solution without CompileStatic.

	

Re: How to specify a default array value for annotation attribute with static compilation?

Posted by Szymon Stępniak <sz...@gmail.com>.
You might actually don't need the @CompileStatic annotation in this
example. Here is what the bytecode decompiled to the Java class looks like
(an example without static compilation enabled):

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

public @interface SomeAnnotation {
    Class<?>[] someAttribute() default {};
}


This is exactly what you could expect from using static compilation.
However, if your annotation has some other methods where using the static
compilation useful, you can annotate problematic method
with @CompileDynamic, something like this:

import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic

@CompileStatic
@interface SomeAnnotation {

    @CompileDynamic
    Class<?>[] someAttribute() default []
}


On Tue, Mar 9, 2021 at 12:30 PM Damir Murat <da...@gmail.com> wrote:

> Without static compilation, annotation defined as bellow works as expected:
>
> @interface SomeAnnitation {
>  Class<?>[] someAttribute default []
> }
>
> However, with static compilation,
>
> @CompileStatic
> @interface SomeAnnitation {
>  Class<?>[] someAttribute default []
> }
>
> I'm getting an error saying "Cannot return value of type java.util.List <E
> extends java.lang.Object> on method returning type java.lang.Class <?>[]"
>
> I tried several things but failed. Is there a way to do this in Groovy, or
> should I just create the annotation in Java?
>
> Tnx,
> Damir Murat
>