非瞬态类成员的 Java PMD 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:35:17  来源:igfitidea点击:

Java PMD warning on non-transient class member

javacoding-stylejavabeanspmd

提问by Yuval Adam

On line:

在线的:

private boolean someFlag;

I get the following PMD warning:

我收到以下 PMD 警告:

Found non-transient, non-static member. Please mark as transient or provide accessors.

找到非瞬态、非静态成员。请标记为瞬态或提供访问器。

Can someone please explain why this warning is there and what it means? (I understand how to fix it, I don't understand why it's there...)

有人可以解释为什么会出现此警告以及它的含义吗?(我明白如何修复它,我不明白为什么它在那里......)

I'm getting this on many other member declarations as well...

我也在许多其他成员声明中得到了这一点......



EDIT:My class is definitelynot a bean, and not serializable...

编辑:我的班级绝对不是bean,也不是可序列化的......

采纳答案by tcurdt

I assume your class is a bean that by definition implements Serializable. A transient variable will be excluded from the serialization process. If you serialize and then deserialize the bean the value will be actually have the default value.

我假设你的类是一个 bean,根据定义实现Serializable. 瞬态变量将从序列化过程中排除。如果您序列化然后反序列化 bean,则该值实际上将具有默认值。

PMD assumes you are dealing with a serializable bean here. For a bean it is expected to have getters/setters for all member variables. As you have omitted these, you imply that your member variable is not part of the bean ....and so does not need to be serialized. If that is the case you should exclude it from the serialization. Which you do by marking the variable as "transient".

PMD 假设您在这里处理的是一个可序列化的 bean。对于 bean,期望所有成员变量都有 getter/setter。由于您省略了这些,您暗示您的成员变量不是 bean 的一部分……因此不需要序列化。如果是这种情况,您应该将其从序列化中排除。您可以通过将变量标记为“瞬态”来实现。

回答by Andreas Petersson

transient is used as a hint to the jvm serialization that it should ignore it when writing a class in a stream/to disk. so if your instance is recovered and becomes an object in memory, the field will be null.

瞬态用作 jvm 序列化的提示,即在将类写入流/磁盘时应忽略它。因此,如果您的实例被恢复并成为内存中的对象,则该字段将为空。

the problem with static members is, there is only one of them in memory at a time. so its not entirely clear what should happen upon deserialization. should the old value be kept? or the cached version overwrite the old one?

静态成员的问题是,一次只有一个成员在内存中。所以它并不完全清楚反序列化时会发生什么。应该保留旧值吗?还是缓存版本覆盖旧版本?

what you should do: do not use static fields in Serializable classes at all. move it somewhere else, or better yet, do not use static members / singletons at all. they introduce a global state which may lead to numerous problems and bad OO design.

你应该做什么:根本不要在 Serializable 类中使用静态字段。将它移到其他地方,或者更好的是,根本不要使用静态成员/单身人士。它们引入了一个全局状态,这可能会导致许多问题和糟糕的 OO 设计。

回答by toolkit

See the rule that is happening here

看到这里发生的规则

BeanMembersShouldSerialize

If a class is a bean, or is referenced by a bean directly or indirectly it needs to be serializable. Member variables need to be marked as transient, static, or have accessor methods in the class. Marking variables as transient is the safest and easiest modification. Accessor methods should follow the Java naming conventions, i.e.if you have a variable foo, you should provide getFoo and setFoo methods.

BeanMembersShouldSerialize

如果一个类是一个 bean,或者被一个 bean 直接或间接引用,它需要是可序列化的。成员变量需要标记为瞬态、静态或在类中具有访问器方法。将变量标记为瞬态是最安全和最简单的修改。访问器方法应该遵循 Java 命名约定,即如果你有一个变量 foo,你应该提供 getFoo 和 setFoo 方法。

回答by Andreas Petersson

ok, now i get it. after adding the definition

好的,现在我明白了。添加定义后

private boolean someFlag;

it is clear what happens here:

很明显这里发生了什么:

this error message does refer to the accessing schema. PMD states that classes referred to by beans, must also follow the bean schema.

此错误消息确实涉及访问架构。PMD 声明 bean 引用的类也必须遵循 bean 模式。

most likely to support property-style access like MyBean.referredClass.someFlag will be translated to someObject.getReferredClass().getSomeFlag()

最有可能支持属性样式的访问,如 MyBean.referredClass.someFlag 将被转换为 someObject.getReferredClass().getSomeFlag()

PMD it expects that there is a isSomeFlag()/getSomeFlag() and setSomeFlag() method by which you could access its value, and not access it directly.

PMD 它期望有一个 isSomeFlag()/getSomeFlag() 和 setSomeFlag() 方法,您可以通过它们访问它的值,而不是直接访问它。

Found non-transient, non-static member. Please mark as transient **or provide accessors**.