我可以在方法体内使用注释吗?

tow*_*owi 6 java annotations exception

允许Java注释的语义将它们放在函数体内的某个位置,例如注释特定的函数调用,语句或表达式?

例如:

class MyClass {
    void theFunc(Thing thing) {
        String s = null;
        @Catching(NullPointerException)   //<< annotation ?
          s = thing.getProp().getSub().getElem().getItem();
        if(s==null)
            System.out.println("null, you fool");
    }
}
Run Code Online (Sandbox Code Playgroud)

缩写经常写的(经常,绝对!):

class MyClass {
    void theFunc(Thing thing) {
        String s = null;
        try {
            s = thing.getProp().getSub().getElem().getItem();
        } catch(NullPointerException ex) { }
        if(s==null) 
            System.out.println("null, you fool");            
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这个嵌入式注释的概念可行呢?或类似的东西?

dig*_*oel 7

ElementType指定注释的有效目标.你不能注释任何旧陈述.它基本上缩小到宣言; 声明:

  • 注释类型
  • constructor
  • 领域
  • 局部变量
  • 方法
  • 参数
  • 类型