简体   繁体   English

如何在项目中搜索返回 Collection 接口实现的所有方法?

[英]How to search for all methods in a project that return implementation of Collection interface?

I've been reading Josh Bloch's 'Effective Java 2nd Edition'.我一直在阅读 Josh Bloch 的“有效 Java 第 2 版”。 Item 43 states 'Return empty arrays or collections, not nulls'.项目 43 状态“返回空的 arrays 或 collections,而不是空值”。 My question is how can I search for all methods in a project that return an implementation of java.util.Collection interface?我的问题是如何搜索项目中返回 java.util.Collection 接口实现的所有方法? IDE that is used is Eclipse, but any way of finding the right result is acceptable, eg regex or some other IDE.使用的 IDE 是 Eclipse,但任何找到正确结果的方法都是可以接受的,例如正则表达式或其他一些 IDE。

Found this in Eclipse Help:在 Eclipse 帮助中找到了这个:

To search for methods with a specific return type, use "* " as follows:要搜索具有特定返回类型的方法,请使用“*”,如下所示:

  1. Open the search dialog and click on the Java Search tab.打开搜索对话框并单击 Java 搜索选项卡。
  2. Type '*' and the return type, separated by a space, in the Search string.在搜索字符串中键入“*”和返回类型,以空格分隔。
  3. Select the Case sensitive checkbox. Select 区分大小写复选框。
  4. Select Method and Declarations and then click Search. Select 方法和声明,然后单击搜索。

It can help finding all methods that return specific types but not implementations of some interface.它可以帮助查找返回特定类型但不返回某些接口的实现的所有方法。

Thanks for mentioning SemmleCode.感谢您提及 SemmleCode。 To find all methods in the source that return a subtype of java.util.Collection, you write:要查找源中返回 java.util.Collection 子类型的所有方法,请编写:

import default

class CollectionType extends RefType {
  CollectionType() {
    this.getASupertype*().hasQualifiedName("java.util","Collection")
  }
}

from Method m
where m.getType() instanceof CollectionType
      and
      m.fromSource()
select m

That is, we first define what we mean by a CollectionType: all types that have java.util.Collection as a supertype.也就是说,我们首先定义 CollectionType 的含义:所有具有 java.util.Collection 作为超类型的类型。 There's a star behind getASupertype because we want to apply that operation zero or more times. getASupertype 后面有一颗星,因为我们想应用该操作零次或多次。 Next we just select those methods whose return type is such a CollectionType and which occur in the source.接下来我们只是 select 那些返回类型是这样一个 CollectionType 并且出现在源中的方法。

Go on, try it out:-) It's an easy exercise to further refine the query to those methods that are supposed to return a CollectionType but may return null. Go on,试试看:-) 这是一个简单的练习,可以将查询进一步细化为那些应该返回 CollectionType 但可能返回 null 的方法。 All this can be done interactively, with auto-completion while you develop the query, and continuous checking to help you get it right.所有这些都可以交互完成,在您开发查询时自动完成,并持续检查以帮助您正确完成。 You can view the results in many different ways and navigate between analysis results and code with ease.您可以通过多种不同方式查看结果,并轻松在分析结果和代码之间导航。

-Oege [disclosure: I'm the CEO of Semmle] -Oege [披露:我是 Semmle 的首席执行官]

In IntelliJ IDEA invoke Structural Search, choose existing template "methods of the class".在 IntelliJ IDEA 调用结构搜索中,选择现有模板“类的方法”。 The action will add following template:该操作将添加以下模板:

class $Class$ { 
  $ReturnType$ $MethodName$($ParameterType$ $Parameter$);
}

Set text constraint for "ReturnType" variable (Edit variables action) to be java.util.Collection, specify "Apply constraint within hierarchy" to search for descendants.将“ReturnType”变量(编辑变量操作)的文本约束设置为 java.util.Collection,指定“在层次结构内应用约束”以搜索后代。 For "MethodName" variable specify that it is target of the search.对于“MethodName”变量,指定它是搜索的目标。 That start searching, enjoy:)那开始搜索,享受:)

