我试图做一些困惑的技巧来在 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.
另一种设计是用带有 String
、Integer
和 Boolean
第二个参数的 3 个方法来替换它,但 API 设计者决定不要那样做。
关于java - Eclipse 直到运行时才报告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47268529/