Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Java class is instance of Serializable

I wonder when Java Class is instance of Serializable. As I know class is serializable only if it implements Serializable interface. I'm trying to use junit to generate entity class (from some kind of template) and check if it is serializable.

My generated class (which does NOT implement Serializable) looks like:

package test;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.annotation.Generated;
import javax.persistence.Id;


@Entity
@Table( name = MyTestTableDto.TABLE_NAME )
public class MyTestTableDto 
{
    public static final String TABLE_NAME = "MY_TEST_TABLE";

    public static final String COLUMN_MY_ID_FIELD = "MY_ID_FIELD";
    public static final String FIELD_MY_ID_FIELD = "myIdField";

    @Id
    @Column( nullable = true, length = 14, name = COLUMN_MY_ID_FIELD )
    private Long myIdField;

    public Long getMyIdField()
    {
        return myIdField;
    }

    public void setMyIdField( Long aMyIdField )
    {
        this.myIdField = aMyIdField;
    }

}

Test is below:

 File generatedFile = new File( GEN_OUTPUT_DIR, File.separator + className + ".java" );

 assertTrue( generatedFile.getClass() instanceof Serializable ); //returns true

The result suggest that my generated class is instance of Serializable. My question is why? I think if it does not implements Serializable it should not be instance of it. I searched for an answer but I couldnt find anything.

like image 490
dzuma Avatar asked Oct 07 '13 11:10

dzuma


People also ask

When should a Java class implement serializable?

So, implement the Serializable interface when you need to store a copy of the object, send them to another process which runs on the same system or over the network. Because you want to store or send an object. It makes storing and sending objects easy. It has nothing to do with security.

How do you check if a class is serializable?

If you are curious to know if a Java Standard Class is serializable or not, check the documentation for the class. The test is simple: If the class implements java. io. Serializable, then it is serializable; otherwise, it's not.

What does it mean if a class is serializable?

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.

What happens if a class implements serializable?

If a super class implements Serializable, then its sub classes do automatically. When an instance of a serializable class is deserialized, the constructor doesn't run. If a super class doesn't implement Serializable, then when a subclass object is deserialized, the super class constructor will run.


3 Answers

You do this:

File generatedFile = new File(...);

This created an instance of the class File.

generatedFile.getClass()

This got the Class object for the File object, an object of type Class<File>

The documentation of File describes the class:

All Implemented Interfaces: Serializable, Comparable

If you'd like to achieve that you want to parse a java source file to see if it implements Serializable, you should get an appropriate tool for this, build the abstract syntax tree of the file in question, and extract the information from there.

Or you could just do it like this, using the isAssignableFrom method of the Class class:

public boolean checkSerializable(Class<?> classToCheck) {
    return Serializable.class.isAssignableFrom(classToCheck);
}

And then check the class itself:

boolean isSerializable = checkSerializable(MyTestTableDto.class);
like image 59
ppeterka Avatar answered Oct 06 '22 18:10

ppeterka


This is because you call instanceof on Class object, which is serializable.

getClass returns a Class<T> object, where T is the type of the class. Because Class is defined as

public final
class Class<T> implements java.io.Serializable,
                          java.lang.reflect.GenericDeclaration,
                          java.lang.reflect.Type,
                          java.lang.reflect.AnnotatedElement 
{
  ...
}

it's clearly serializable.

like image 3
Danstahr Avatar answered Oct 06 '22 17:10

Danstahr


The statement

assertTrue( generatedFile.getClass() instanceof Serializable );

is returning true, because this will check whether the runtime class of this generatedFile Object is an instance of Serializable or not. And the class object is serializable.

like image 2
Debojit Saikia Avatar answered Oct 06 '22 17:10

Debojit Saikia