我可以在方法体内使用注释吗? [英] Can I use an annotation inside a method body?

查看:136
本文介绍了我可以在方法体内使用注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Allows the semantics of Java annotations to place them somewhere inside a functions body, e.g. to annotate a specific function call, statement or expression?

例如:

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

缩写为经常写的(定义得太频繁了! ):

To abbreviate the often written (too often, definitly!):

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

如果该嵌入注释的概念可能吗?还是类似的东西?

If the concept of this embedded annotation possible at all? Or something similar?

推荐答案

ElementType 指定注释的有效目标。您无法注释任何旧的声明。基本上只限于声明;声明:

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


  • 注释类型

  • 构造函数

  • 字段

  • 局部变量

  • 方法

  • package

  • 参数

  • 类型

  • annotation type
  • constructor
  • field
  • local variable
  • method
  • package
  • parameter
  • type

这篇关于我可以在方法体内使用注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