是否有可能在任何 Java IDE 中折叠源代码中的类型定义?

发布于 2024-10-10 11:30:09 字数 1004 浏览 2 评论 0原文

最近我经常不得不阅读这样的 Java 代码:

LinkedHashMap<String, Integer> totals =  new LinkedHashMap<String, Integer>(listOfRows.get(0))
for (LinkedHashMap<String, Integer> row : (ArrayList<LinkedHashMap<String,Integer>>) table.getValue()) {    
    for(Entry<String, Integer> elem : row.entrySet()) {
        String colName=elem.getKey();
        int Value=elem.getValue();
        int oldValue=totals.get(colName);

        int sum = Value + oldValue;
        totals.put(colName, sum);
    }
}

由于长且嵌套的类型定义,简单的算法变得相当模糊。因此,我希望可以使用 IDE 删除或折叠类型定义,以查看没有类型的 Java 代码,例如:

totals =  new (listOfRows.get(0))
for (row : table.getValue()) {    
    for(elem : row.entrySet()) {
        colName=elem.getKey();
        Value=elem.getValue();
        oldValue=totals.get(colName);

        sum = Value + oldValue;
        totals.put(colName, sum);
    }
}

最好的方法当然是折叠类型定义,但是当将鼠标移到变量上时,会将类型显示为工具提示。是否有 Java IDE 或 IDE 插件可以做到这一点?

Lately I often have to read Java code like this:

LinkedHashMap<String, Integer> totals =  new LinkedHashMap<String, Integer>(listOfRows.get(0))
for (LinkedHashMap<String, Integer> row : (ArrayList<LinkedHashMap<String,Integer>>) table.getValue()) {    
    for(Entry<String, Integer> elem : row.entrySet()) {
        String colName=elem.getKey();
        int Value=elem.getValue();
        int oldValue=totals.get(colName);

        int sum = Value + oldValue;
        totals.put(colName, sum);
    }
}

Due to the long and nested type definitions the simple algorithm becomes quite obscured. So I wished I could remove or collapse the type definitions with my IDE to see the Java code without types like:

totals =  new (listOfRows.get(0))
for (row : table.getValue()) {    
    for(elem : row.entrySet()) {
        colName=elem.getKey();
        Value=elem.getValue();
        oldValue=totals.get(colName);

        sum = Value + oldValue;
        totals.put(colName, sum);
    }
}

The best way of course would be to collapse the type definitions, but when moving the mouse over a variable show the type as a tooltip. Is there a Java IDE or a plugin for an IDE that can do this?

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

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

发布评论

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

评论(1

゛时过境迁 2024-10-17 11:30:09

IntelliJ IDEA 会将声明右侧的类型转换为 <~>。这样:

Map<Integer, String> m = new HashMap<Integer, String>();

将显示折叠为:

Map<Integer, String> m = new HashMap<~>();

这可以通过编辑器/代码折叠/通用构造函数和方法参数属性和IDE 的社区版 是免费的。


Or you could use Scala, which has type inference:

val totals = new mutable.Map[String, Int]
for { 
    row <- table.getValue
    (colName, value) <- row.entrySet 
} totals += (colName -> (value + totals.get(colName) getOrElse 0)

IntelliJ IDEA will convert the types on the right-hand side of a declaration to <~>. So that:

Map<Integer, String> m = new HashMap<Integer, String>();

Will appear folded as:

Map<Integer, String> m = new HashMap<~>();

This is settable via the Editor / Code Folding / Generic Constructor and Method Parameters property and the community edition of the IDE is free.


Or you could use Scala, which has type inference:

val totals = new mutable.Map[String, Int]
for { 
    row <- table.getValue
    (colName, value) <- row.entrySet 
} totals += (colName -> (value + totals.get(colName) getOrElse 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文