ClassCastException 是由 Just In Time 中的错误引起的吗?

发布于 2024-08-24 13:50:38 字数 2384 浏览 11 评论 0原文

鉴于这段代码:(

public static void writeFile(File file,List buffer)throws IOException{
    File fic = new File(file.getCanonicalPath());
    cat.debug("writing file : "+fic.getAbsolutePath());
    FileOutputStream out = new FileOutputStream(fic);
    PrintStream ps = new PrintStream(out);
    for(int i=0;i<buffer.size();i++){
        ps.println(buffer.get(i));
    }
    ps.flush();
    ps.close();
    out.close();
}

请不要提供有关如何安全关闭流的建议,这是遗留代码,新版本使用了 try / finally)

我在“ps.println(buffer.get(i))”处得到了 ClassCastException "

该方法被调用多次(比如 5 次),列表中仅填充了字符串 然后,使用一个充满 String 和另一个对象(例如 ErrorObject)的列表来调用它 当我们到达第一个 ErrorObject 时,我们得到 ClassCastException。

com.mycompany.ErrorObject incompatible with java.lang.String

该问题在生产环境中出现,但在开发环境中无法重现: 产品:jvm=IBM J9 VM 2.4 J2RE 1.6.0 IBM J9 2.4 AIX ppc-32 jvmap3260-20081105_25433(启用 JIT,启用 AOT) 开发人员:WinXP、JDK 1.6.0_16

此代码是否有任何原因可能失败?

它最近被修补了,我担心生产团队没有正确升级 jar,但我的老板已经检查了补丁是否正确应用......

我想知道即时编译器是否可以将 ps.println“连接”到ps.println(String) 而不是 ps.println(Object)。 这可以解释这样的问题,但我不知道这是否可能。

欢迎任何建议,提前谢谢您

编辑: 我被问到完整的堆栈跟踪,所以这里是:

java.lang.ClassCastException: com.mycompany.util.ErrorObject incompatible with java.lang.String
    at com.mycompany.util.FileUtils.writeFile(FileUtils.java:91)
    at com.mycompany.util.FileUtils.writeFile(FileUtils.java:50)
    at com.mycompany.itools.task.DBCompareInits.doDBTask(DBCompareInits.java:959)
    at com.mycompany.itools.task.DBTask.doTask(DBTask.java:115)
    at com.mycompany.itools.task.TaskGroup.startGroup(TaskGroup.java:115)
    at com.mycompany.runner.Runner.main(Runner.java:209)

编辑2: javap-c

   65:  invokeinterface #20,  1; //InterfaceMethod java/util/List.size:()I
   70:  if_icmpge   92
   73:  aload   4
   75:  aload_1
   76:  iload   5
   78:  invokeinterface #21,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
   83:  invokevirtual   #31; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
   86:  iinc    5, 1
   89:  goto    62
   92:  aload   4
   94:  invokevirtual   #32; //Method java/io/PrintStream.flush:()V
   97:  aload   4
   99:  invokevirtual   #33; //Method java/io/PrintStream.close:()V
   102: aload_3
   103: invokevirtual   #28; //Method java/io/FileOutputStream.close:()V

Given this piece of code :

public static void writeFile(File file,List buffer)throws IOException{
    File fic = new File(file.getCanonicalPath());
    cat.debug("writing file : "+fic.getAbsolutePath());
    FileOutputStream out = new FileOutputStream(fic);
    PrintStream ps = new PrintStream(out);
    for(int i=0;i<buffer.size();i++){
        ps.println(buffer.get(i));
    }
    ps.flush();
    ps.close();
    out.close();
}

(please no advice about how to close streams safely, this is legacy code and the new version makes use of try / finally)

I get a ClassCastException at the "ps.println(buffer.get(i))"

this method is called several times (say 5 times) with a list only filled with Strings
then, it is called with a list filled with String and an other object (say ErrorObject)
At the point we reach the 1st ErrorObject, we get the ClassCastException.

com.mycompany.ErrorObject incompatible with java.lang.String

