Java 标准库中的无副作用方法

发布于 2024-09-07 02:37:29 字数 273 浏览 10 评论 0原文

我正在对 Java 程序进行分析,该程序需要有关方法调用的副作用信息。对于标准库类,我想编译带有只读参数的方法列表。也就是说,方法不会修改其参数或可从其参数访问的任何内容。我可以从 javadoc 中推断出很多信息,但这需要时间。

有人可以建议一个更简单的参考或方法来确定标准方法调用是否修改其参数吗?通读每个 javadoc 条目真的是最好的方法吗?

谢谢!

编辑: 一个好处是识别对对象也没有副作用的方法。例如, stack.pop() 会,而 stack.size() 不会。

I'm working on an analysis for Java programs that requires side-effect information about method calls. For standard library classes, I would like to compile a list of methods with read-only parameters. That is, methods that don't modify their arguments, or anything reachable from their arguments. I can infer a lot from the javadocs, but that will take time.

Could anyone suggest an easier reference or method for determining whether standard method calls modify their arguments? Is reading through the each javadoc entry really the best way?

Thanks!

EDIT:
A bonus would be identifying methods that have no side-effects on the object either. For instance, stack.pop() would whereas stack.size() would not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-09-14 02:37:29

好吧,所有仅使用原始类型/字符串/对象/泛型类型作为参数的方法应该让您满意,无需进一步考虑。对于 java.lang 和 java.util 这应该涵盖大部分方法。

但是你最好限制你想要处理的包,因为标准 jdk 为所有任务和目的提供了巨大的类库。

编辑
对于声明为 E extends ModifyingObject 的泛型类型来说,它有点模糊,所以请亲自查看。

Well, all methods taking only primitive types/strings/Object/generic types as parameters should satisfy you without further consideration. And for java.lang and java.util this should cover most of the methods.

But you'd really better to limit packages you want to process, because standard jdk offers huge library of classes for all tasks and purposes.

edit
It's somewhat fuzzier for generic types declared as E extends ModifiableObject, so see for yourself.

甚是思念 2024-09-14 02:37:29

您可以尝试针对 JDK 的源代码运行类型推断引擎。

也许这篇论文Java 的类型限定符推断可能有用。 (不过网上好像没有全文)

You might try running a type inference engine against the source code of the JDK.

Perhaps the paper Type qualifier inference for Java may be of use. (Full text does not appear to be online though)

红墙和绿瓦 2024-09-14 02:37:29

我们的DMS 软件再工程工具包是一个通用的可定制程序分析和转换工具。它有一个 Java 前端,用于解析 Java 并生成符号表、类继承关系、控制和数据流信息。

根据该信息,可以计算关于方法 M 是否直接修改参数或从参数可到达的任何内容的本地信息。可以构造一个调用图,以及由 M 直接或间接调用的方法 X 修改的任何内容。这实际上就是您的答案。您必须将其应用到感兴趣的代码源,在您的例子中,是 Java 标准库。

即使提供了所有提供的信息,配置 DMS 来执行此操作也并非易事。 OTOH,这个答案将非常准确(模保守假设和反思),可重复,并且易于应用于您选择的任何方法。通过 Javadocs 手动执行此操作可能非常耗时且容易出错。

Our DMS Software Reengineering Toolkit is a general purpose customizable program analysis and transformation tool. It has a Java Front End that parses Java and produces symbol tables, class inheritance relations, control and data flow information.

From this information, local information about whether a method M directly modifies an argument or anything reachable from an argument can be computed. A call graph can be constructed, and anything modified by a method X called directly or indirectly by M. That's in effect your answer. You'd have to apply this to the source of the code of interest, in your case, the Java Standard library.

Configuring DMS to do this isn't trivial even with all the supplied information. OTOH, this answer is going be pretty accurate (modulo conservative assumptions and reflection), repeatable, and easy to apply to any method you choose. Doing this by hand via Javadocs is likely to be extremely time consuming and error prone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文