简体   繁体   中英

Annotations are not working in Groovy classes

I have Groovy-Eclipse for Juno but my Groovy classes are not able to recognize any Annotations. I am getting Groovy:class Translation is not an annotation in @Translation .Or Groovy:class Override is not an annotation in @Override For example:

import somewhere.Translation
@Translation(translationContext = TranslationContext.SOMETHING)
class SOMECLASS extends SOMETHING {

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Translation {

Here all the annotations @Target,@Retention and @interface are compiling fine for Translation
I have all the jars included in my project lib. What am I missing here?

Looks like you have your project/workspace configured for Java 1.4 or earlier. Annotations are not being recognized since the source level doesn't allow them.

Go to Project -> Properties -> Java Compiler. Enable project settings and set the compliance level to 1.5 or later.

  • Did you import import java.lang.annotation.*; ?
  • Is translationContext defined like a method in the @interface ?
  • Is the correct Translation in the classpath, and no duplicates are there.

This code works for me in the groovy console :

@Translation    
class A{
def meth = { print "HzzzI" }
​}

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)
public @interface Translation {
} 

A a = new A();
a.meth()

I have faced the same issue. My Eclipse IDE underlines the annotations but the code works fine! Just try to run it. That's it for me. Hope that's it for you as well)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM