home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / DUMPCLAS.TAR / util / FieldInfo.java < prev    next >
Encoding:
Java Source  |  1996-05-22  |  3.2 KB  |  105 lines

  1. /*
  2.  * @(#)FieldInfo.java    1.3 95/08/16 Chuck McManis
  3.  *
  4.  * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies.
  10.  *
  11.  * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  12.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  14.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
  15.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  16.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  17.  */
  18.  
  19. package util;
  20.  
  21. import java.io.DataInputStream;
  22. import java.io.DataOutputStream;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * This class defines a FieldInfo in the class file. Fields are used to
  27.  * describe instance variables in a class. The toString() method is
  28.  * augmented by a version that takes an array of ConstantPoolInfo
  29.  * objects (a constant pool). When a constant pool is available the
  30.  * toString() method generates a declaration for the field as it would
  31.  * appear in Java source.
  32.  *
  33.  * @version     1.3, 16 Aug 1995
  34.  * @author    Chuck McManis
  35.  * @see        ClassFile
  36.  */
  37.  
  38. public class FieldInfo {
  39.     short        accessFlags;
  40.     ConstantPoolInfo    name;
  41.     ConstantPoolInfo    signature;
  42.     AttributeInfo    attributes[];
  43.  
  44.     public boolean read(DataInputStream di, ConstantPoolInfo pool[])
  45.     throws IOException {
  46.     int    count;
  47.  
  48.     accessFlags = di.readShort();
  49.     name = pool[di.readShort()];
  50.     signature = pool[di.readShort()];
  51.     count = di.readShort();
  52.     if (count != 0) {
  53.         attributes = new AttributeInfo[count];
  54.         for (int i = 0; i < count; i++) {
  55.         attributes[i] = new AttributeInfo();
  56.         if (! attributes[i].read(di, pool))
  57.             return (false);
  58.         }
  59.     }
  60.     return (true);
  61.     }
  62.  
  63.     public void write(DataOutputStream dos, ConstantPoolInfo pool[])
  64.     throws IOException, Exception {
  65.     dos.writeShort(accessFlags);
  66.     dos.writeShort(ConstantPoolInfo.indexOf(name, pool));
  67.     dos.writeShort(ConstantPoolInfo.indexOf(signature, pool));
  68.     if (attributes == null) {
  69.         dos.writeShort(0);
  70.     } else {
  71.         dos.writeShort(attributes.length);
  72.         for (int i = 0; i < attributes.length; i++) {
  73.             attributes[i].write(dos, pool);
  74.         }
  75.     }
  76.     }
  77.  
  78.     public String toString() {
  79.     StringBuffer x = new StringBuffer();
  80.  
  81.     x.append(ClassFile.accessString(accessFlags));
  82.     x.append(ClassFile.typeString(signature.toString(), name.toString()));
  83.     if (attributes != null) {
  84.         x.append(" = "+attributes[0].toString());
  85.     }
  86.     return (x.toString());
  87.     }
  88.  
  89.     public String toString(ConstantPoolInfo pool[]) {
  90.     StringBuffer x = new StringBuffer();
  91.     String    mytype;
  92.  
  93.     x.append(ClassFile.accessString(accessFlags));
  94.     mytype = ClassFile.typeString(signature.toString(), name.toString());
  95.     x.append(mytype);
  96.     if (attributes != null) {
  97.         if (mytype.startsWith("boolean")) {
  98.         x.append(" "+attributes[0].toBoolean(pool));
  99.         } else
  100.             x.append(" "+attributes[0].toString(pool));
  101.     }
  102.     return (x.toString());
  103.     }
  104. }
  105.