/[classpath]/classpath/java/io/ObjectStreamClass.java
ViewVC logotype

Diff of /classpath/java/io/ObjectStreamClass.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.24 by mkoch, Sun Oct 12 16:53:14 2003 UTC revision 1.25 by glavaux, Tue Dec 2 18:35:52 2003 UTC
# Line 291  public class ObjectStreamClass implement Line 291  public class ObjectStreamClass implement
291      this.fields = fields;      this.fields = fields;
292    }    }
293    
294    void setClass (Class cl) throws InvalidClassException    /**
295       * This method builds the internal description corresponding to a Java Class.
296       * As the constructor only assign a name to the current ObjectStreamClass instance,
297       * that method sets the serial UID, chose the fields which will be serialized,
298       * and compute the position of the fields in the serialized stream.
299       *
300       * @param cl The Java class which is used as a reference for building the descriptor.
301       * @param superClass The descriptor of the super class for this class descriptor.
302       * @throws InvalidClassException if an incompatibility between computed UID and
303       * already set UID is found.
304       */
305      void setClass (Class cl, ObjectStreamClass superClass) throws InvalidClassException
306    {    {
307      this.clazz = cl;      this.clazz = cl;
308    
# Line 312  public class ObjectStreamClass implement Line 323  public class ObjectStreamClass implement
323        }        }
324    
325      isProxyClass = clazz != null && Proxy.isProxyClass (clazz);      isProxyClass = clazz != null && Proxy.isProxyClass (clazz);
326      ObjectStreamClass osc = (ObjectStreamClass)classLookupTable.get (clazz);      this.superClass = superClass;
     if (osc == null)  
       classLookupTable.put (clazz, this);  
     superClass = lookupForClassObject (clazz.getSuperclass ());  
327      calculateOffsets ();      calculateOffsets ();
328        
329        try
330          {
331            ObjectStreamField[] exportedFields = getSerialPersistentFields (clazz);  
332    
333            if (exportedFields == null)
334              return;
335    
336            ObjectStreamField[] newFieldList = new ObjectStreamField[exportedFields.length + fields.length];
337            int i, j, k;
338    
339            /* We now check the import fields against the exported fields.
340             * There should not be contradiction (e.g. int x and String x)
341             * but extra virtual fields can be added to the class.
342             */
343    
344            Arrays.sort(exportedFields);
345    
346            i = 0; j = 0; k = 0;
347            while (i < fields.length && j < exportedFields.length)
348            {
349              int comp = fields[i].getName().compareTo (exportedFields[j].getName());
350    
351              if (comp < 0)
352                {
353                  newFieldList[k] = fields[i];
354                  fields[i].setPersistent(false);
355                  fields[i].setToSet(false);
356                  i++;
357                }
358              else if (comp > 0)
359                {
360                  /* field not found in imported fields. We add it
361                   * in the list of supported fields.
362                   */
363                  newFieldList[k] = exportedFields[j];
364                  newFieldList[k].setPersistent(true);
365                  newFieldList[k].setToSet(false);
366                  j++;
367                }
368              else
369                {
370                  if (!fields[i].getType().equals (exportedFields[j].getType()))
371                    throw new InvalidClassException (
372                                "serialPersistentFields must be compatible with" +
373                                " imported fields (about " + fields[i].getName() + ")");
374                  newFieldList[k] = fields[i];
375                  fields[i].setPersistent(true);
376                  i++;
377                  j++;
378                }
379              k++;
380            }
381    
382            if (i < fields.length)
383              for (;i<fields.length;i++,k++)
384              {
385                fields[i].setPersistent(false);
386                fields[i].setToSet(false);
387                newFieldList[k] = fields[i];
388              }
389            else
390              if (j < exportedFields.length)
391                for (;j<exportedFields.length;j++,k++)
392                  {
393                    exportedFields[j].setPersistent(true);
394                    exportedFields[j].setToSet(false);
395                    newFieldList[k] = exportedFields[j];
396                  }
397            
398            fields = new ObjectStreamField[k];
399            System.arraycopy (newFieldList, 0, fields, 0, k);
400          }
401        catch (NoSuchFieldException ignore)
402          {
403            return;
404          }
405        catch (IllegalAccessException ignore)
406          {
407            return;
408          }
409    }    }
410    
411    void setSuperclass (ObjectStreamClass osc)    void setSuperclass (ObjectStreamClass osc)
# Line 435  public class ObjectStreamClass implement Line 524  public class ObjectStreamClass implement
524        }        }
525      }      }
526      catch (NoSuchFieldException ignore)      catch (NoSuchFieldException ignore)
527      {}        {}
528        catch (IllegalAccessException ignore)
529          {}
530    
531      int num_good_fields = 0;      int num_good_fields = 0;
532      Field[] all_fields = cl.getDeclaredFields ();      Field[] all_fields = cl.getDeclaredFields ();
# Line 612  public class ObjectStreamClass implement Line 703  public class ObjectStreamClass implement
703    
704    // Returns the value of CLAZZ's private static final field named    // Returns the value of CLAZZ's private static final field named
705    // `serialPersistentFields'.    // `serialPersistentFields'.
706    private ObjectStreamField[] getSerialPersistentFields (Class clazz)    private ObjectStreamField[] getSerialPersistentFields (Class clazz)
707        throws NoSuchFieldException, IllegalAccessException
708    {    {
709      ObjectStreamField[] o = null;      ObjectStreamField[] o = null;
     try  
       {  
         // Use getDeclaredField rather than getField for the same reason  
         // as above in getDefinedSUID.  
         Field f = clazz.getDeclaredField ("serialPersistentFields");  
         f.setAccessible(true);  
         o = (ObjectStreamField[])f.get (null);  
       }  
     catch (java.lang.NoSuchFieldException e)  
       {  
       }  
     catch (java.lang.IllegalAccessException e)  
       {  
       }  
710    
711      return o;      // Use getDeclaredField rather than getField for the same reason
712        // as above in getDefinedSUID.
713        Field f = clazz.getDeclaredField ("serialPersistentFields");
714        f.setAccessible(true);
715        return (ObjectStreamField[])f.get (null);
716    }    }
717    
718    public static final ObjectStreamField[] NO_FIELDS = {};    public static final ObjectStreamField[] NO_FIELDS = {};

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.25

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26