From 4806937feb070b114b05e800dcb57726951adc8a Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Sat, 10 Aug 2024 14:11:35 +0200 Subject: [PATCH] feat: add throw instruction --- .../diff/visitor/ClassFileVisitor.java | 6 +++ .../diff/visitor/ClassFileVisitorTest.java | 18 ++++++++- src/test/resources/NEQ1/2/README.md | 2 + .../BaseNCodecOutputStream.class | Bin 0 -> 2644 bytes .../BaseNCodecOutputStream.class | Bin 0 -> 2640 bytes src/test/resources/NEQ1/2/diffoscope.diff | 37 ++++++++++++++++++ src/test/resources/NEQ1/2/gumtree.diff | 5 +++ 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/NEQ1/2/README.md create mode 100644 src/test/resources/NEQ1/2/commons-codec-1.15/BaseNCodecOutputStream.class create mode 100644 src/test/resources/NEQ1/2/commons-codec-1.16.0/BaseNCodecOutputStream.class create mode 100644 src/test/resources/NEQ1/2/diffoscope.diff create mode 100644 src/test/resources/NEQ1/2/gumtree.diff diff --git a/src/main/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitor.java b/src/main/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitor.java index 896e2c3284..78954ad9ae 100644 --- a/src/main/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitor.java +++ b/src/main/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitor.java @@ -25,6 +25,7 @@ import java.lang.classfile.instruction.ReturnInstruction; import java.lang.classfile.instruction.StackInstruction; import java.lang.classfile.instruction.StoreInstruction; +import java.lang.classfile.instruction.ThrowInstruction; import java.lang.classfile.instruction.TypeCheckInstruction; import java.lang.reflect.Field; import java.util.Stack; @@ -305,6 +306,11 @@ private void addInstructionNode(Instruction instruction) { Tree node = treeContext.createTree(type); addLeafNode(node); } + case ThrowInstruction throwInstruction -> { + Type type = TypeSet.type(throwInstruction.opcode().name()); + Tree node = treeContext.createTree(type); + addLeafNode(node); + } default -> { // todo: to be implemented } diff --git a/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java b/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java index c73b53a057..bfd6c91f3b 100644 --- a/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java +++ b/src/test/java/io/github/algomaster99/bytecode/diff/visitor/ClassFileVisitorTest.java @@ -20,6 +20,7 @@ public class ClassFileVisitorTest { private static final Path RESOURCES = Path.of("src/test/resources"); private static final Path EQ = RESOURCES.resolve("EQ"); + private static final Path NEQ1 = RESOURCES.resolve("NEQ1"); @Test void shouldShowDiffIn_majorVersion() throws IOException { @@ -176,7 +177,7 @@ void gumtreeShowsDifferenceBetween_tryCatchFinally_and_tryWithResource() throws // assert List rootOperations = diff.getRootOperations(); - assertThat(rootOperations).size().isEqualTo(28); + assertThat(rootOperations).size().isEqualTo(29); } @Test @@ -378,6 +379,21 @@ void shouldDelete3Instructions() throws IOException { } } + @Nested + class NEQ1 { + @Disabled("after difference in exception table is implemented") + @Test + void shouldShowDifferenceInExceptionTable() throws IOException { + Path oldClass = NEQ1.resolve("2").resolve("commons-codec-1.15").resolve("BaseNCodecOutputStream.class"); + Path newClass = NEQ1.resolve("2").resolve("commons-codec-1.16.0").resolve("BaseNCodecOutputStream.class"); + DiffImpl diff = getDiff(oldClass, newClass); + + // assert + List rootOperations = diff.getRootOperations(); + assertThat(rootOperations).size().isEqualTo(1); + } + } + private static DiffImpl getDiff(Path oldClass, Path newClass) throws IOException { byte[] bytes1 = Files.readAllBytes(oldClass); byte[] bytes2 = Files.readAllBytes(newClass); diff --git a/src/test/resources/NEQ1/2/README.md b/src/test/resources/NEQ1/2/README.md new file mode 100644 index 0000000000..526f71843c --- /dev/null +++ b/src/test/resources/NEQ1/2/README.md @@ -0,0 +1,2 @@ +Gumtree shows difference between the classfile version which is ignored by +diffoscope. diff --git a/src/test/resources/NEQ1/2/commons-codec-1.15/BaseNCodecOutputStream.class b/src/test/resources/NEQ1/2/commons-codec-1.15/BaseNCodecOutputStream.class new file mode 100644 index 0000000000000000000000000000000000000000..cbbe0bdba22292376dcd843c0ff559f4ac0b3551 GIT binary patch literal 2644 zcmbtWZBrXn6n<_J*bo* z2OK|kboxcd`cic|3UzF!)vx{ve~9Dw+?xQY)S03mvU~5@d!O^1bDneH_rHF51mJmi z8bSg`Jbxx>%p2ycl{CG%InOQfowv+n&UOudF*#%ut<12rWJ|$(DHscU%b3#;5$L#P zylo_HFF9g6f#p|!7l`J)bFM@Z5R(G2+$xSh-{`Y@P4RBgc4r)GXfYsIXlh76Gd(x3 z76O664M6P~F5e<#?FrkpgHr+{-J>f}RP#@505&z*Ga(R>If@~S%{m&;EU@igkb5TT zAkeC#4lxbw0t3&UjFkvtXv0?NXPZE{yNuYWqaM1%?$V(lDp$|xs6nm3rcv9qGNrkk z<&PUVhiPmc^-RN=Fnn9?E5=ALYZnEMZ6Nq3lE%DxN+bg9Pfu$_2zezafg02CiewWz zw_sZHflc*kNC>p7)Fho<{Ssq5eLDJ8%$8S>`=pM8a$i$$O2t_j@qkWg?`2jYf@(r#Fzbl{%(+CM1E=I?gCS zcA4s!hOxjfub(&ORbgbL!!pvdlvy{`Chv^scnL2H)UgL`r(D#d-T#S0TH?K;<5i3b z)U&C~&AScl1TQ^F!Z)H3;Q*sf-;5NB8H z#7X{uL>Upgu$wz#V-NPypIq&u|BzZqmSI}ihqQFl7U8Z(u^Qn@o^MJlqnEz}iJx$2 z8G~yanw5|9d}fH4<*f_Kw_d(i@h#-USciUt%q4n1QMcd17QW*AwS+@_b}ZxA9kl+_ zc1pSqV+Wt@ybeq;y;ram)99q~`f#<%eSgLMi#!)WsI!UgII7kW8KgxLsZB)gV`we7 z8129{`ng_(T3bO4$ziY#>oHGDUS}%I8OOPk@!QxRe;Cb3TYp#vNpt#0q%CqAjocmT z54S~@alZ2wVsg8T*Vf|P$3IfQO_q28O;|)L-eDo%Wr^OyAU?n`e2CNdNHH8N`>rq~ z3tVJ*DOa8x!#LTtGIy_2h+$$hVS=^@WPbRM$e$P#vK!dYd`eXg@GTP)mBg2E`7YOG z?MYDJp86Tnm*Ss7>-r4!4`5Q^*5i2Zqt8(v@4DLi z=!-RL*KH*^ibDyZH{MlF9hI?JQd%{fN`xzwD{Eswwb+-8>J9_>immZArT-0O_$?0M zE_3!BM({n-_<`Zxqk?`^p_E%@dNqI3{326A;wY&6$<+*3VU@KyXq&VKt+PS)Oct~2 z;?7Fp@p=pCwuC!B8BeGMi*2vztdVHIV>ZyQ92dVSChg^Gt4vy`!*MzZQH!z&4(9ll L+}b#XT}1x|)R!`m literal 0 HcmV?d00001 diff --git a/src/test/resources/NEQ1/2/commons-codec-1.16.0/BaseNCodecOutputStream.class b/src/test/resources/NEQ1/2/commons-codec-1.16.0/BaseNCodecOutputStream.class new file mode 100644 index 0000000000000000000000000000000000000000..e5dd937ec445a311dd7eae67e2bdd7e3694d8e97 GIT binary patch literal 2640 zcmbtWZF3V<6n<`!cGGlQO5cDcMc!%CmOw2p65ec~8Z8M@EYebCv)Qx@+ugF+KXI=eZ@m3znJ6+pgg+riP7@l^v0mTsfF82jhWn8FLyU0$taQ zTSm(EQlqvLSbpvI0?~qZ&Xq_4Vp1TMU&9gTAA5eUY2Gc_?u=s%F9rk)O$`fZrsoFM zLLe}-0jNDAm0N_YKViFea7tjbXKXc!TK?$`z@{gACj=rgM=^x4Sw|CE1-AVQa_>X~ z1ln~pAf};HVDR~qu^K@P9oQ=UY!e9gR1o7j8lg+L&p^7)IQ;{;ApN>)|o>7J?KOlAYQTdiigcR~_4t>cUW zWUr~dX&Mg<^Tv5&UX@2iIwB)IOR@D(bMnrpj#u!iKm$9`b}FSk+Vh_{WF+2eI$p<^ zKqH&HaM3E2oq&S)ACVFDFtR#wxWEG4_HEh2n#z=n;4o4oKks|<#*87qs#Ae*W@K2s ze7VS#DbQkpv^$|8NcGwI$MQw5NkDG2OVe$>f~ zTd)==uUv81E4zhKjc!eBrC7XTv1D;>g&oV4b<_Rk%!S`v?U*)f~e;J8n_cFHM*4( z?8HfafMf*`yRe%(Vq*{X(w|)IqyLavNtR(+*@v|B&=%pYSFsx9N}g{?uAq;f1IZt8 zXaz&-99or+^L%EBnB%Pr%C}y*SM@FA#Gpk+dXs!5<^Zv_-@z8X68yAC#It*h6pPjr8Twzitu@zH@<0|^8xt+9DoDNi-zD!F5p?C}3aX>v4+z>62NPRML zAH(Y*#Z+Qu>Bp`?t*@enoHGDUT3Pz8OOPki6!h$Jd9?gZ6GXzq&agW(h*rg zGk1pu!X1$noR8l@Om0{3#(JFl_yYx$8PhGa;5OQ^!167!Fz@o;!+SVJ$(+Uqis4Yj zca5JJoTi0{va`_>gm4Lsh==?Iq@;H?oZJ8048;9Jx=sJ`V@_c z?rVLIK3lhTFDao>97+g%iSA13sEo~$(wgB^GF+`(MH_>R82Fq~-DMzOupz#r;J>2y zzQG}U%bb0OQGAaK?lIgSsGMaLN~KYz*YY>bZ)8eH97UBsxtifBtg_YsZIjld#hYZO zCaIhs1A+NoVES8k06$r&s>%!VR7c P!NDBgl3NFduZ!s4aYZzv literal 0 HcmV?d00001 diff --git a/src/test/resources/NEQ1/2/diffoscope.diff b/src/test/resources/NEQ1/2/diffoscope.diff new file mode 100644 index 0000000000..fbfb980f55 --- /dev/null +++ b/src/test/resources/NEQ1/2/diffoscope.diff @@ -0,0 +1,37 @@ +--- commons-codec-1.15/BaseNCodecOutputStream.class ++++ commons-codec-1.16.0/BaseNCodecOutputStream.class +├── procyon -ec {} +│ @@ -12,30 +12,30 @@ +│ public class BaseNCodecOutputStream extends FilterOutputStream +│ { +│ private final boolean doEncode; +│ private final BaseNCodec baseNCodec; +│ private final byte[] singleByte; +│ private final BaseNCodec.Context context; +│ +│ - public BaseNCodecOutputStream(final OutputStream output, final BaseNCodec basedCodec, final boolean doEncode) { +│ - super(output); +│ + public BaseNCodecOutputStream(final OutputStream outputStream, final BaseNCodec basedCodec, final boolean doEncode) { +│ + super(outputStream); +│ this.singleByte = new byte[1]; +│ this.context = new BaseNCodec.Context(); +│ this.baseNCodec = basedCodec; +│ this.doEncode = doEncode; +│ } +│ +│ @Override +│ public void close() throws IOException { +│ this.eof(); +│ this.flush(); +│ this.out.close(); +│ } +│ +│ - public void eof() throws IOException { +│ + public void eof() { +│ if (this.doEncode) { +│ this.baseNCodec.encode(this.singleByte, 0, -1, this.context); +│ } +│ else { +│ this.baseNCodec.decode(this.singleByte, 0, -1, this.context); +│ } +│ } diff --git a/src/test/resources/NEQ1/2/gumtree.diff b/src/test/resources/NEQ1/2/gumtree.diff new file mode 100644 index 0000000000..c76f472c5b --- /dev/null +++ b/src/test/resources/NEQ1/2/gumtree.diff @@ -0,0 +1,5 @@ +[=== +update-node +--- +majorVersion: 51 [6,8] +replace 51 by 52] \ No newline at end of file