简体   繁体   English

如何删除“Assignment of parameter” is not allowed checkstyle 错误?

[英]How to remove “Assignment of parameter” is not allowed checkstyle error?

I am currently programing a simplified school course system using Java and I'm checking the style using CheckStyle.我目前正在使用 Java 编写简化的学校课程系统,并且正在使用 CheckStyle 检查样式。 When I run CheckStyle on one of my classes it gives me two errors that say:当我在我的一个类上运行 CheckStyle 时,它给了我两个错误:

Assignment of parameter 'endTime' is not allowed.
Assignment of parameter 'startTime' is not allowed.

Ive tried multiple things to solve this issue since I can't just create a setter when I'm using that annotation.我已经尝试了多种方法来解决这个问题,因为当我使用该注释时我不能只创建一个设置器。

I am also using Eclipse btw.我也在使用 Eclipse 顺便说一句。

    public void setMeetingDaysAndTime(String meetingDays, int startTime, int endTime) {
    if (meetingDays == null || meetingDays.length() == 0) {
        throw new IllegalArgumentException("Invalid meeting days.");
    }
    if ("A".equals(meetingDays)) {
        this.meetingDays = meetingDays;
        startTime = 0;
        endTime = 0;

This is just a snippet of where my error occurs.这只是我的错误发生的一个片段。

 startTime = 0;

This is re-assigning the parameter startTime .这是重新分配参数startTime

You didn't paste a lot - if startTime is also a field, this is not assigning the field.您没有粘贴很多 - 如果startTime也是一个字段,这不是分配该字段。 Just like the line above it, you'd need this.startTime = 0 , in order to use the field (that this. thing is needed when you shadow the field by taking in an argument that has the same name as the field).就像上面的行一样,您需要this.startTime = 0才能使用this.字段(当您通过接受与该字段同名的参数来隐藏该字段时,需要 this.thing )。

Alternatively, turn this lint checkstyle error off, it's silly.或者,关闭此 lint checkstyle 错误,这很愚蠢。 It is attempting to avoid confusion about what that value is during the run of your method, but the fix requires making a new local variable which is itself at least as confusing.它试图避免在方法运行期间对该值的混淆,但修复需要创建一个新的局部变量,该变量本身至少同样令人困惑。 In other words, it's a stylecheck that is raising a point where the cure is worse than the disease it is pointing at.换句话说,这是一种风格检查,它提高了治疗方法比它所指向的疾病更糟糕的程度。

If your intent really is to just carry on with the setMeetingDaysAndTime method as if startTime was 0 , then make a new local variable:如果您的意图确实是继续使用setMeetingDaysAndTime方法,就好像startTime0一样,那么创建一个新的局部变量:

public void setMeetingDaysAndTime(String meetingDays, int startTime_, ...) {
    int startTime = startTime_;

    if ( ... ) {
        startTime = 0;
    }
}

The following description is given for ParameterAssignment : 对 ParameterAssignment 给出以下描述

Disallows assignment of parameters.不允许分配参数。

Rationale: Parameter assignment is often considered poor programming practice.理由:参数分配通常被认为是糟糕的编程实践。 Forcing developers to declare parameters as final is often onerous.强制开发人员将参数声明为 final 通常很麻烦。 Having a check ensure that parameters are never assigned would give the best of both worlds.进行检查以确保永远不会分配参数将两全其美。

In your case,在你的情况下,

startTime = 0;
endTime = 0;

might be an error instead of可能是一个错误而不是

this.startTime = 0;
this.endTime = 0;

depending on whether there are the fields startTime and endTime that you actually meant.取决于是否有您实际意思的字段startTimeendTime

To prevent this error (when there is a parameter with the same name as the field), make sure to use this.<fieldName> =...;为防止出现此错误(当存在与字段同名的参数时),请确保使用this.<fieldName> =...; or use the prefix new for setter method parameters:或为 setter 方法参数使用前缀new

public void setMeetingDaysAndTime(String newMeetingDays, int newStartTime, int newEndTime) {
    if (newMeetingDays == null || newMeetingDays.length() == 0) {
        throw new IllegalArgumentException("Invalid meeting days.");
    }
    if ("A".equals(newMeetingDays)) {
        meetingDays = newMeetingDays;
        startTime = 0;
        endTime = 0;

If you really want to assign a new value to the parameter, use a variable with a descriptive name instead.如果您确实想为参数分配新值,请改用具有描述性名称的变量。

Example: Instead of re-assigning parameters like:示例:而不是重新分配参数,例如:

public void foo(String message, int value) {
    message = message.trim();
    value = value < 0 ? 0 : value;
    bar(message, value);

use local variables instead:改用局部变量:

public void foo(String message, int value) {
    String trimmedMessage = message.trim();
    int normalizedValue = value < 0 ? 0 : value;
    bar(trimmedMessage, normalizedValue);

Eclipse provides the ParameterAssignment Checkstyle warning also as compiler warning ( Project > Properties: Java Compiler > Errors/Warnings ): Code style > Parameter assignment . Eclipse 提供 ParameterAssignment Checkstyle 警告也作为编译器警告项目>属性:Java 编译器>错误/警告):代码样式>参数分配 And for the root cause, if this.而对于根本原因,如果是this. is missing by mistake, there is the compiler warning Name shadowing and conflicts > Local variable declaration hides another field or variable which is also disabled by default.错误丢失,有编译器警告名称阴影和冲突>局部变量声明隐藏另一个默认情况下也禁用的字段或变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM