eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Suspect code generation for "try-with-resource" functionality

Open
#934 9 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
compiler
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Generated java class and its output is different using eclipse with traditional Java

As per specification, In Throwable.addSuppressed() method, if exception is this throwable; a throwable cannot suppress itself in such scenario it should throw java.lang.IllegalArgumentException but the result is varies with using eclipse compiler.

https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#addSuppressed-java.lang.Throwable-

```
package test;

public class Try_with_Resource {
public static void main(String[] args) {
RuntimeException e = new RuntimeException("My Exception");
try (TestB A = new TestB(e)) {
throw e;
}
}
}

class TestB implements AutoCloseable {
RuntimeException e;
public TestB(RuntimeException e) {
this.e = e;
}

@Override
public void close() {
throw e;
}
}
```

**Output using regular java** (with java 7 version & above)

![image](https://user-images.githubusercontent.com/23518430/229134665-f85f06a1-07cd-4916-8e03-128aac3c8ce4.png)

**Output from eclipse**

![image](https://user-images.githubusercontent.com/23518430/229135191-0accbb3e-3eec-401a-8f7b-21ef43cd70eb.png)

**The bytecode of class generated by eclipse vs regular javac**

Using eclipse compiler

```
$ javap -c Try_with_Resource.class
Compiled from "Try_with_Resource.java"
public class test.Try_with_Resource {
public test.Try_with_Resource();
Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #16 // class java/lang/RuntimeException
3: dup
4: ldc #18 // String My Exception
6: invokespecial #20 // Method java/lang/RuntimeException."":(Ljava/lang/String;)V
9: astore_1
10: aconst_null
11: astore_2
12: aconst_null
13: astore_3
14: new #23 // class test/TestB
17: dup
18: aload_1
19: invokespecial #25 // Method test/TestB."":(Ljava/lang/RuntimeException;)V
22: astore 4
24: aload_1
25: athrow
26: astore_2
27: aload 4
29: ifnull 37
32: aload 4
34: invokevirtual #28 // Method test/TestB.close:()V
37: aload_2
38: athrow
39: astore_3
40: aload_2
41: ifnonnull 49
44: aload_3
45: astore_2
46: goto 59
49: aload_2
50: aload_3
51: if_acmpeq 59
54: aload_2
55: aload_3
56: invokevirtual #31 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
59: aload_2
60: athrow
Exception table:
from to target type
24 26 26 any
14 39 39 any
}
```

Using Traditional java compiler

```
$ javap -c test/Try_with_Resource
Compiled from "Try_with_Resource.java"
public class test.Try_with_Resource {
public test.Try_with_Resource();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #7 // class java/lang/RuntimeException
3: dup
4: ldc #9 // String My Exception
6: invokespecial #11 // Method java/lang/RuntimeException."":(Ljava/lang/String;)V
9: astore_1
10: new #14 // class test/TestB
13: dup
14: aload_1
15: invokespecial #16 // Method test/TestB."":(Ljava/lang/RuntimeException;)V
18: astore_2
19: aload_1
20: athrow
21: astore_3
22: aload_2
23: invokevirtual #21 // Method test/TestB.close:()V
26: goto 37
29: astore 4
31: aload_3
32: aload 4
34: invokevirtual #24 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
37: aload_3
38: athrow
Exception table:
from to target type
19 21 21 Class java/lang/Throwable
22 26 29 Class java/lang/Throwable
}
```

I am not sure it might be the reason, I could see generated “Exception table” in java class with eclipse compiler is different from java class generated by the traditional java compiler.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.