groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From C├ędric Champeau <cedric.champ...@gmail.com>
Subject Re: Annotation on script
Date Fri, 12 Jun 2015 15:54:10 GMT
Hi!

An annotation cannot be directly set on a script. Is has to be on a
package, import, ... So if in your example you use a package, then you
should annotate the package declaration and get the annotation from it.

2015-06-12 17:46 GMT+02:00 Stefano Rocca <s.rocca@oandsi.it>:

> Hi,
> we're using groovy.util.GroovyScriptEngine to run our scripts and we would
> like to use reflection on the Script class returned by createScript to get
> the annotations declared in the script, for example:
>
> --- script1.groovy ---
> package xxxx
>
> @CustomAnnotation("xyz", description = "aaa bbb ccc")
>
> println "Hello world!"
>
> --- END ---
>
> --- CustomAnnotation.java ---
> package xxxx;
>
> import java.lang.annotation.Documented;
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
>
> @Documented
> @Retention(RetentionPolicy.RUNTIME)
> @Target({   ElementType.METHOD,         ElementType.TYPE,
>             ElementType.CONSTRUCTOR,    ElementType.FIELD,
>             ElementType.LOCAL_VARIABLE, ElementType.PACKAGE
> })
> public @interface CustomAnnotation {
>
>     String value() default "";
>     String description() default "";
>
> }
>
> --- END ---
>
> if we execute
>
> Script s = groovyScriptEngine.createScript("script1.groovy", new
> Binding()); // groovyScriptEngine is an instance of
> groovy.util.GroovyScriptEngine
>
> we cannot find the annotation CustomAnnotation anywhere in Script s (i.e.:
> s.getClass().getAnnotations() is empty)
>
> Is it possible to obtain such annotation?
>
> Thanks in advance.
>
> Stefano
>
>

Mime
View raw message