转自:http://hi.baidu.com/yu270682210/blog/item/ef4283d5e977fecd51da4bbd.html
众所周知,Eclipse是带有自己的编译器的,而且我们一般都是使用这个默认自带
的编译 器去编译自己的项目。但是这个编译器和Sun的javac是否一致呢?别说,还真有那么一点区别。
这是一个普通的 annotation:
这是一个普通的 annotation:
- @Target (ElementType.METHOD)
- @Retention (RetentionPolicy.RUNTIME)
- @Documented
- public @interface Anno {
- public String itemName();
- }
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Anno {
public String itemName();
}
下面是一个普通的类,使用了这个 annotation:
- public class UseAnno
- {
- @Anno (itemName= "test" )
- public void testMethod() {
- //
- }
- }
public class UseAnno
{
@Anno(itemName="test")
public void testMethod() {
//
}
}
这两个文件无论是用 eclipse 还是Sun的javac编译,都不会出错误。下面我们把annotation修改一下:
- @Target (ElementType.METHOD)
- @Retention (RetentionPolicy.RUNTIME)
- @Documented
- public @interface Anno {
- public String itemName();
- public Integer b = new Integer( 2 );
- }
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Anno {
public String itemName();
public Integer b = new Integer(2);
}
然后在 eclipse 里编译,正常通过。运行的时候也不会出错误,而且运行时,用反射的方式也能正确地得到 annotation 里定义的那个变量。
但是如果你用 javac 编译呢?就会出现错误,无法编译,提示:
注释 Anno <clinit> 缺少。
关于 clinit ,大家可以 google 一下资料,我查到的资料是 javac 在处理 clinit 的时候有一些 bug,可能上面这种情况就是这个 bug 的表现症状之一了。
(之所以认为它是编译器的 bug,是因为 eclipse 编译后,在 java 环境中可以正确运行。)
所以:
1、如果可以的话,尽量不使用 eclipse 自带的编译器。
2、如果可以的话,使用 Netbeans 应该是正确的选择,毕竟都是出自Sun的手中。
3、养成持续集成的习惯。否则如果到后期,才发现一些特性在 javac 的编译中会有差别,损失可就大了。