java - Eclipse 直到运行时才报告错误

标签 java eclipse eclipse-plugin

我试图做一些困惑的技巧来在 IMarker 中携带 ProblemDescriptor 对象。

ProblemDescriptor 问题 = new ProblemDescriptor(...我的论点...); marker.setAttribute("PROBLEM_DESCRIPTOR", 问题);

后来我想检查该属性是否已设置,并利用其中的信息。

开发时不会报告错误,但在运行时我收到错误:

属性值类型为 com.localhost.problems.ProblemDescription,预期为 java.lang.String、Boolean、Integer 之一

这是一个特殊情况/该代码中的错误实现,还是我应该使用仅在运行时报告的此类错误?

最佳答案

您使用的setAttribute方法是

public void setAttribute(String attributeName, Object value)

由于第二个参数是Object,因此无法在编译时检查正确性,但是 Javadoc 明确指出:

Sets the attribute with the given name. The value must be null or an instance of one of the following classes: String, Integer, or Boolean. If the value is null, the attribute is considered to be undefined.

The attribute value cannot be a String whose UTF encoding exceeds 65535 bytes. On persistent markers this limit is enforced by an assertion.

另一种设计是用带有 StringIntegerBoolean 第二个参数的 3 个方法来替换它,但 API 设计者决定不要那样做。

关于java - Eclipse 直到运行时才报告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47268529/

相关文章:

java - 为什么jvm不能在树莓派上写入gpio?

java - 这是使用 Java 2D 图形 API 的正确方法吗?

没有更新站点的 Eclipse?

eclipse - 寻找 Spring Bean 引用资料?

java - Eclipse API : How to check whether an IFile is a . 项目或 .classpath 文件?

android - 我的 ADT 插件已更新,但 eclipse 无法识别它

java - 为什么 Event.fireAsync() 需要 @ObservesAsync 注释?

java - 使用 Spring 自定义唯一用户名验证

eclipse - 为什么我不能在eclipse中设置断点?

java - Eclipse Thym 需要 'osgi.bundle; org.eclipse.jgit [3.0.0,5.0.0)' 但找不到