views:

15

answers:

1

I'm trying to track down th source of a warning:

warning: Cannot find annotation method 'itemFieldName()' in type 'com.thoughtworks.xstream.annotations.XStreamImplicit'

The relevant code is:

@XStreamAlias("things")
@XStreamImplicit(itemFieldName = "things")
private List<Thing> things;

Looking at the XStream JAR I see:

@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target({java.lang.annotation.ElementType.FIELD})
public static @interface XStreamImplicit {

    java.lang.String itemFieldName() default "";
}

So I'm not really sure why I'm getting the warning. It does not appear to be causing problems, but I'm getting a bunch of these in my compiler output and I'd like to tidy them up.

Edit: Did some more digging and found this: http://java.dzone.com/articles/when-good-annotations-go-bad look at the comment from "Fabrizio"

Hmm.... maybe I'm answering too in a hurry and I didn't understand well... but AFAIK when you have compiled a class C1 that is annotated with A, you can put C1 in the compile (and run) classpath for C2 without the need of putting A in C2 classpath.You only get warnings and A is ignored (as it's proper to do: annotations have a meaning only in a certain context, and in C2 context A is meaningless).

I' ve just double checked, compiling a sample class X against a JAR that contains javax.persistence annotations (but not putting jpa.jar in the compiler classpath):

istral:/tmp> javac -classpath it-tidalwave-catalog.jar X.java it/tidalwave/catalog/persistence/CategoryPB.class(it/tidalwave/catalog/persistence:CategoryPB.class): warning: Cannot find annotation method 'name()' in type 'javax.persistence.Table': class file for javax.persistence.Table not found it/tidalwave/catalog/persistence/CategoryPB.class(it/tidalwave/catalog/persistence:CategoryPB.class): warning: Cannot find annotation method 'length()' in type 'javax.persistence.Column': class file for javax.persistence.Column not found it/tidalwave/catalog/persistence/CategoryPB.class(it/tidalwave/catalog/persistence:CategoryPB.class): warning: Cannot find annotation method 'name()' in type 'javax.persistence.Column'

etc... Just warnings, the compilation is successful.

So I think, this is just an annotation/classpath issue.

A: 

I'm not really familiar with XStream - looks pretty nifty - but based on the nature of the annotations you are using (and the annotations tutorial), I think you have one too many annotations on your things. From my quick once-over, the alias annotation converts fully-qualified object names into more appropriate XML element names (the object names are the default). The implicit collections will work without any field name, just using the class name of the collection content type. If you want the collection elements in XML to have a different name, that's when you use the itemFieldName property.

My recommendation is to try the same code with just removing the @XStreamAlias annotation from things, and see if that fixes it. Also, if it works, I think you might want to change the value in itemFieldName, because as it stands, you'll have a collection of elements nameed things - I think using thing instead might work better.

Hope this helps!

mlschechter
I see where you're going. You are correct, using both annotations is unnecessary. http://xstream.codehaus.org/annotations-tutorial.html I wrote a test and proved it too. Didn't change the compiler warnings though.
Freiheit