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

ArrayType.java

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

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