[JSR308] help on a simple example

Florin Bratu kreathorex at gmail.com
Tue Oct 28 10:35:31 EDT 2008


Hello everyone!

Michael Ernst wrote:
> Florin-
>
>> this example, given on the website, does not work:
>>
>> List<@Annotation Object> objs;
>
> Can you give some more details?  For instance, exactly what does not work?
> Can you supply all the files necessary to reproduce the behavior you are
> seeing, and the exact commands that you ran?  What did you expect to
> happen, and what did happen?  These details are necessary for us to
> understand what is going wrong for you.
>
>                     -Mike
>

Thank you for the reply!

More specifically: I have a project contained in a directory tree structure
like the following:

AnnotationsTest
|-src/
|--def/
|---Annotation.java
|---AnnotationProcessor.java
|--usage/
|---Main.java
|-bin/
|--def/
|--usage/

As you can see, I have the *def* package, which contain two classes:

1) Annotation.java, which contains the annotation definition:

 package def;

 public @interface Annotation {

 }

2) AnnotationProcessor.java, which defines the processor for the annotation:

package def;

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.Element;
import javax.lang.model.element.TypeElement;

@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes(
        {
            "def.Annotation"
        }
    )
public class AnnotationProcessor extends AbstractProcessor {

    public static final String ANNOTATION = "def.Annotation";

    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {

        if (annotations.isEmpty()) {
            // called with no annotations
            return true;
        }

        for (TypeElement annotationElement : annotations) {
            System.out.println("Found annotation element:" +
annotationElement);
            if (
annotationElement.getQualifiedName().toString().equals(ANNOTATION) ) {
                processAnnot(roundEnv, annotationElement);
            }
        }

        return true;
    }

    private void processAnnot(RoundEnvironment roundEnv,
            TypeElement annotationElement) {

        Set<? extends Element> annotatedElements =
            roundEnv.getElementsAnnotatedWith(annotationElement);
        for( Element element : annotatedElements ) {
            System.out.println("Found annotation " + ANNOTATION + " applied
on the element " + element.toString() );
        }
    }
}

2) In the package *usage*, I have defined a test class, Main.java, with the
following contents:

package usage;

import java.util.List;

/*@def.Annotation*/
public class Main {

    List<@def.Annotation String> strings;

}

--------------------------------------------------------
I compile the classses from within the def package using the OpenJDK
compiler, patched with your JSR308 implementation. The output class files I
put in the bin/def/ directory.

I run compilation + annotation processing on Main.java, issuing the
following command:
$ javac -cp $CLASSPATH:../../bin/ -processorpath ../../bin/ -typeprocessor
def.AnnotationProcessor Main.java

The output is:
Found annotation element:def.Annotation
Found annotation def.Annotation applied on the element usage.Test2


So, I concluded that the annotation applied on the usage of the type String
is not detected, but the annotation placed on the declaration of the type
Main is detected.

It is odd, because it does not raise a syntax error - the OpenJDK compiler
does not recognize syntax constructs like     List<@def.Annotation String>
strings;
right? So this means that javac is correctly configured. But the annotation
processor is not called when the def.Annotation is encountered. I do not
understand why.

I tried using -processor option, instead of -typeprocessor; I get the same
output.

Thank you,
Florin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.csail.mit.edu/pipermail/jsr308/attachments/20081028/b16fe80b/attachment.htm 


More information about the JSR308 mailing list