This discussion is archived
4 Replies Latest reply: Jan 26, 2010 3:41 PM by EJP RSS

Annotation and enum

843793 Newbie
Currently Being Moderated
Hi... there is a strange behavior compiling an annotation that contains an enum. Here an example of annotation:
package my.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import my.event.Event.Phase;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface EventConfig {
   Phase phase() default Phase.PROCESS;
} 
Here the declaration of Phase enum:

package my.event;

public class Event<T>
{
   static public enum Phase {
      ANY, DECODE, PROCESS, RENDER
   }

}
Here a simple annotation processor:
package my.annotation.processor;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;


@SupportedAnnotationTypes(value = { "other.annotation.ComponentConfig" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class MyAnnotationProcessor extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
return true;
}
} 
Here the service file META-INF/services/javax.annotation.processing.Processor:
my.annotation.processor.MyAnnotationProcessor
I compile and create a jar for the Annotation Processor containinng the service file: MyAnnotationProcessor.jar

Here there is the commands to compile theese classes:
javac -cp $PROCESSOR_PROJECT/MyAnnotationProcessor.jar my/event/Event.java my/annotation/EventConfig.java 
It returns a compilation error:
my/annotation/EventConfig.java:13: incompatible types
found   : my.event.Event.Phase
required: my.event.Event.Phase
   Phase phase() default Phase.PROCESS;
If I delete the 'default Phase.PROCESS;' code, the compile succeed.

if I delete the MyAnnotationProcessor.jar from the classpath the compile succeed too.

I find strange that there is a different java syntax when you compile a enum together an annotation processor. Maybe is it a bug?

Edited by: luca.stancapiano on Nov 14, 2009 3:21 PM
  • 1. Re: Annotation and enum
    843793 Newbie
    Currently Being Moderated
    You don't say which version of javac you are using.

    I recommend seeing if the problem persists if you use the "-processor" and "-processpath" command-line flags instead of the META-INF/services file.
  • 2. Re: Annotation and enum
    843793 Newbie
    Currently Being Moderated
    Hi...thanks for the response. .I tried the 1.6 update 17 and the old update 07.
    I confirm you that if I use the -processor and -processorpath instead of the service file I get the same error.
    I signalized this problem to the bug application of the sun, actually I have no response
  • 3. Re: Annotation and enum
    843793 Newbie
    Currently Being Moderated
    I resolved the problem using the static import. The Annotation become:

    package my.annotation;

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;

    import static my.event.Event.Phase.PROCESS;

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    public @interface EventConfig {
    Phase phase() default PROCESS;
    }

    It seems that the static import knows the position of the Enum in the classpath respect to the simple import that let the duplication of the class. It should be fixed
  • 4. Re: Annotation and enum
    EJP Guru
    Currently Being Moderated
    So report it as a bug.