History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-2392
Type: Bug Bug
Status: Closed Closed
Resolution: Cannot Reproduce
Priority: Major Major
Assignee: Unassigned
Reporter: Vinícius Godoy de Mendonça
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
groovy

Can't reflect groovy script annotations in java

Created: 13/Dec/07 02:27 PM   Updated: 23/Dec/07 12:15 PM
Component/s: groovy-jdk
Affects Version/s: 1.5
Fix Version/s: 1.5.2

Time Tracking:
Not Specified

Environment:
SUN JRE 1.5.0.13
Windows XP SP2

Testcase included: yes


 Description  « Hide
First, create the following annotation in Java:
package problem;

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.METHOD)
public @interface Test
{
    String name() default "";
}

Second, create the following groovy script:

import problem.Test;
class Script
{
    @Test(name="problems")
    void teste()
    {
    }
}

Finally, try to find the annotated method in Java:

GroovyClassLoader loader = new GroovyClassLoader(TesteMain.class.getClassLoader());
        Class clazz = loader.parseClass(new File("c:\\Script.groovy"));
        
        System.out.println(clazz.getCanonicalName());
        for (Method m : clazz.getMethods())
        {
            for (Annotation a : m.getDeclaredAnnotations())
                System.out.println(a.toString());
        }

The output will only show the class name, not the annotated method. Using a java class instead of a Groovy script, everything works fine.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Alexandru Popescu - 13/Dec/07 02:31 PM
How have you run the above code? (I have seen the same problem while working with the IntelliJ IDEA plugin). Can you try it out from console?

./alex

.w( the_mindstorm )p.


Vinícius Godoy de Mendonça - 13/Dec/07 02:38 PM
You can create a java class with a main, like this one:
public class Sample {
      public static void main(String[] args) {
         GroovyClassLoader loader = new GroovyClassLoader(TesteMain.class.getClassLoader());
         Class clazz = loader.parseClass(new File("c:\\Script.groovy"));
        
         System.out.println(clazz.getCanonicalName());
         for (Method m : clazz.getMethods()) {
              for (Annotation a : m.getDeclaredAnnotations())
                 System.out.println(a.toString());
         }
}

We are running using Eclipse, and we only import the "groovy-all.jar" file.
We do not use groovy eclipse plugin, since it's basically a java code.


Vinícius Godoy de Mendonça - 13/Dec/07 02:42 PM
I forgot to mention: The problem also happens with GroovyScriptEngine.

Alexandru Popescu - 14/Dec/07 07:27 PM
Have you tried to run your code from command line? This is the info I have asked for in my comment, but I don't seem to have got an answer.

./alex

.w( the_mindstorm )p.


Vinícius Godoy de Mendonça - 17/Dec/07 07:47 AM
Hi,

Sorry taking so many time to answer.

I've tested in the command prompt and everything worked fine.
Then, i decided to run in a new, fresh, eclipse project and it worked again.

What is really strange is: if I use the same code in my app environment, the problem still occurs. I'm completely sure about the groovy version i'm using. Is there any trace I can take?

Anyway, I'll try to reproduce the exact circumstances in which the error occurs.


Guillaume Laforge - 23/Dec/07 12:15 PM
Re-open if you find the exact circumstances that lead to this problem.