所以,我使用 IntelliJ IDEA 在 Java 中进行编程,并且我正在试验关键字 instanceof,我的代码最终看起来像这样:
public class Main {
public static void main(String args[])
{
One one = new One();
One two = new Two();
if (one instanceof Two)
{
System.out.println(one);
}
if (two instanceof Two)
{
System.out.println(one);
}
}
}
class One { }
class Two extends One { }
IntelliJ 在 two instanceof Two 行提示“[...] 总是正确的”,但是对于 one instanceof Two IntelliJ 没有给我一个“[...] 总是错误的”提示。有谁知道为什么吗?
最佳答案
更新:已在 IDEA 2018.3 中修复。
(免责声明:IntelliJ IDEA 开发人员在此,负责此功能)。
简短的回答:因为它没有实现。
当我们在数据流分析中跟踪变量的实际类型时,我们使用 TypeConstraint 描述的模型类(class)。它允许我们跟踪两种事实:1)如果变量实际类型是 instanceof something 和 2)如果变量实际类型不是 instanceof something。有了这些事实,我们可以在许多情况下推断出总是真/总是假的 instanceof 例如:
void test(Object foo) {
if (foo instanceof String) {
if (foo instanceof Integer) {
// always false: "instanceof String" fact is not compatible
// with "instanceof Integer"
}
}
}
或者
void test(Object foo) {
if (!(foo instanceof Number)) {
if (foo instanceof Integer) {
// always false: "not instanceof Number" fact is not compatible
// with "instanceof Integer"
}
}
}
但是对于你的情况,这个模型是不够的。我们需要扩展它来跟踪变量的确切类型。在您的代码中,我们跟踪 one 是 instanceof One(与 instanceof Two 事实兼容),尽管 new 表达式,我们可以知道更精确的类型信息,即 one 是 exactly One。这并不经常使用,因为在大多数情况下(变量是方法参数,变量从方法返回,变量从字段赋值,数组元素,强制转换表达式等)我们无法知道类型是精确类型还是子类型,所以目前的模型是完全令人满意的。我可以想象只有两种情况 exactly One 事实跟踪是有用的:new 表达式(就像你的情况一样)和比较之后像 obj.getClass() = = Xyz.class.
我认为,这是一个可以合理实现的功能。我已经考虑过了,但因为除了我之外还有其他人也很关心,所以我提交了an issue , 这样您就可以跟踪它。
关于java - IntelliJ 显示 "always true"提示但不显示 "always false"的 instanceof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225186/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用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
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que