`
tkiicpp
  • 浏览: 81583 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Eclipse的编译器

阅读更多
转自:http://hi.baidu.com/yu270682210/blog/item/ef4283d5e977fecd51da4bbd.html
众所周知,Eclipse是带有自己的编译器的,而且我们一般都是使用这个默认自带的编译 器去编译自己的项目。但是这个编译器和Sun的javac是否一致呢?别说,还真有那么一点区别。

这是一个普通的 annotation:

Java代码
  1. @Target (ElementType.METHOD)   
  2. @Retention (RetentionPolicy.RUNTIME)   
  3. @Documented   
  4. public @interface Anno {   
  5.            
  6.      public String itemName();   
  7.        
  8. }  
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Anno {
  
 public String itemName();
 
}


下面是一个普通的类,使用了这个 annotation:

Java代码
  1. public class UseAnno   
  2. {   
  3.      @Anno (itemName= "test" )   
  4.      public void testMethod() {   
  5.          //   
  6.      }   
  7.   
  8. }  
public class UseAnno
{
 @Anno(itemName="test")
 public void testMethod() {
  //
 }

}


这两个文件无论是用eclipse 还是Sun的javac编译,都不会出错误。下面我们把annotation修改一下:

Java代码
  1. @Target (ElementType.METHOD)   
  2. @Retention (RetentionPolicy.RUNTIME)   
  3. @Documented   
  4. public @interface Anno {   
  5.            
  6.      public String itemName();   
  7.      public Integer b = new Integer( 2 );   
  8. }  
@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 的编译中会有差别,损失可就大了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics