JDK-6226715 : (ann) java.lang.annotation.AnnotationTypeMismatchException could not be serialized
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-02-08
  • Updated: 2017-05-16
  • Resolved: 2016-07-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b127Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Description
The instance of the java.lang.annotation.AnnotationTypeMismatchException class 
could not be serialized because the AnnotationTypeMismatchException class has 
non-transient and non-static field 'element' of non-serializable 
class java.lang.reflect.Method. 
 
The minimized test to reproduce the bug:
--- Test.java ---
import java.lang.annotation.AnnotationTypeMismatchException;
import java.lang.reflect.Method;
import java.io.*;

public class Test {

    public static void main(String argv[]) throws Exception {
        Method[] meths = MyClass.class.getDeclaredMethods();
        System.out.println("meths[0]: " + meths[0].toString());

        AnnotationTypeMismatchException obj =
                new AnnotationTypeMismatchException(meths[0], "foundType");
        
        ObjectOutputStream oos = new ObjectOutputStream(
	        new FileOutputStream("test.ser"));
        System.out.println("Serializing AnnotationTypeMismatchException ...");
        oos.writeObject(obj);
	oos.close();
        System.out.println("PASSED.");
    }
    
}

class MyClass {

    public MyClass() {
        super();
    }
    
    public String dummyMethod() {
        return "";
    }

}
---  end-of-Test.java ---


Test output:
-----------
<gyi@mars> java Test
meths[0]: public java.lang.String MyClass.dummyMethod()
Serializing AnnotationTypeMismatchException ...
Exception in thread "main" java.io.NotSerializableException: java.lang.reflect.Method
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
        at Test.main(Test.java:16)

###@###.### 2005-2-08 16:53:49 GMT

Comments
Review thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-June/042132.html
30-06-2016

EVALUATION Just as a Vector containing nonserializable objects cannot be serialized despite Vector implementing Serializable, an exception containing nonserializable objects cannot be serialized. This exception contains a Method object, which is nonserializable. The spec is not explicit about what happens if the Method object is null. One possible fix would be to explicitly allow null, and make use of this when serializing the exception. ###@###.### 2005-2-08 23:13:46 GMT
08-02-2005