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

ArrayType.java

00001 package de.fub.bytecode.generic;
00002 
00003 import de.fub.bytecode.Constants;
00004 
00005 /** 
00006  * Denotes array type, such as int[][]
00007  *
00008  * @version $Id: ArrayType.java,v 1.1.1.1 2002/01/24 03:41:38 pserver Exp $
00009  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00010  */
00011 public final class ArrayType extends ReferenceType {
00012   private int  dimensions;
00013   private Type basic_type;
00014 
00015   /**
00016    * Convenience constructor for array type, e.g. int[]
00017    *
00018    * @param type array type, e.g. T_INT
00019    */ 
00020   public ArrayType(byte type, int dimensions) {
00021     this(BasicType.getType(type), dimensions);
00022   }  
00023   /**
00024    * Constructor for array of given type
00025    *
00026    * @param type type of array (may be an array itself)
00027    */ 
00028   public ArrayType(Type type, int dimensions) {
00029     super(Constants.T_ARRAY, "<dummy>");
00030 
00031     if((dimensions < 1) || (dimensions > Constants.MAX_BYTE))
00032       throw new ClassGenException("Invalid number of dimensions: " + dimensions);
00033 
00034     switch(type.getType()) {
00035     case Constants.T_ARRAY:
00036       ArrayType array = (ArrayType)type;
00037       this.dimensions = dimensions + array.dimensions;
00038       basic_type      = array.basic_type;
00039       break;
00040       
00041     case Constants.T_VOID:
00042       throw new ClassGenException("Invalid type: void[]");
00043 
00044     default: // Basic type or reference
00045       this.dimensions = dimensions;
00046       basic_type = type;
00047       break;
00048     }
00049 
00050     StringBuffer buf = new StringBuffer();
00051     for(int i=0; i < this.dimensions; i++)
00052       buf.append('[');
00053 
00054     buf.append(basic_type.getSignature());
00055 
00056     signature = buf.toString();
00057   }  
00058   /**
00059    * Convenience constructor for reference array type, e.g. Object[]
00060    *
00061    * @param class_name complete name of class (java.lang.String, e.g.)
00062    */ 
00063   public ArrayType(String class_name, int dimensions) {
00064     this(new ObjectType(class_name), dimensions);
00065   }  
00066   /** @return true if both type objects refer to the same array type.
00067    */
00068   public boolean equals(Object type) {
00069     if(type instanceof ArrayType) {
00070       ArrayType array = (ArrayType)type;
00071       return (array.dimensions == dimensions) && array.basic_type.equals(basic_type);
00072     } else
00073       return false;
00074   }  
00075   /**
00076    * @return basic type of array, i.e., for int[][][] the basic type is int
00077    */
00078   public Type getBasicType() {
00079     return basic_type;
00080   }  
00081   /** @return number of dimensions of array
00082    */
00083   public int getDimensions() { return dimensions; }  
00084   /**
00085    * @return element type of array, i.e., for int[][][] the element type is int[][]
00086    */
00087   public Type getElementType() {
00088     if(dimensions == 1)
00089       return basic_type;
00090     else
00091       return new ArrayType(basic_type, dimensions - 1);
00092   }  
00093   /** @return a hash code value for the object.
00094    */
00095   public int hashcode() { return basic_type.hashCode() ^ dimensions; }  
00096 }

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