谁能解释Android ViewBinding&#x27的bind()函数优化的Java代码?

发布于 2025-01-18 13:11:16 字数 969 浏览 0 评论 0原文

我正在查看 app/build/ generated/data_binding_base_class_source_code 中自动生成的 ViewBinding 代码,并看到了 bind() 函数代码,但我无法理解它。

@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));
}

这个 MissingId: {} 块是什么,看起来像 goto + switch。评论已经说没有人会这样写,但这仍然是 Java 语言的特性。

谁能解释一下这是如何工作的,并且这个功能有名字吗?

我在 Java 方面有一些经验,但主要是在 Kotlin 上工作,所以在任何地方都找不到它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲欢浪云 2025-01-25 13:11:16

sissove_idbreak使用的标签。您可以找到更多详细信息在这里

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文