免责声明:我知道 @Deprecated 的含义和目的.
@Deprecated的定义注释在 Java 的源代码中看起来像这样:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
我理解目标值为 CONSTRUCTOR 的目的, FIELD , TYPE , METHOD和 PACKAGE .
但是,将方法参数或局部变量标记为@Deprecated是什么意思? ?
奇怪的是,下面的示例编译时没有任何警告。
interface Doable {
void doIt(@Deprecated Object input);
}
class Action implements Doable {
@Override
public void doIt(@Deprecated Object input) {
String string = String.valueOf(input); // no warning!
@Deprecated
String localVariable = "hello";
System.out.println("Am I using a deprecated variable?" + localVariable); // no warning!
}
}
这是他们将来可能打算实现的东西吗?
FWIW,我在 Ubuntu 12.04 64 位上使用 JDK 1.7.0_11。无论我从 Eclipse 还是命令行运行程序,结果都是一样的。
对于 @Deprecated 的正常使用,编译器会发出警告。 ,例如使用 java.util.Date 的弃用构造函数之一类(class)。只是为了证明我没有错误的终端或设置,这里是输出:
$ javac com/adarshr/Test.java -Xlint:deprecation
com/adarshr/Test.java:12: warning: [deprecation] Date(int,int,int) in Date has been deprecated
new Date(2013, 1, 31);
^
1 warning
$
$ javac -version
javac 1.7.0_11
最佳答案
What does it mean to mark a method parameter or a local variable as
@Deprecated?
它有the same meaning as when applied to any other element:
A program element annotated
@Deprecatedis one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
为什么编译器不忽略 Java 7 中不推荐使用的参数和字段的警告?
Because that's exactly what the JLS (§ 9.6.3.6) dictates.
A Java compiler must produce a deprecation warning when a type, method, field, or constructor whose declaration is annotated with the annotation
@Deprecatedis used (i.e. overridden, invoked, or referenced by name), unless:
The use is within an entity that is itself annotated with the annotation @Deprecated; or
The use is within an entity that is annotated to suppress the warning with the annotation
@SuppressWarnings("deprecation"); orThe use and declaration are both within the same outermost class.
Use of the
@Deprecatedannotation on a local variable declaration or on a parameter declaration has no effect.
(强调)
关于java - @Deprecated 对方法参数和局部变量意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627313/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput