title: How to exclude field from class serialization in runtime?
body: <p>How to exclude class field from serialization process in runtime ?
  There is <strong>transient</strong> modifier for compilation time but what about runtime ?
  I mean common java serialization with ObjectOutputStream, not gson or something.</p>
  
  <p>Sorry I think I didn't explain right. This is not exactly about serialization , but about <strong>de</strong>-serialization. I have batch of legacy files and handle them like this:</p>
  
  <pre><code>public class Deserialize {
  
  /**
   * @param args
   * @throws IOException 
   * @throws ClassNotFoundException 
   */
  public static void main(String[] args) throws ClassNotFoundException, IOException {
      File file = new File("/home/developer/workspace/DDFS/some.ddf");
      HackedObjectInputStream in = new HackedObjectInputStream(new GZIPInputStream(new FileInputStream(file)));
  
      System.out.println("Attempt to open " + file.getAbsolutePath());
      Object obj = in.readObject();
      in.close();
  
  
  }
  
   static class HackedObjectInputStream extends ObjectInputStream
      {
  
          /**
           * Migration table. Holds old to new classes representation.
           */
          private static final Map&lt;String, Class&lt;?&gt;&gt; MIGRATION_MAP = new HashMap&lt;String, Class&lt;?&gt;&gt;();
  
          static
          {
              MIGRATION_MAP.put("DBOBExit", Exit.class);
          }
  
          /**
           * Constructor.
           * @param stream input stream
           * @throws IOException if io error
           */
          public HackedObjectInputStream(final InputStream stream) throws IOException
          {
              super(stream);
          }
  
          @Override
          protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException
          {
              ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
  
              for (final String oldName : MIGRATION_MAP.keySet())
              {
                  if (resultClassDescriptor.getName().equals(oldName))
                  {
                      resultClassDescriptor = ObjectStreamClass.lookup(MIGRATION_MAP.get(oldName));   
                  }
              }
  
              return resultClassDescriptor;
          }
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>This code works fine for most of files , but some files throws </p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: cannot assign instance of java.awt.Polygon to field Exit.msgbackPt of type java.awt.Point in instance of Exit
  at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2053)
  </code></pre>
  
  <p>because of different versions of Exit class . New version has new fields.
  Error disappearing when I add transient to new fields, but another files starts to throwing an exception (latest files). </p>
  
  <p>So can I add transient to these new fileds in runtime if I detect legacy serilized file ?
  Maybe reflection or something ?</p>