Duplicate and ambiguous switch cases
david32768@btinternet.com david32768@btinternet.com
david32768 at btinternet.com
Mon Jan 29 06:46:06 UTC 2024
I apologise for the misleading comment about the error message.
If the bad lookup is picked up by StackMapGenerate (V65) it also prints
the code.
If the bad lookup is picked up by StackCounter (V50) it does not.
```
import java.io.IOException;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.ArrayList;
import java.lang.classfile.*;
import java.lang.classfile.instruction.*;
public class CBBuildDup {
private static void build(CodeBuilder cob) {
Label L1 = cob.newLabel();
Label L2 = cob.newLabel();
SwitchCase sc1 = SwitchCase.of(0,L2); // 0 -> L2
SwitchCase sc2 = SwitchCase.of(0,L2); // 0 -> L2 Duplicate
// SwitchCase sc2 = SwitchCase.of(0,L1); // 0 -> L1 Ambiguous
List<SwitchCase> cases = new ArrayList<>();
cases.add(sc1);
cases.add(sc2);
cob.iload(0)
.lookupswitch(L1, cases)
// .tableswitch(L1, cases)
.labelBinding(L2)
.iconst_1()
.ireturn()
.labelBinding(L1)
.iconst_0()
.ireturn();
}
public static void main(String[] args) throws IOException {
byte[] bytes = ClassFile.of()
.build(ClassDesc.of("CBSwitchDup"),
clb -> clb.withFlags(ClassFile.ACC_PUBLIC|
ClassFile.ACC_SUPER)
// .withVersion(50,0) // bad lookup (StackCounter)
.withVersion(65,0) // bad lookup + code dump (StackMapGenerator)
.withMethodBody("table",
MethodTypeDesc.ofDescriptor("(I)I"),
ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC,
CBBuildDup::build));
for (VerifyError err:ClassFile.of().verify(bytes)) {
System.err.println(err);
}
Path pathc = Paths.get("CBSwitchDup.class");
Files.write(pathc,bytes);
}
}
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240129/f9b5f569/attachment-0001.htm>
More information about the classfile-api-dev
mailing list