Snailclimb / Snailclimb/JavaGuide

反射机制 --> Class.newInstance() && Constructor.newInstance()

未关闭
#1,606 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
JavaScript
星标
159k
派生
46.1k
平均合并
14 小时 36 分钟
30 天内合并 PR
6

描述

  1. JDK9文档中,Class.newInstance()方法被标识为Deprecated,同时也提到The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance()
  2. The Java™ Tutorials中提到Constructor.newInstance()是首选,原因为:
  • Class.newInstance() can only invoke the zero-argument constructor, while Constructor.newInstance() may invoke any constructor, regardless of the number of parameters.(Class.newInstance()方法只能调用无参数的构造方法,而Constructor.newInstance()方法可以调用任意参数数量的构造方法。)
  • Class.newInstance() throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. Constructor.newInstance() always wraps the thrown exception with an InvocationTargetException.(Class.newInstance()会抛出任何构造器抛出的受检查异常和不受检查异常;Constructor.newInstance()会将抛出的异常包装在InvocationTargetException中抛出。)
  • Class.newInstance() requires that the constructor be visible; Constructor.newInstance() may invoke private constructors under certain circumstances.(Class.newInstance()要求构造方法可见;而Constructor.newInstance()在某些情况下,可以调用私有的构造方法)

Source1:https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
Source2:https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读链接的 JDK 9 Class API 和 Java Tutorials 中关于构造函数反射的页面,然后在 JavaGuide 中定位相关的反射文档。更新指南,以说明 Class.newInstance() 已被弃用以及其与 Constructor.newInstance() 之间所述的差异,同时保留引用的来源。

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
documentation
Issue 类型
文档
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。