From 267ccf6ec7c0d475a357c24a935b608804645776 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Mon, 9 Sep 2024 19:43:00 +0200 Subject: [PATCH] [java-decompiler] IDEA-346312 adapt patch 044 - clean-up - more tests GitOrigin-RevId: 0b33a70fbe9e01e38a56fd9311435bbaafcaa4f5 --- .../main/rels/NestedClassProcessor.java | 8 +- .../modules/decompiler/exps/VarExprent.java | 4 +- .../java/decompiler/SingleClassesTest.java | 1 + .../engine/testData/results/TestFinally.dec | 103 ++++++++++++++++++ .../engine/testData/src/pkg/TestFinally.java | 31 ++++++ 5 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 plugins/java-decompiler/engine/testData/results/TestFinally.dec create mode 100644 plugins/java-decompiler/engine/testData/src/pkg/TestFinally.java diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/main/rels/NestedClassProcessor.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/main/rels/NestedClassProcessor.java index c791d4bb92ce..c37b25c4a401 100644 --- a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/main/rels/NestedClassProcessor.java +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/main/rels/NestedClassProcessor.java @@ -922,14 +922,14 @@ public class NestedClassProcessor { case Exprent.EXPRENT_VAR -> { VarExprent varExpr = (VarExprent)expr; if (varExpr.isDefinition()) { - Stack stack = new Stack<>(); - stack.push(varExpr.getDefinitionType()); + List stack = new ArrayList<>(); + stack.add(varExpr.getDefinitionType()); while (!stack.isEmpty()) { - VarType varType = stack.pop(); + VarType varType = stack.remove(0); if (classType.equals(varType) || (varType != null && varType.getArrayDim() > 0 && classType.getValue().equals(varType.getValue()))) { res = true; } else if (varType != null && varType.isGeneric()) { - ((GenericType)varType).getArguments().forEach(stack::push); + stack.addAll(((GenericType)varType).getArguments()); } } } diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/modules/decompiler/exps/VarExprent.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/modules/decompiler/exps/VarExprent.java index 9184e6347cc2..8f1714047ae8 100644 --- a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/modules/decompiler/exps/VarExprent.java +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/modules/decompiler/exps/VarExprent.java @@ -7,6 +7,7 @@ import org.jetbrains.java.decompiler.main.ClassWriter; import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode; import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.collectors.BytecodeMappingTracer; +import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger; import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; import org.jetbrains.java.decompiler.main.rels.MethodWrapper; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; @@ -68,7 +69,8 @@ public class VarExprent extends Exprent { try { return GenericType.parse(lvt.getSignature()); } catch (StringIndexOutOfBoundsException ex) { - ex.printStackTrace(); + DecompilerContext.getLogger().writeMessage("Inconsistent data: ", + IFernflowerLogger.Severity.WARN, ex); } } else if (lvt != null) { diff --git a/plugins/java-decompiler/engine/test/org/jetbrains/java/decompiler/SingleClassesTest.java b/plugins/java-decompiler/engine/test/org/jetbrains/java/decompiler/SingleClassesTest.java index 0c4efa60104b..0eef3ef2ed9b 100644 --- a/plugins/java-decompiler/engine/test/org/jetbrains/java/decompiler/SingleClassesTest.java +++ b/plugins/java-decompiler/engine/test/org/jetbrains/java/decompiler/SingleClassesTest.java @@ -248,5 +248,6 @@ public class SingleClassesTest extends SingleClassesTestBase { @Test public void testPreserveAssignmentToRecord2() { doTest("pkg/PreserveAssignmentToRecord2"); } @Test public void testLambda() { doTest("pkg/TestLambda"); } @Test public void testCustomSyntheticRecords() { doTest("pkg/TestCustomSyntheticRecords"); } + @Test public void testFinally() { doTest("pkg/TestFinally"); } } diff --git a/plugins/java-decompiler/engine/testData/results/TestFinally.dec b/plugins/java-decompiler/engine/testData/results/TestFinally.dec new file mode 100644 index 000000000000..44023820b9f7 --- /dev/null +++ b/plugins/java-decompiler/engine/testData/results/TestFinally.dec @@ -0,0 +1,103 @@ +package pkg; + +import java.util.List; + +public class TestFinally { + public void test(List> a) { + try { + this.testThrow();// 13 + } catch (Exception var12) {// 15 + var12.printStackTrace();// 16 + } catch (Throwable var13) {// 17 + throw new RuntimeException(var13);// 18 + } finally { + for(A s : a) {// 21 + String a2 = s.toString();// 22 + System.out.println(a2);// 23 + } + + } + + }// 26 + + public void testThrow() { + }// 30 + + public class A { + public A(final TestFinally this$0) { + }// 8 + } +} + +class 'pkg/TestFinally' { + method 'test (Ljava/util/List;)V' { + 0 7 + 1 7 + 32 8 + 34 9 + 65 10 + 6e 11 + 71 13 + 72 13 + 73 13 + 74 13 + 75 13 + 76 13 + 77 13 + 78 13 + 83 13 + 84 13 + 85 13 + 86 13 + 87 13 + 88 13 + 89 13 + 8a 13 + 8b 13 + 8c 13 + 8d 13 + 8e 13 + 8f 14 + 90 14 + 91 14 + 92 14 + 93 14 + 94 14 + 95 14 + 96 15 + 97 15 + 98 15 + 99 15 + 9a 15 + 9b 15 + 9c 15 + 9d 15 + a4 20 + } + + method 'testThrow ()V' { + 0 23 + } +} + +class 'pkg/TestFinally$A' { + method ' (Lpkg/TestFinally;)V' { + 4 27 + } +} + +Lines mapping: +8 <-> 28 +13 <-> 8 +15 <-> 9 +16 <-> 10 +17 <-> 11 +18 <-> 12 +21 <-> 14 +22 <-> 15 +23 <-> 16 +26 <-> 21 +30 <-> 24 +Not mapped: +24 +25 diff --git a/plugins/java-decompiler/engine/testData/src/pkg/TestFinally.java b/plugins/java-decompiler/engine/testData/src/pkg/TestFinally.java new file mode 100644 index 000000000000..e53d38f5905f --- /dev/null +++ b/plugins/java-decompiler/engine/testData/src/pkg/TestFinally.java @@ -0,0 +1,31 @@ +package pkg; + + +import java.util.List; + +public class TestFinally { + + public class A{ + + } + public void test(List> a) { + try { + testThrow(); + + }catch (Exception e) { + e.printStackTrace(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + finally { + for (A s : a) { + String a2 = s.toString(); + System.out.println(a2); + } + } + } + + public void testThrow() { + + } +} \ No newline at end of file