This problem occurs in production environment, but cannot be reproduced in Dev environment :
Prod : jvm=IBM J9 VM 2.4 J2RE 1.6.0 IBM J9 2.4 AIX ppc-32 jvmap3260-20081105_25433 (JIT enabled, AOT enabled)
Dev : WinXP, JDK 1.6.0_16

Is there any reason this code could fail ?

It was patched recently, i fear that the production team did not upgrade the jar correctly, but my boss already checked that the patch was applied correclty ...

I was wondering if the Just in Time compiler could "wire" the ps.println to the ps.println(String) instead of the ps.println(Object).
That could explain such a problem, but I have no idea if this possible.

Any advices welcome, thank you in advance

EDIT :
i was asked full stack trace so here it is :

java.lang.ClassCastException: com.mycompany.util.ErrorObject incompatible with java.lang.String
    at com.mycompany.util.FileUtils.writeFile(FileUtils.java:91)
    at com.mycompany.util.FileUtils.writeFile(FileUtils.java:50)
    at com.mycompany.itools.task.DBCompareInits.doDBTask(DBCompareInits.java:959)
    at com.mycompany.itools.task.DBTask.doTask(DBTask.java:115)
    at com.mycompany.itools.task.TaskGroup.startGroup(TaskGroup.java:115)
    at com.mycompany.runner.Runner.main(Runner.java:209)

EDIT 2 :
javap -c

   65:  invokeinterface #20,  1; //InterfaceMethod java/util/List.size:()I
   70:  if_icmpge   92
   73:  aload   4
   75:  aload_1
   76:  iload   5
   78:  invokeinterface #21,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
   83:  invokevirtual   #31; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V
   86:  iinc    5, 1
   89:  goto    62
   92:  aload   4
   94:  invokevirtual   #32; //Method java/io/PrintStream.flush:()V
   97:  aload   4
   99:  invokevirtual   #33; //Method java/io/PrintStream.close:()V
   102: aload_3
   103: invokevirtual   #28; //Method java/io/FileOutputStream.close:()V

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

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

发布评论

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

评论(2

一张白纸 2024-08-31 13:50:38

我想知道即时编译器是否可以将 ps.println “连接”到 ps.println(String) 而不是 ps.println(Object)。这可以解释这样的问题,但我不知道这是否可能。

这是不可能的。或者至少不会,除非存在字节码编译器或 JIT 编译器错误。如果你有无可辩驳的证据表明情况确实如此,你只应该责怪编译器错误。

然而,我要检查的第一件事是正在执行的代码确实是从您正在查看的源代码编译而来的。确认这一点的一种方法是从源代码重新编译,然后比较在类的各个副本上运行 javap 的结果。查看字节码还会告诉您字节码编译器要使用哪个 println 重载。

编辑 - javap 输出清楚地表明该版本的字节码应该调用 println(Object),并且没有 checkcast< /code> 操作码在眼前。 JIT 编译器错误调用错误的方法并自发插入代码来执行类转换,这听起来越来越难以置信。

I was wondering if the Just in Time compiler could "wire" the ps.println to the ps.println(String) instead of the ps.println(Object). That could explain such a problem, but I have no idea if this possible.

It is not possible. Or at least not unless there is a bytecode compiler or JIT compiler bug. And you should only blame compiler bugs if you have irrefutable evidence that this is so.

However, the first thing I would check is that the code being executed was really compiled from the source code that you are looking at. One way to confirm this would be to recompile from source, and then compare the results of running javap on respective copies of the class. Looking at the bytecodes will also tell you which overload of println the bytecode compiler is saying to use.

EDIT - the javap output clearly shows that that version of the bytecode should call println(Object), and there is no checkcast opcode in sight. A JIT compiler bug that calls the wrong method and spontaneously inserts code to do a classcast is sounding more and more implausible.

带上头具痛哭 2024-08-31 13:50:38

在 ErrorObject 类中声明 toString() 方法并转换为字符串,并将 +"" 添加到 println() 调用中。就像 pintln(errorObjList+"");

Declare toString() method with converting to string inside in ErrorObject class and add +"" to println() call. Like pintln(errorObjList+"");

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