我正在尝试追踪警告的来源:
warning: Cannot find annotation method 'itemFieldName()' in type 'com.thoughtworks.xstream.annotations.XStreamImplicit'
相关代码是:
@XStreamAlias("things")
@XStreamImplicit(itemFieldName = "things")
private List<Thing> things;
查看 XStream JAR,我看到:
@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 "";
}
所以我不太确定为什么会收到警告。它似乎没有引起问题,但我在编译器输出中收到了一堆这些问题,我想整理它们。
编辑:做了一些更多的挖掘,发现了这个: http://java.lang. dzone.com/articles/when-good-annotations-go-bad 看看“Fabrizio”的评论
嗯......也许我也在回答
急啊,我也不太明白……
但据我所知,当你编译了
用 A 注释的 C1 类,您
可以将C1放入编译(并运行)
C2 的类路径,无需
将 A 放入 C2 类路径中。你只会得到
警告并且 A 被忽略(因为它是
正确做法:注释有一个
仅在特定上下文中有意义,并且
在 C2 上下文中 A 毫无意义)。
我刚刚仔细检查过,正在编译
针对 JAR 的示例类 X
包含 javax.persistence 注释
(但不将 jpa.jar 放入
编译器类路径):
istral:/tmp> javac-类路径
it-tidalwave-catalog.jar X.java
it/tidalwave/catalog/persistence/CategoryPB.class(it/tidalwave/catalog/persistence:CategoryPB.class):
警告:找不到注释方法
类型中的“名称()”
'javax.persistence.Table':类文件
找不到 javax.persistence.Table
it/tidalwave/catalog/persistence/CategoryPB.class(it/tidalwave/catalog/persistence:CategoryPB.class):
警告:找不到注释方法
类型中的“length()”
'javax.persistence.Column':类文件
找不到 javax.persistence.Column
it/tidalwave/catalog/persistence/CategoryPB.class(it/tidalwave/catalog/persistence:CategoryPB.class):
警告:找不到注释方法
类型中的“名称()”
'javax.persistence.Column'
等等...只是警告,编译
成功了。
所以我认为,这只是一个注释/类路径问题。
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.
发布评论
评论(1)
我不太熟悉 XStream - 看起来很漂亮 - 但基于您正在使用的注释的性质(以及 注释教程),我认为您的
事物
上有太多注释。我快速浏览了一下,别名注释将完全限定的对象名称转换为更合适的 XML 元素名称(对象名称是默认名称)。隐式集合无需任何字段名称即可工作,仅使用集合内容类型的类名称。如果您希望 XML 中的集合元素具有不同的名称,则可以使用itemFieldName
属性。我的建议是尝试相同的代码,只需从
things
中删除@XStreamAlias
注释,看看是否可以解决问题。另外,如果它有效,我认为您可能想要更改itemFieldName
中的值,因为就目前情况而言,您将拥有一个名为things
的元素集合 - 我认为使用thing
可能会效果更好。希望这有帮助!
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 theitemFieldName
property.My recommendation is to try the same code with just removing the
@XStreamAlias
annotation fromthings
, and see if that fixes it. Also, if it works, I think you might want to change the value initemFieldName
, because as it stands, you'll have a collection of elements nameedthings
- I think usingthing
instead might work better.Hope this helps!