Skip to content

Commit

Permalink
add a new nextIsLdcStartingWith function to the AbstractMatcher to si…
Browse files Browse the repository at this point in the history
…mplify matching code
  • Loading branch information
poseidon-mysugr committed Nov 22, 2021
1 parent 814ccfc commit 0ffc688
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ final void nextIsLdc(final String cst) {
cursor = null;
}

/**
* Moves {@link #cursor} to next instruction if it is <code>LDC</code> with
* <code>cst</code> that starts with <code>cstPrefix</code>, otherwise sets
* it to <code>null</code>.
*/
final void nextIsLdcStartingWith(final String cstPrefix) {
nextIs(Opcodes.LDC);
if (cursor == null) {
return;
}
LdcInsnNode ldcNode = (LdcInsnNode) cursor;
if (ldcNode.cst instanceof String
&& ((String) ldcNode.cst).startsWith(cstPrefix)) {
return;
}
cursor = null;
}

/**
* Moves {@link #cursor} to next instruction if it has given opcode,
* otherwise sets it to <code>null</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,7 @@ private void match(final MethodNode methodNode,
cursor = s.dflt;
nextIsType(Opcodes.NEW, "java/lang/IllegalStateException");
nextIs(Opcodes.DUP);
nextIs(Opcodes.LDC);
if (cursor == null) {
return;
}
if (!((LdcInsnNode) cursor).cst.equals(
"call to 'resume' before 'invoke' with coroutine")) {
return;
}
nextIsLdc("call to 'resume' before 'invoke' with coroutine");
nextIsInvoke(Opcodes.INVOKESPECIAL,
"java/lang/IllegalStateException", "<init>",
"(Ljava/lang/String;)V");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,8 @@ public void match(final MethodNode methodNode,
nextIs(Opcodes.IFNULL);
nextIsType(Opcodes.NEW, "java/lang/UnsupportedOperationException");
nextIs(Opcodes.DUP);
nextIs(Opcodes.LDC);
if (cursor == null
|| !(((LdcInsnNode) cursor).cst instanceof String)
|| !(((String) ((LdcInsnNode) cursor).cst).startsWith(
"Super calls with default arguments not supported in this target"))) {
cursor = null;
}
nextIsLdcStartingWith(
"Super calls with default arguments not supported in this target");
nextIsInvoke(Opcodes.INVOKESPECIAL,
"java/lang/UnsupportedOperationException", "<init>",
"(Ljava/lang/String;)V");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,7 @@ public void match(final String exceptionType,
final JumpInsnNode jumpInsnNode = (JumpInsnNode) cursor;
nextIsType(Opcodes.NEW, exceptionType);
nextIs(Opcodes.DUP);
nextIs(Opcodes.LDC);
if (cursor == null) {
return;
}
final LdcInsnNode ldc = (LdcInsnNode) cursor;
if (!(ldc.cst instanceof String && ((String) ldc.cst)
.startsWith("null cannot be cast to non-null type"))) {
return;
}
nextIsLdcStartingWith("null cannot be cast to non-null type");
nextIsInvoke(Opcodes.INVOKESPECIAL, exceptionType, "<init>",
"(Ljava/lang/String;)V");
nextIs(Opcodes.ATHROW);
Expand Down

0 comments on commit 0ffc688

Please sign in to comment.