Ability to automatically grant Serializable to all fields recursively

Asked 2 years ago, Updated 2 years ago, 34 views

If you want the Java class to be Serializable, you need to recursively add Serializable to all existing fields. Is there any IDE functionality or tool to do this automatically?

java

2022-09-30 20:18

3 Answers

It's not a direct answer, but I thought it might be misunderstood, so I'll write it down.

All existing fields must be recursively serializable

is sufficient, but not necessary.

For example, if you have a Parent with the following Child type in the field,

class Parent implements Serializable {

    private child;

    // ...
}

US>class Child {
}

Child type is not Serializable, so the Parent is actually non-serial, not .

If the child field contains the following types of instances, it can be serialized:

class SerializableChild extensions Child implements Serializable {
}

Alternatively, child can be serialized even if null.

On the other hand, serialization fails if the following types of instances are set:

class NonSerializableChild extensions Child {
}

(Executable sample code: http://ideone.com/2ER9Dm)

Javadoc calls it

Class serialization is enabled by the class that implements the java.io.Serializable interface.In classes that do not implement this interface, the state is not serialized or restored in series.
(omitted)
You might encounter objects that do not support serializable interfaces while patrolling graphs.In this case, a NotSerializableException is thrown, and this exception identifies the class of non-serial objects.

Translating to make it easier to understand

  • classis not serializable or ignored during serialization or de-serialization (see Javadoc's subsequent text for details)
  • Objectis not Serializable or an exception is thrown at runtime

That's what it means.


2022-09-30 20:18

You do not need to declare Serializable in the fields of the class implemented (implementations).

In other words, the fields of a class implemented with Serializable must be primitive or serializable.

Are you looking for Edia who can:

public class Sample implementable{
    public Child;
}

After creating this Sample class, if you try to create a Child class,

public class Child [implementations Serializable This Is Automatically Created] {

}

Is that so?


2022-09-30 20:18

It doesn't seem to be there, so I tried to make something easy by myself.
For now, I only need to serialize all classes under a folder, but sometimes a slightly complicated class doesn't work.

https://github.com/lamrongol/MakeJavaClassSerializable


2022-09-30 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.