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 に答える