【个人Android进阶之旅】Android 源代码中的Java代码中//$NON-NLS-1$ 注释是什么意思?

一、背景

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

google官方的lint源代码连接为:
https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks?spm=5176.100239.blogcont6918.10.MXIr5Jhtml

个人github上备份了该源代码:
https://github.com/ouyangpeng/android-lint-checksjava

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

/** 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$的注释,以下所示:git

/* (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$ 究竟是什么意思呢?github

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

参考了网上的资料,
查看了stackoverflow上的一个提问,下面是连接:微信

https://stackoverflow.com/questions/654037/what-does-non-nls-1-meanmarkdown

这里写图片描述

原文解释以下:eclipse

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

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 $给你一个方法来将该事实传递给编译器。

查看连接:
http://www.eeworm.com/read/185704/8990379/3/

里面有一段注释对// $ 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型变量是一个标签或者关键字,不须要被本地化。

参考连接:
http://www.eclipse.org/articles/Article-Internationalization/how2I18n.html

里面举了一个例子,以下所示:
这里写图片描述

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

三、参考连接


这里写图片描述

做者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/77941890

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

这里写图片描述

相关文章
相关标签/搜索