7

允许 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");
    }
}

缩写经常写的(太频繁了,绝对!):

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");            
    }
}

如果这个嵌入注释的概念完全可行?或者类似的东西?

4

1 回答 1

8

ElementType specifies the valid targets of an annotation. You can't annotate any old statement. It's basically narrowed down to declarations; declarations of:

  • annotation type
  • constructor
  • field
  • local variable
  • method
  • package
  • parameter
  • type
于 2013-06-05T20:51:12.993 回答