【问题标题】:java.lang.UnsupportedOperationException: RectangleReadOnly: this Rectangle is read only while creating Rectangle objectjava.lang.UnsupportedOperationException: RectangleReadOnly: 这个 Rectangle 在创建 Rectangle 对象时是只读的
【发布时间】:2017-03-23 05:45:46
【问题描述】:

我得到 java.lang.UnsupportedOperationException: RectangleReadOnly: this Rectangle is read only。 当我尝试创建一个 Rectangle 对象然后旋转它时出现异常。 使用的jar文件是-com.lowagie.text-2.1.7.jar

Rectangle pg = PageSize.getRectangle("LETTER");
    if (isLandscape) pg = pg.rotate(); (exception coming on this line)
    if (!TextOp.isEmpty(pageBGColor)) {
      pg.setBackgroundColor(PDFUtil.getColor(pageBGColor));
    }
    document_ =  new Document(pg);

【问题讨论】:

标签: java itext unsupportedoperation


【解决方案1】:

当你这样做时

Rectangle pg = PageSize.getRectangle("LETTER");

根据a search on GitHub for RectangleReadOnly,它返回对预定义static final Rectangle 的引用,该引用由RectangleReadOnly 类的对象实现。这是Rectangle 的特化,它强制执行其自身的不变性,因此您无法修改您可以要求的“常量”命名矩形。

试试吧

Rectangle pg = new Rectangle(PageSize.getRectangle("LETTER"));

改为创建不可变 Rectangle 对象的可变克隆。

可以说这是库中的一个错误。它应该为你做对象克隆。

【讨论】:

  • 静态 final 只会阻止从类中更改引用的内容,它不会阻止他正在做的事情形成我所知道的。我猜想在库中该对象被声明为不可修改。我实际上用那个 jar 运行了相当于他的代码并且没有抛出异常。不知道那里发生了什么。
  • 亲爱的 Jim Garrison,我尝试了您建议的解决方案,但没有成功。还是一样的例外。矩形 pg = PageSize.getRectangle("LETTER");和矩形 pg = new Rectangle(PageSize.getRectangle("LETTER"));正在做同样的事情。不知道为什么它不让改变对象。
  • 您的 grepcode 链接在 itext 5.1.2 中显示了 PageSize 的来源。 op表示他使用的是itext 2.1.7。您是否检查过这些版本之间的类是否发生了变化?
  • 您链接的 grepcode 网站不是最新的,它不包含 iText 5.5.7 到 5.5.11。 Grepcode 上也缺少 iText 7。我向 info@grepcode.com 发送了一封电子邮件通知他们。
  • 除了链接到 GrepCode,您还可以只链接到 GitHub 上的实际源代码。我希望你不介意我会更新你的答案?
猜你喜欢
  • 1970-01-01
  • 2014-12-12
  • 2014-01-14
  • 2019-09-27
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多