When Java class is instance of Serializable -
i wonder when java class instance of serializable. know class serializable if implements serializable interface. i'm trying use junit generate entity class (from kind of template) , check if serializable.
my generated class (which 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 below:
file generatedfile = new file( gen_output_dir, file.separator + classname + ".java" ); asserttrue( generatedfile.getclass() instanceof serializable ); //returns true
the result suggest generated class instance of serializable. question why? think if not implements serializable should not instance of it. searched answer couldnt find anything.
you this:
file generatedfile = new file(...);
this created instance of class file
.
generatedfile.getclass()
this got class
object file
object, object of type class<file>
the documentation of file describes class:
all implemented interfaces: serializable, comparable
if you'd achieve want parse java source file see if implements serializable, should appropriate tool this, build abstract syntax tree of file in question, , extract information there.
or this, using isassignablefrom method of class class:
public boolean checkserializable(class<?> classtocheck) { return serializable.class.isassignablefrom(classtocheck); }
and check class itself:
boolean isserializable = checkserializable(mytesttabledto.class);
Comments
Post a Comment