Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Support for Ternary Operator assignments in var context

未关闭
#237 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
java
领域
devtools

调研方向

从 #217 中引用的现有 var 重构工作开始,并将列出的三元示例和前置条件作为行为规范。完成的标准是:该 recipe 仅对符合条件的三元赋值应用 var,跳过所述排除情况,并验证变更前后的结果。

由索引模型根据 Issue 内容生成。

描述

enhancement

What problem are you trying to solve?

While developing #217 I discovered that handling, especially detecting ternary operator assignments, is hard.
With this issue, Rewrite become able to apply ´var´ to ternary operator assignments.

What precondition(s) should be checked before applying this recipe?

var should not be applied to variable declarations where:

  • the type of the ternary operator and the left-hand side don't match
  • the type is byte or short
  • only null is assigned
  • the declaration is outside a method declaration

Describe the situation before applying the recipe

class A {
    void foo(String bar) {
        int i1 = bar != null ? 3 : 5;
        Integer i2 = bar != null ? 3 : null;
        Integer i3 = bar == null ? null : bar.lenght();
        Integer i4 = bar != null ? throw new IlligalArgumentExcpetion() : -1;
        int i5 = bar != null ? bar.lenght() : 0;
        float f1 = bar != null ? 3f : 3.5;
    }
}

Describe the situation after applying the recipe


class A {
    void foo(String bar) {
        var i1 = bar != null ? 3 : 5;
        var i2 = bar != null ? 3 : null;
        var i3 = bar == null ? null : bar.lenght();
        var i4 = bar != null ? throw new IlligalArgumentExcpetion() : -1;
        var i5 = bar != null ? bar.lenght() : 0;
        var f1 = bar != null ? 3f : 3.5;
    }
}

Have you considered any alternatives or workarounds?

An alternative would be to skip every ternary operator, this would lead to no 100% local variable type inference for rewrite.

Any additional context

It would be nice to write a separate recipe for this and integrate them later on as global var refactoring.

Are you interested in contributing this recipe to OpenRewrite?

not now.

主要语言
Java
星标
156
派生
129
平均合并
7 小时 12 分钟
30 天内合并 PR
43

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openrewrite/rewrite-migrate-java 的其他 Issue

查看 openrewrite/rewrite-migrate-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。