在NetBeans中无法正常对Java类中的字段进行排序。

huangapple go评论83阅读模式
英文:

Sorting fields in Java class not working in NetBeans

问题

以下是您要翻译的内容:

我正在尝试使用NetBeans,并且真的很喜欢其中的一些做法。我之前用的是Eclipse。像是能够对源代码项进行排序的功能却没有生效。

使用的环境:

产品版本:Apache NetBeans IDE 12.1
Java:1.8.0_131;Java HotSpot(TM) 64位 Server VM 25.131-b11
运行时:Java(TM) SE Runtime Environment 1.8.0_131-b11
系统:运行在amd64上的Linux版本4.8.6-300.fc25.x86_64;UTF-8;en_US (nb)
用户目录:/home/bpracht/.netbeans/12.1
缓存目录:/home/bpracht/.cache/netbeans/12.1

在示例类中,我希望按照常规方式排序(即,将私有字段排序到顶部,将公共方法排序到底部)。在下面的类上单击“Alt-Shift-F”格式化之后,它并没有进一步改变顺序:

class Token<T> {

    public Token offsetStart(final Long value) {
        this.offsetStart = value;
        return this;
    }

    public Token length(final Long value) {
        this.length = value;
        return this;
    }

    public Token fullyResolved(final Boolean value) {
        this.fullyResolved = value;
        return this;
    }

    // 更多方法...

    private Long offsetStart;

    private Long length;

    private Boolean fullyResolved;

    // 更多字段...

    public Long getLength() {
        return length;
    }

    public long getOffsetStart() {
        return offsetStart;
    }

    public void setOffsetStart(long offsetStart) {
        this.offsetStart = offsetStart;
    }

    public void setLength(Long length) {
        this.length = length;
    }

}

interface LambdaSetValue<T> {

    void setValue(T value);
}

在“工具-选项-编辑器-》格式化”中,我有以下设置:
成员排序顺序:
字段
静态字段
静态初始化器
静态方法
静态类

实例初始化器
构造函数
方法

(已选中)按可见性排序成员:
私有
受保护
默认
公共

(已选中)保持 Getter 和 Setter 在一起
(已选中)按字母顺序对成员分组
(未选中)按依赖关系排序
插入点(按类别排序)

我希望至少能将

private Long offsetStart

放在

private Long length

之后。

提前感谢您的帮助。
Woodsman

英文:

I'm experimenting with NetBeans and really like how some things are done. I'm coming from Eclipse. Features like the ability to sort the source code items aren't working.
Using:

Product Version: Apache NetBeans IDE 12.1
Java: 1.8.0_131; Java HotSpot(TM) 64-Bit Server VM 25.131-b11
Runtime: Java(TM) SE Runtime Environment 1.8.0_131-b11
System: Linux version 4.8.6-300.fc25.x86_64 running on amd64; UTF-8; en_US (nb)
User directory: /home/bpracht/.netbeans/12.1
Cache directory: /home/bpracht/.cache/netbeans/12.1

In the sample class, I'd like to sort this in a conventional way (ie. private fields sorted at the top, public methods at the bottom. After clicking Format "Alt-Shift-F" on the class below, it does not alter the order any further:

class Token&lt;T&gt; {

    public Token offsetStart(final Long value) {
        this.offsetStart = value;
        return this;
    }

    public Token length(final Long value) {
        this.length = value;
        return this;
    }

    public Token fullyResolved(final Boolean value) {
        this.fullyResolved = value;
        return this;
    }

    public Token parentToken(final Token value) {
        this.parentToken = value;
        return this;
    }

    public Token firstChildToken(final Token value) {
        this.firstChildToken = value;
        return this;
    }

    public Token lastChildToken(final Token value) {
        this.lastChildToken = value;
        return this;
    }

    public Token previousToken(final Token value) {
        this.previousToken = value;
        return this;
    }

    public Token nextToken(final Token value) {
        this.nextToken = value;
        return this;
    }

    public Token lambdaSetValue(final LambdaSetValue&lt;T&gt; value) {
        this.lambdaSetValue = value;
        return this;
    }

    private Long offsetStart;

    private Long length;

    private Boolean fullyResolved;

    private Token parentToken;

    private Token firstChildToken;

    private Token lastChildToken;

    private Token previousToken;

    private Token nextToken;

    private LambdaSetValue&lt;T&gt; lambdaSetValue;

    public Long getLength() {
        return length;
    }

    public long getOffsetStart() {
        return offsetStart;
    }

