groovy

Can't reflect groovy script annotations in java

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Cannot Reproduce
  • Affects Version/s: 1.5
  • Fix Version/s: 1.5.2
  • Component/s: groovy-jdk
  • Labels:
    None
  • Environment:
    SUN JRE 1.5.0.13
    Windows XP SP2
  • Testcase included:
    yes
  • Number of attachments :
    0

Description

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.

Activity

Hide
Alexandru Popescu added a comment -

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.

Show
Alexandru Popescu added a comment - 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.
Hide
Vinícius Godoy de Mendonça added a comment -

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.

Show
Vinícius Godoy de Mendonça added a comment - 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.
Hide
Vinícius Godoy de Mendonça added a comment -

I forgot to mention: The problem also happens with GroovyScriptEngine.

Show
Vinícius Godoy de Mendonça added a comment - I forgot to mention: The problem also happens with GroovyScriptEngine.
Hide
Alexandru Popescu added a comment -

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.

Show
Alexandru Popescu added a comment - 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.
Hide
Vinícius Godoy de Mendonça added a comment -

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.

Show
Vinícius Godoy de Mendonça added a comment - 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.
Hide
Guillaume Laforge added a comment -

Re-open if you find the exact circumstances that lead to this problem.

Show
Guillaume Laforge added a comment - Re-open if you find the exact circumstances that lead to this problem.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: