有没有办法在java中检查对象是否可序列化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20286340/
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
Is there any way to check whether an object is serializable or not in java?
提问by Jaykishan
I found that Object class
in java is not serializable in java at the end when i've wasted much time for a problem.
So can anybody knows another class's those are not serializable orany way to check whether that class is serializable?
Object class
当我为一个问题浪费了很多时间时,我发现最后在 java 中无法在 java 中序列化。
那么任何人都可以知道另一个类的那些不可序列化或任何方法来检查该类是否可序列化?
采纳答案by René Link
Use
用
if(someObj instanceof Serializable) // recommended because it uses
// the byte code instruction INSTANCEOF
or
或者
if(Serializable.class.isInstance(someObj))
Using Class.isInstance(someObj)
makes sense if the Class
should be replaceable at runtime.
Class.isInstance(someObj)
如果Class
应该在运行时可替换,则使用是有意义的。
For example:
例如:
Class<?> someClass == ....; // assign a class object reference dynamically
if(someClass.isInstance(someObj))
回答by Dragondraikk
You can check for
你可以检查
someObject instanceof Serializable
This will evaluate to true
if someObject
implements the Serializable
interface
这将评估true
是否someObject
实现了Serializable
接口
回答by m0skit0
Yes
是的
if (yourObjectInstance instanceof Serializable) {
// It is
} else {
// It is not
}
Note that if yourObjectInstance
is null
, that would enter the else
part as null
is not Serializable
, no matter what class the reference is about.
请注意,如果yourObjectInstance
是null
,则无论引用是关于什么类,都将输入else
部分作为null
不是Serializable
。
Also as Victor Sorokin points out, having a class implements Serializable
doesn't mean it can actually be serialized.
同样正如 Victor Sorokin 指出的那样,拥有一个类实现Serializable
并不意味着它实际上可以被序列化。
回答by gyorgyabraham
Some people suggested instanceofoperator but it does another thing: it returns false if the reference is null, even if the class implements Serializable!
And this also applies to Class.isInstance(Object obj)
有些人建议使用instanceof运算符,但它做另一件事:如果引用为 null 则返回 false,即使类实现了 Serializable !这也适用于Class.isInstance(Object obj)
回答by Victor Sorokin
Using just instanceof
not 100% reliable, as following code demonstrates. Your best bet is to examine sources of classes you try to marshal, if you have them, or, if not, you may hope
class vendor got this thing right.
使用instanceof
不是 100% 可靠,如下面的代码所示。最好的办法是检查您尝试编组的类的源(如果有),或者,如果没有,您可能希望类供应商把这件事做对了。
class A {
final int field;
/*
// uncomment this ctor to make class "more" serializable
A() {
this.field = -1;
}
*/
A(int field) {
this.field = field;
}
}
class B extends A implements Serializable {
B(int field) {
super(field);
}
public String toString() {
return "B{field=" + field + "}";
}
}
class Test {
public static void main(String[] args) {
System.out.println(Serializable.class.isAssignableFrom(B.class));
B b = new B(11);
try {
ByteArrayOutputStream bf = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bf);
oos.writeObject(b);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bf.toByteArray()));
Object o = ois.readObject();
System.out.println(o.toString());
} catch (Exception e) {
System.out.println("Not exactly Serializable");
e.printStackTrace();
}
}
}
回答by vivek bhoraniya
you can check class is searializable or not:
您可以检查类是否可序列化:
if(someObj instanceof Serializable)
回答by mykey
If an object is serializable, you should be able to convert it to a byte array. So you can use this test method and make sure that it does not throw an exception when you serialize it.
如果对象是可序列化的,您应该能够将其转换为字节数组。所以你可以使用这个测试方法,并确保它在序列化时不会抛出异常。
@Test
public void testIfYourClassIsSerilaizable() {
boolean exceptionThrown = false;
try {
YourClass obj = new YourClass();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
byte [] data = bos.toByteArray();
} catch(IOException ex) {
exceptionThrown = true;
}
Assert.assertFalse(exceptionThrown);
}
So based on that , if YourClass or its attributes do not implement Serializable, the above test will throw an exception, making the class not serializable.
因此基于此,如果 YourClass 或其属性没有实现 Serializable,则上述测试将抛出异常,使该类不可序列化。