    public void setOffsetStart(long offsetStart) {
        this.offsetStart = offsetStart;
    }

    public void setLength(Long length) {
        this.length = length;
    }

}

interface LambdaSetValue&lt;T&gt; {

    void setValue(T value);
}

In Tools-Options-Editor->Formatting I have this:
Members Sort Order:
Fields
Static Fields
Static Initializers
Static Methods
Static Classes
Classes
Instance Initializers
Constructors
Methods

(checked) Sort Members By Visibility has:
Private
Protected
Default
Public

(checked) Keep Getters and Setters Together
(checked) Sort Memoers in Groups Alphabetically
(unchecked) Sort uses dependencies
Insertion Point (Ordered in Category

I want it to at least put

private Long offsetStart after private Long length;

Thanks in advance
Woodsman

答案1

得分: 1

你可以使用源菜单对成员进行排序,然后选择“组织成员”:

在NetBeans中无法正常对Java类中的字段进行排序。

使用我的设置对成员进行组织后的结果:

class Token<T> {

    private Long offsetStart;

    private Long length;

    private Boolean fullyResolved;

    private Token parentToken;

    private Token firstChildToken;

    private Token lastChildToken;

    private Token previousToken;

    private Token nextToken;

    private LambdaSetValue<T> lambdaSetValue;

    public Token offsetStart(final Long value) {
        this.offsetStart = value;
        return this;
    }

    public Token length(final Long value) {
        this.length = value;
        return this;
    }

    public Token fullyResolved(final Boolean value) {
        this.fullyResolved = value;
        return this;
    }

    public Token parentToken(final Token value) {
        this.parentToken = value;
        return this;
    }

    public Token firstChildToken(final Token value) {
        this.firstChildToken = value;
        return this;
    }

    public Token lastChildToken(final Token value) {
        this.lastChildToken = value;
        return this;
    }

    public Token previousToken(final Token value) {
        this.previousToken = value;
        return this;
    }

    public Token nextToken(final Token value) {
        this.nextToken = value;
        return this;
    }

    public Token lambdaSetValue(final LambdaSetValue<T> value) {
        this.lambdaSetValue = value;
        return this;
    }

    public Long getLength() {
        return length;
    }

    public long getOffsetStart() {
        return offsetStart;
    }

    public void setOffsetStart(long offsetStart) {
        this.offsetStart = offsetStart;
    }

    public void setLength(Long length) {
        this.length = length;
    }
}

interface LambdaSetValue<T> {
    void setValue(T value);
}

正如我在评论中提到的,private Long offsetStartprivate Long length 都具有相同的访问级别和相同的类型,没有功能代码上的差异,而 Netbeans 不会按字母顺序排序(使用英语)。

英文:

You can order the members by using the Source menu then choosing Organize Members:
在NetBeans中无法正常对Java类中的字段进行排序。

The result of organizing members using my settings:

class Token&lt;T&gt; {
private Long offsetStart;
private Long length;
private Boolean fullyResolved;
private Token parentToken;
private Token firstChildToken;
private Token lastChildToken;
private Token previousToken;
private Token nextToken;
private LambdaSetValue&lt;T&gt; lambdaSetValue;
public Token offsetStart(final Long value) {
this.offsetStart = value;
return this;
}
public Token length(final Long value) {
this.length = value;
return this;
}
public Token fullyResolved(final Boolean value) {
this.fullyResolved = value;
return this;
}
public Token parentToken(final Token value) {
this.parentToken = value;
return this;
}
public Token firstChildToken(final Token value) {
this.firstChildToken = value;
return this;
}
public Token lastChildToken(final Token value) {
this.lastChildToken = value;
return this;
}
public Token previousToken(final Token value) {
this.previousToken = value;
return this;
}
public Token nextToken(final Token value) {
this.nextToken = value;
return this;
}
public Token lambdaSetValue(final LambdaSetValue&lt;T&gt; value) {
this.lambdaSetValue = value;
return this;
}
public Long getLength() {
return length;
}
public long getOffsetStart() {
return offsetStart;
}
public void setOffsetStart(long offsetStart) {
this.offsetStart = offsetStart;
}
public void setLength(Long length) {
this.length = length;
}
}
interface LambdaSetValue&lt;T&gt; {
void setValue(T value);
}

As mentioned in my comment, both private Long offsetStart and private Long length have the same access levels and the same type with no functional code difference, and Netbeans does not apply alphabetical order (in English).

huangapple
  • 本文由 发表于 2020年10月23日 06:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64491587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定