简体   繁体   English

Java 非瞬态 class 成员的 PMD 警告

[英]Java PMD warning on non-transient class member

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 definitely not a bean, and not serializable...编辑:我的 class绝对不是 bean,也不是可序列化的......

I assume your class is a bean that by definition implements Serializable .我假设您的 class 是根据定义实现Serializable的bean。 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 进行序列化然后反序列化,则该值实际上将具有默认值。

PMD assumes you are dealing with a serializable bean here. PMD 假设您在这里处理的是可序列化的 bean。 For a bean it is expected to have getters/setters for all member variables.对于一个 bean,它应该有所有成员变量的 getter/setter。 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.由于您省略了这些,因此您暗示您的成员变量不是 bean 的一部分......因此不需要序列化。 If that is the case you should exclude it from the serialization.如果是这种情况,您应该将其从序列化中排除。 Which you do by marking the variable as "transient".您可以通过将变量标记为“瞬态”来执行此操作。

Now I get it.现在我懂了。

After adding this 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/getSomeFlagsetSomeFlag方法,您可以通过它访问它的值,而不是直接访问它。

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

See the rule that is happening here查看这里发生的规则

BeanMembersShouldSerialize BeanMembersShouldSerialize

If a class is a bean, or is referenced by a bean directly or indirectly it needs to be serializable.如果 class 是 bean,或者被 bean 直接或间接引用,则它需要可序列化。 Member variables need to be marked as transient, static, or have accessor methods in the class.成员变量需要标记为瞬态,static,或者在 class 中有访问器方法。 Marking variables as transient is the safest and easiest modification.将变量标记为瞬态是最安全和最简单的修改。 Accessor methods should follow the Java naming conventions, ieif you have a variable foo, you should provide getFoo and setFoo methods.访问器方法应该遵循 Java 命名约定,即如果你有一个变量 foo,你应该提供 getFoo 和 setFoo 方法。

transient is used as a hint to the jvm serialization that it should ignore it when writing a class in a stream/to disk.瞬态用作 jvm 序列化的提示,当将 class 写入流/磁盘时,它应该忽略它。 so if your instance is recovered and becomes an object in memory, the field will be null.因此,如果您的实例已恢复并成为 memory 中的 object,则该字段将为 null。

the problem with static members is, there is only one of them in memory at a time. static 成员的问题是,在 memory 中一次只有一个。 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.你应该做什么:根本不要在 Serializable 类中使用 static 字段。 move it somewhere else, or better yet, do not use static members / singletons at all.将它移到其他地方,或者更好的是,根本不要使用 static 成员/单身人士。 they introduce a global state which may lead to numerous problems and bad OO design.他们引入了全局 state,这可能会导致许多问题和糟糕的 OO 设计。

you can solve this issue in 2 ways:您可以通过 2 种方式解决此问题:

  1. Add getters and setters methods for members.为成员添加 getter 和 setter 方法。
  2. Add final with a constructor to set the value for members.使用构造函数添加 final 以设置成员的值。

暂无
暂无

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

相关问题 可序列化类中的非瞬态不可序列化实例字段 - Non-transient non-serializable instance field in serializable class 具有实用程序静态方法的类给出了非瞬态不可序列化的实例字段错误 - Class with utility static method gives Non-transient non-serializable instance field error 如何处理 Findbugs“可序列化类中的非瞬态不可序列化实例字段”? - How to handle a Findbugs “Non-transient non-serializable instance field in serializable class”? 实体 class 定义非瞬态不可序列化实例字段价格 - Entity class defines non-transient non-serializable instance field price 使字段临时进行序列化,但对JPA非临时 - Make a field transient for serialization, but non-transient for JPA 我怎么能不序列化继承的非瞬态字段? - How can I not serialize an inherited non-transient field? 在Java中将对象的临时成员反序列化为非null默认值 - Deserialize a transient member of an object to a non-null default in Java 具有非最终成员的不可变Java类 - Immutable Java class with non-final member 是否可以在Python中创建与类成员持久性状态类似的行为,就像使用Java'transient'关键字那样? - Is it possible to create in Python a similar behaviour to a class member persistency state, as done with Java 'transient' keyword? 非持久字段:JPA'@Transient'与Java SE 7'transient' - Non-persisting fields: JPA '@Transient' vs Java SE 7 'transient'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM