// // Various ways of enforcing non-null parameter. // In all cases, par should be subject to code sinking. //
/// CHECK-START: void Main.doit1(int[]) code_sinking (before) /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: <<Tst:z\d+>> Equal /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: InvokeStaticOrDirect [{{l\d+}},<<Str>>] method_name:java.lang.StringBuilder.append /// CHECK: Throw /// CHECK: end_block // /// CHECK-START: void Main.doit1(int[]) code_sinking (after) /// CHECK: begin_block /// CHECK: <<Tst:z\d+>> Equal /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: InvokeStaticOrDirect [{{l\d+}},<<Str>>] method_name:java.lang.StringBuilder.append /// CHECK: Throw /// CHECK: end_block staticpublicvoid doit1(int[] a) { // Being in the boot image means we know the load string cannot throw. Create one that is // unlikely to be there to ensure we handle that case.
String par = "stringUnlikelyToBeInBootImage"; if (a == null) thrownew Error("you are null: " + par); for (int i = 0; i < a.length; i++) {
a[i] = 1;
}
}
/// CHECK-START: void Main.doit2(int[]) code_sinking (before) /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: <<Tst:z\d+>> NotEqual /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: InvokeStaticOrDirect [<<Str>>] method_name:Main.doThrow /// CHECK: end_block // /// CHECK-START: void Main.doit2(int[]) code_sinking (after) /// CHECK: begin_block /// CHECK: <<Tst:z\d+>> NotEqual /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: InvokeStaticOrDirect [<<Str>>] method_name:Main.doThrow /// CHECK: end_block staticpublicvoid doit2(int[] a) { // Being in the boot image means we know the load string cannot throw. Create one that is // unlikely to be there to ensure we handle that case.
String par = "stringUnlikelyToBeInBootImage"; if (a == null)
doThrow(par); for (int i = 0; i < a.length; i++) {
a[i] = 2;
}
}
/// CHECK-START: void Main.doit3(int[]) code_sinking (before) /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: <<Tst:z\d+>> Equal /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: InvokeStaticOrDirect [{{l\d+}},<<Str>>] method_name:java.lang.StringBuilder.append /// CHECK: Throw /// CHECK: end_block // /// CHECK-START: void Main.doit3(int[]) code_sinking (after) /// CHECK: begin_block /// CHECK: <<Tst:z\d+>> Equal /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: InvokeStaticOrDirect [{{l\d+}},<<Str>>] method_name:java.lang.StringBuilder.append /// CHECK: Throw /// CHECK: end_block staticpublicvoid doit3(int[] a) { // Being in the boot image means we know the load string cannot throw. Create one that is // unlikely to be there to ensure we handle that case.
String par = "stringUnlikelyToBeInBootImage";
checkNotNullDirect(a, par); for (int i = 0; i < a.length; i++) {
a[i] = 3;
}
}
/// CHECK-START: void Main.doit4(int[]) code_sinking (before) /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: <<Tst:z\d+>> NotEqual /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: InvokeStaticOrDirect [<<Str>>] method_name:Main.doThrow /// CHECK: end_block // /// CHECK-START: void Main.doit4(int[]) code_sinking (after) /// CHECK: begin_block /// CHECK: <<Tst:z\d+>> NotEqual /// CHECK: If [<<Tst>>] /// CHECK: end_block /// CHECK: begin_block /// CHECK: <<Str:l\d+>> LoadString /// CHECK: InvokeStaticOrDirect [<<Str>>] method_name:Main.doThrow /// CHECK: end_block staticpublicvoid doit4(int[] a) { // Being in the boot image means we know the load string cannot throw. Create one that is // unlikely to be there to ensure we handle that case.
String par = "stringUnlikelyToBeInBootImage";
checkNotNullSplit(a, par); // resembles Kotlin runtime lib // (test is lined, doThrow is not) for (int i = 0; i < a.length; i++) {
a[i] = 4;
}
}
// Ensures Phi values are merged properly. staticpublicint doit5(int[] a) { int t = 100; // Being in the boot image means we know the load string cannot throw. Create one that is // unlikely to be there to ensure we handle that case.
String par = "stringUnlikelyToBeInBootImage"; if (a == null) {
doThrow(par);
} else {
t = 1000;
} for (int i = 0; i < a.length; i++) {
a[i] = 5;
} // Phi on t, even though doThrow never reaches. return t;
}
// // Various ways of exploiting non-null parameter. // In all cases, implicit null checks are redundant. //
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.