Search code examples
javaoptimizationandroid-viewbindinggenerated

Can anyone explain the Android ViewBinding's bind() function optimized Java code?


I was looking at the auto-generated ViewBinding code in app/build/generated/data_binding_base_class_source_code and saw the bind() functions code and i cannot understand it.

@NonNull
public static LayoutBindingBinding bind(@NonNull View rootView) {
  // The body of this method is generated in a way you would not otherwise write.
  // This is done to optimize the compiled bytecode for size and performance.
  int id;
  missingId: {
    id = R.id.text_name;
    TextView textName = ViewBindings.findChildViewById(rootView, id);
    if (textName == null) {
      break missingId;
    }

    return new LayoutBindingBinding((LinearLayout) rootView, textName);
  }
  String missingId = rootView.getResources().getResourceName(id);
  throw new NullPointerException("Missing required view with ID: ".concat(missingId));
}

What is this missingId: {} block it looks like goto + switch. The comment already says nobody would write like this, but still it is a Java language feature.

Can anyone explain me how this works and also does this feature has a name?

I have some experience in Java but mostly working on Kotlin, so cannot find it anywhere.


Solution

  • missing_id is a label which is used by the break. You can find more details here.