博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【我的Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?...
阅读量:7005 次
发布时间:2019-06-28

本文共 3562 字,大约阅读时间需要 11 分钟。

1、背景

最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作。因此编写了自定义的Lint规则,在编写自定义的Lint规则前,当然是需要去把Google的关于Lint检测的源代码看一遍学习学习如何编写自定义规则。

google官方的lint源代码链接为:

我的github上备份了该源代码:

在看源代码的过程中,我发现一些奇怪的注释//$NON-NLS-1$,这个注释到底是什么意思呢?

/** Using a view inflater unconditionally in an AdapterView */    public static final Issue ISSUE = Issue.create(            "ViewHolder", //$NON-NLS-1$            "View Holder Candidates",            "When implementing a view Adapter, you should avoid unconditionally inflating a " +            "new layout; if an available item is passed in for reuse, you should try to " +            "use that one instead. This helps make for example ListView scrolling much " +            "smoother.",            Category.PERFORMANCE,            5,            Severity.WARNING,            IMPLEMENTATION)            .addMoreInfo(            "http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder");    private static final String GET_VIEW = "getView";  //$NON-NLS-1$    static final String INFLATE = "inflate";           //$NON-NLS-1$

这里写图片描述

除了上面的//$NON-NLS-1$注释之外,还有类似于 //$NON-NLS-2$//$NON-NLS-3$的注释,如下所示:

/* (non-Javadoc)     * @see javax.lang.model.util.Types#contains(javax.lang.model.type.TypeMirror, javax.lang.model.type.TypeMirror)     */    @Override    public boolean contains(TypeMirror t1, TypeMirror t2) {        switch(t1.getKind()) {            case EXECUTABLE :            case PACKAGE :                throw new IllegalArgumentException("Executable and package are illegal argument for Types.contains(..)"); //$NON-NLS-1$            default:                break;        }        switch(t2.getKind()) {            case EXECUTABLE :            case PACKAGE :                throw new IllegalArgumentException("Executable and package are illegal argument for Types.contains(..)"); //$NON-NLS-1$            default:                break;        }        throw new UnsupportedOperationException("NYI: TypesImpl.contains(" + t1 + ", " + t2 + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$    }

这里写图片描述

这里写图片描述

反正看了下代码,基本上所有的使用字符串的地方,后面都会接上这么一个注释,//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$。那么 这个 //$NON-NLS-1$ 到底是什么意思呢?

2、//$NON-NLS-1$的意义

参考了网上的资料,

查看了stackoverflow上的一个提问,下面是链接:

这里写图片描述

原文解释如下:

They silence a warning that Eclipse emits when it encounters string literals (and has been configured to complain).

The idea is that UI messages should not be embedded as string literals, but rather sourced from a resource file (so that they can be translated, proofed, etc). Consequently, Eclipse can be configured to detect string literals, so that you don’t accidentally have leave unexternalized UI strings in the code; however, there are strings which should not be externalized (such as regexps) and so, //NONNLS1 gives you a way to communicate that fact to the compiler.

使用Google翻译内容如下:

当它遇到字符串文字(并已被配置为抱怨)时,它们会使Eclipse发出警告。

这个想法是,UI消息不应该嵌入字符串文字,而是从资源文件中获取(以便可以翻译,校对等)。因此,Eclipse可以配置为检测字符串文字,这样您就不会意外地在代码中留下无用的UI字符串;但是,有些字符串不应该被外部化(如regexps),所以// $ NON-NLS-1 $给你一个方法来将该事实传递给编译器。

查看链接:

里面有一段注释对// $ NON-NLS-1 $进行了描述,如下所示:

这里写图片描述

描述内容为:

The string $NON-NLS-1$ is a hint for both the compiler =

and the=20
Externalization wizard that the first character string on this line is a =
tag or=20
keyword of some sort and should not be localized.

大概含义就是:

$NON-NLS-1$表明本行的第一个string型变量是一个标签或者关键字,不需要被本地化。

参考链接:

里面举了一个例子,如下所示:

这里写图片描述

总结一下:使用了//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$的含义就是告诉Eclipse等IDE软件,该字符串不需要被本地化操作。

3、参考链接


这里写图片描述

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:

如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作!

这里写图片描述

转载于:https://www.cnblogs.com/ouyangpeng/p/8537628.html

你可能感兴趣的文章
11 条重要的数据库设计规则
查看>>
Snapshot Volume 操作 - 每天5分钟玩转 OpenStack(58)
查看>>
两种进入容器的方法 - 每天5分钟玩转 Docker 容器技术(23)
查看>>
Mybatis JPA mini
查看>>
CSS书写规范
查看>>
centos5.3搭建安全高效的LNMP服务器
查看>>
各种光线收发器接口一览
查看>>
7月共处理钓鱼网站6903个:非CN域名达6744个
查看>>
TCP/IP 三次握手 四次断开
查看>>
AR-关于应收账款坏账处理
查看>>
一切都在哲学中
查看>>
我的友情链接
查看>>
PDF Creator v5.0 支持手势和触屏
查看>>
make gridView's cell as square
查看>>
js返回相对时间
查看>>
Spring中 @Autowired注解与@Resource注解的区别
查看>>
你必须知道的.NET:内存分配
查看>>
struct.error: 'h' format requires -32768 number 32767
查看>>
nginx报Could not build the server_names_hash,server_names_hash_bucket_size:32错
查看>>
linux中的计划任务
查看>>