I don't know of a way to find all methods whose return type is any arbitrary implementation of java.util.Collection (including ones you may define yourself).我不知道如何找到返回类型为 java.util.Collection 的任意实现的所有方法(包括您可以自己定义的方法)。

If you can accept solutions that are a bit more limited, then a few possibilities come to mind:如果您可以接受更有限的解决方案,那么会想到一些可能性:

  1. Go to any class that imports java.util.Collection, double-click on "Collection", right-click on "Collection", and select "References > Project". Go to any class that imports java.util.Collection, double-click on "Collection", right-click on "Collection", and select "References > Project".

  2. Likewise for any particular collection type of interest.对于任何特定的感兴趣的集合类型也是如此。

  3. Go to any method returning a collection type, double-click on its return type, and search for that type as above. Go 到任何返回集合类型的方法,双击它的返回类型,然后像上面那样搜索该类型。

  4. Go to any class that imports any java.util. Go 到导入任何 java.util 的任何 class。 anything , drag across "java.util."任何东西,拖过“java.util”。 omitting the class following that package prefix , and right-click-search for project references as above.在 package 前缀之后省略 class ,然后右键单击搜索上述项目引用。

For all of the above, the "Search" view will show you an expandable outline of the places in the project where the selected item occurs.对于上述所有内容,“搜索”视图将向您显示项目中出现所选项目的位置的可展开轮廓。 You can traverse that outline (using either the keyboard -- with enter to select -- or the mouse -- clicking to select) to examine places those uses.您可以遍历该轮廓(使用键盘 - 输入 select - 或鼠标 - 单击以选择)以检查这些使用的位置。 I don't see a way to limit the viewed references to "method return type", but at least you'll be able to move quickly to relevant places in the code to eyeball for the specific usage type of interest.我看不到将查看的引用限制为“方法返回类型”的方法,但至少您可以快速移动到代码中的相关位置,以关注感兴趣的特定使用类型。

(With all these, you can click on the small downward-pointing triangle in the top of the "Search" tab and filter on the type of usage to be included in the display, such as excluding import statements.) (有了所有这些,您可以单击“搜索”选项卡顶部的小向下三角形并过滤要包含在显示中的使用类型,例如排除导入语句。)

Plugin time.插件时间。 I whipped up something that does most of the work...我做了一个可以完成大部分工作的东西......

Grab抓住

http://javadude.com/misc/collectionmarker.zip http://javadude.com/misc/collectionmarker.zip

http://javadude.com/misc/collectionmarker-source.zip (if interested) http://javadude.com/misc/collectionmarker-source.zip (如果有兴趣)

Unzip collectionmarker.zip into your eclipse dir (your eclipse dir should contain a plugins dir)将 collectionmarker.zip 解压缩到 eclipse 目录中(您的 eclipse 目录应该包含 plugins 目录)

Restart eclipse重启 eclipse

There will be two little eclipse icons on the toolbar - one clears markers, one adds them.工具栏上会有两个小 eclipse 图标 - 一个清除标记,一个添加它们。

This plugin searches everything in your workspace;该插件搜索您工作区中的所有内容; it ignores selections.它忽略选择。 (It's quick n dirty) (它又快又脏)

Hope this helps!希望这可以帮助! -- Scott ——斯科特

If there is no way to do this with the existing user interface, you can always use reflection to walk through the code.如果现有的用户界面没有办法做到这一点,您总是可以使用反射来遍历代码。 It is not that much work, a couple of nested loops.这不是很多工作,几个嵌套循环。

intellij IDEA has structural/expression based searches,so by searching for all methods that return Collection, you can at least get something close. intellij IDEA 具有基于结构/表达式的搜索,因此通过搜索所有返回 Collection 的方法,您至少可以得到一些接近的结果。

PMD can check for this, as can SemmleCode , if your IDE does not support structural search.如果您的 IDE 不支持结构搜索, PMD可以检查这一点, SemmleCode也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM