Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Unknown.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import de.fub.bytecode.Constants;
00004 import java.io.*;
00005 import java.util.*;
00006 
00007 /**
00008  * This class is derived from <em>Attribute</em> and represents a reference to an
00009  * unknown (i.e. unimplemented) attribute of this class.
00010  * It is instantiated from the <em>Attribute.readAttribute()</em> method.
00011  *
00012  * @version $Id: Unknown.java,v 1.1.1.1 2002/01/24 03:44:00 pserver Exp $
00013  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00014  * @see     Attribute
00015  */
00016 public final class Unknown extends Attribute {
00017   private byte[] bytes;
00018   private String name;
00019 
00020   private static Hashtable unknown_attributes = new Hashtable();
00021 
00022   /**
00023    * Create a non-standard attribute.
00024    *
00025    * @param name_index Index in constant pool
00026    * @param length Content length in bytes
00027    * @param bytes Attribute contents
00028    * @param constant_pool Array of constants
00029    */
00030   public Unknown(int name_index, int length, byte[] bytes,
00031          ConstantPool constant_pool)
00032   {
00033     super(ATTR_UNKNOWN, name_index, length, constant_pool);
00034     this.bytes = bytes;
00035 
00036     name = ((ConstantUtf8)constant_pool.getConstant(name_index,
00037                             CONSTANT_Utf8)).getBytes();
00038     unknown_attributes.put(name, this);
00039   }  
00040   /**
00041    * Construct object from file stream.
00042    * @param name_index Index in constant pool
00043    * @param length Content length in bytes
00044    * @param file Input stream
00045    * @param constant_pool Array of constants
00046    * @throw IOException
00047    */
00048   Unknown(int name_index, int length, DataInputStream file,
00049       ConstantPool constant_pool)
00050        throws IOException
00051   {
00052     this(name_index, length, (byte [])null, constant_pool);
00053 
00054     if(length > 0) {
00055       bytes = new byte[length];
00056       file.readFully(bytes);
00057     }
00058   }  
00059   /**
00060    * Initialize from another object. Note that both objects use the same
00061    * references (shallow copy). Use clone() for a physical copy.
00062    */
00063   public Unknown(Unknown c) {
00064     this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
00065   }  
00066   /**
00067    * Called by objects that are traversing the nodes of the tree implicitely
00068    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00069    * fields, attributes, etc. spawns a tree of objects.
00070    *
00071    * @param v Visitor object
00072    */
00073   public void accept(Visitor v) {
00074     v.visitUnknown(this);
00075   }  
00076   /**
00077    * @return deep copy of this attribute
00078    */
00079   public Attribute copy(ConstantPool constant_pool) {
00080     Unknown c = (Unknown)clone();
00081 
00082     if(bytes != null)
00083       c.bytes = (byte[])bytes.clone();
00084 
00085     c.constant_pool = constant_pool;
00086     return c;
00087   }  
00088   /**
00089    * Dump unknown bytes to file stream.
00090    *
00091    * @param file Output file stream
00092    * @throw IOException
00093    */ 
00094   public final void dump(DataOutputStream file) throws IOException
00095   {
00096     super.dump(file);
00097     if(length > 0)
00098       file.write(bytes, 0, length);
00099   }  
00100   /**
00101    * @return data bytes.
00102    */  
00103   public final byte[] getBytes() { return bytes; }  
00104   /**
00105    * @return name of attribute.
00106    */  
00107   public final String getName() { return name; }  
00108   /** @return array of unknown attributes, but just one for each kind.
00109    */
00110   static Unknown[] getUnknownAttributes() {
00111     Unknown[]   unknowns = new Unknown[unknown_attributes.size()];
00112     Enumeration entries  = unknown_attributes.elements();
00113 
00114     for(int i=0; entries.hasMoreElements(); i++)
00115       unknowns[i] = (Unknown)entries.nextElement();
00116 
00117     unknown_attributes = new Hashtable();
00118     return unknowns;
00119   }  
00120   /**
00121    * @param bytes.
00122    */
00123   public final void setBytes(byte[] bytes) {
00124     this.bytes = bytes;
00125   }  
00126   /**
00127    * @return String representation.
00128    */ 
00129   public final String toString() {
00130     if(length == 0 || bytes == null)
00131       return "(Unknown attribute " + name + ")";
00132 
00133     String hex;
00134     if(length > 10) {
00135       byte[] tmp = new byte[10];
00136       System.arraycopy(bytes, 0, tmp, 0, 10);
00137       hex = Utility.toHexString(tmp) + "... (truncated)";
00138     }
00139     else
00140       hex = Utility.toHexString(bytes);
00141 
00142     return "(Unknown attribute " + name + ": " + hex + ")";
00143   }  
00144 }

Generated at Thu Feb 7 06:58:58 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001