diff src/clojure/asm/Item.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/asm/Item.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,258 @@
     1.4 +/***
     1.5 + * ASM: a very small and fast Java bytecode manipulation framework
     1.6 + * Copyright (c) 2000-2005 INRIA, France Telecom
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 3. Neither the name of the copyright holders nor the names of its
    1.18 + *    contributors may be used to endorse or promote products derived from
    1.19 + *    this software without specific prior written permission.
    1.20 + *
    1.21 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.25 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    1.31 + * THE POSSIBILITY OF SUCH DAMAGE.
    1.32 + */
    1.33 +package clojure.asm;
    1.34 +
    1.35 +/**
    1.36 + * A constant pool item. Constant pool items can be created with the 'newXXX'
    1.37 + * methods in the {@link ClassWriter} class.
    1.38 + *
    1.39 + * @author Eric Bruneton
    1.40 + */
    1.41 +final class Item{
    1.42 +
    1.43 +/**
    1.44 + * Index of this item in the constant pool.
    1.45 + */
    1.46 +int index;
    1.47 +
    1.48 +/**
    1.49 + * Type of this constant pool item. A single class is used to represent all
    1.50 + * constant pool item types, in order to minimize the bytecode size of this
    1.51 + * package. The value of this field is one of {@link ClassWriter#INT},
    1.52 + * {@link ClassWriter#LONG}, {@link ClassWriter#FLOAT},
    1.53 + * {@link ClassWriter#DOUBLE}, {@link ClassWriter#UTF8},
    1.54 + * {@link ClassWriter#STR}, {@link ClassWriter#CLASS},
    1.55 + * {@link ClassWriter#NAME_TYPE}, {@link ClassWriter#FIELD},
    1.56 + * {@link ClassWriter#METH}, {@link ClassWriter#IMETH}.
    1.57 + * <p/>
    1.58 + * Special Item types are used for Items that are stored in the ClassWriter
    1.59 + * {@link ClassWriter#typeTable}, instead of the constant pool, in order to
    1.60 + * avoid clashes with normal constant pool items in the ClassWriter constant
    1.61 + * pool's hash table. These special item types are
    1.62 + * {@link ClassWriter#TYPE_NORMAL}, {@link ClassWriter#TYPE_UNINIT} and
    1.63 + * {@link ClassWriter#TYPE_MERGED}.
    1.64 + */
    1.65 +int type;
    1.66 +
    1.67 +/**
    1.68 + * Value of this item, for an integer item.
    1.69 + */
    1.70 +int intVal;
    1.71 +
    1.72 +/**
    1.73 + * Value of this item, for a long item.
    1.74 + */
    1.75 +long longVal;
    1.76 +
    1.77 +/**
    1.78 + * First part of the value of this item, for items that do not hold a
    1.79 + * primitive value.
    1.80 + */
    1.81 +String strVal1;
    1.82 +
    1.83 +/**
    1.84 + * Second part of the value of this item, for items that do not hold a
    1.85 + * primitive value.
    1.86 + */
    1.87 +String strVal2;
    1.88 +
    1.89 +/**
    1.90 + * Third part of the value of this item, for items that do not hold a
    1.91 + * primitive value.
    1.92 + */
    1.93 +String strVal3;
    1.94 +
    1.95 +/**
    1.96 + * The hash code value of this constant pool item.
    1.97 + */
    1.98 +int hashCode;
    1.99 +
   1.100 +/**
   1.101 + * Link to another constant pool item, used for collision lists in the
   1.102 + * constant pool's hash table.
   1.103 + */
   1.104 +Item next;
   1.105 +
   1.106 +/**
   1.107 + * Constructs an uninitialized {@link Item}.
   1.108 + */
   1.109 +Item(){
   1.110 +}
   1.111 +
   1.112 +/**
   1.113 + * Constructs an uninitialized {@link Item} for constant pool element at
   1.114 + * given position.
   1.115 + *
   1.116 + * @param index index of the item to be constructed.
   1.117 + */
   1.118 +Item(final int index){
   1.119 +	this.index = index;
   1.120 +}
   1.121 +
   1.122 +/**
   1.123 + * Constructs a copy of the given item.
   1.124 + *
   1.125 + * @param index index of the item to be constructed.
   1.126 + * @param i     the item that must be copied into the item to be constructed.
   1.127 + */
   1.128 +Item(final int index, final Item i){
   1.129 +	this.index = index;
   1.130 +	type = i.type;
   1.131 +	intVal = i.intVal;
   1.132 +	longVal = i.longVal;
   1.133 +	strVal1 = i.strVal1;
   1.134 +	strVal2 = i.strVal2;
   1.135 +	strVal3 = i.strVal3;
   1.136 +	hashCode = i.hashCode;
   1.137 +}
   1.138 +
   1.139 +/**
   1.140 + * Sets this item to an integer item.
   1.141 + *
   1.142 + * @param intVal the value of this item.
   1.143 + */
   1.144 +void set(final int intVal){
   1.145 +	this.type = ClassWriter.INT;
   1.146 +	this.intVal = intVal;
   1.147 +	this.hashCode = 0x7FFFFFFF & (type + intVal);
   1.148 +}
   1.149 +
   1.150 +/**
   1.151 + * Sets this item to a long item.
   1.152 + *
   1.153 + * @param longVal the value of this item.
   1.154 + */
   1.155 +void set(final long longVal){
   1.156 +	this.type = ClassWriter.LONG;
   1.157 +	this.longVal = longVal;
   1.158 +	this.hashCode = 0x7FFFFFFF & (type + (int) longVal);
   1.159 +}
   1.160 +
   1.161 +/**
   1.162 + * Sets this item to a float item.
   1.163 + *
   1.164 + * @param floatVal the value of this item.
   1.165 + */
   1.166 +void set(final float floatVal){
   1.167 +	this.type = ClassWriter.FLOAT;
   1.168 +	this.intVal = Float.floatToRawIntBits(floatVal);
   1.169 +	this.hashCode = 0x7FFFFFFF & (type + (int) floatVal);
   1.170 +}
   1.171 +
   1.172 +/**
   1.173 + * Sets this item to a double item.
   1.174 + *
   1.175 + * @param doubleVal the value of this item.
   1.176 + */
   1.177 +void set(final double doubleVal){
   1.178 +	this.type = ClassWriter.DOUBLE;
   1.179 +	this.longVal = Double.doubleToRawLongBits(doubleVal);
   1.180 +	this.hashCode = 0x7FFFFFFF & (type + (int) doubleVal);
   1.181 +}
   1.182 +
   1.183 +/**
   1.184 + * Sets this item to an item that do not hold a primitive value.
   1.185 + *
   1.186 + * @param type    the type of this item.
   1.187 + * @param strVal1 first part of the value of this item.
   1.188 + * @param strVal2 second part of the value of this item.
   1.189 + * @param strVal3 third part of the value of this item.
   1.190 + */
   1.191 +void set(
   1.192 +		final int type,
   1.193 +		final String strVal1,
   1.194 +		final String strVal2,
   1.195 +		final String strVal3){
   1.196 +	this.type = type;
   1.197 +	this.strVal1 = strVal1;
   1.198 +	this.strVal2 = strVal2;
   1.199 +	this.strVal3 = strVal3;
   1.200 +	switch(type)
   1.201 +		{
   1.202 +		case ClassWriter.UTF8:
   1.203 +		case ClassWriter.STR:
   1.204 +		case ClassWriter.CLASS:
   1.205 +		case ClassWriter.TYPE_NORMAL:
   1.206 +			hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
   1.207 +			return;
   1.208 +		case ClassWriter.NAME_TYPE:
   1.209 +			hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
   1.210 +			                                * strVal2.hashCode());
   1.211 +			return;
   1.212 +			// ClassWriter.FIELD:
   1.213 +			// ClassWriter.METH:
   1.214 +			// ClassWriter.IMETH:
   1.215 +		default:
   1.216 +			hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
   1.217 +			                                * strVal2.hashCode() * strVal3.hashCode());
   1.218 +		}
   1.219 +}
   1.220 +
   1.221 +/**
   1.222 + * Indicates if the given item is equal to this one.
   1.223 + *
   1.224 + * @param i the item to be compared to this one.
   1.225 + * @return <tt>true</tt> if the given item if equal to this one,
   1.226 + *         <tt>false</tt> otherwise.
   1.227 + */
   1.228 +boolean isEqualTo(final Item i){
   1.229 +	if(i.type == type)
   1.230 +		{
   1.231 +		switch(type)
   1.232 +			{
   1.233 +			case ClassWriter.INT:
   1.234 +			case ClassWriter.FLOAT:
   1.235 +				return i.intVal == intVal;
   1.236 +			case ClassWriter.TYPE_MERGED:
   1.237 +			case ClassWriter.LONG:
   1.238 +			case ClassWriter.DOUBLE:
   1.239 +				return i.longVal == longVal;
   1.240 +			case ClassWriter.UTF8:
   1.241 +			case ClassWriter.STR:
   1.242 +			case ClassWriter.CLASS:
   1.243 +			case ClassWriter.TYPE_NORMAL:
   1.244 +				return i.strVal1.equals(strVal1);
   1.245 +			case ClassWriter.TYPE_UNINIT:
   1.246 +				return i.intVal == intVal && i.strVal1.equals(strVal1);
   1.247 +			case ClassWriter.NAME_TYPE:
   1.248 +				return i.strVal1.equals(strVal1)
   1.249 +				       && i.strVal2.equals(strVal2);
   1.250 +				// ClassWriter.FIELD:
   1.251 +				// ClassWriter.METH:
   1.252 +				// ClassWriter.IMETH:
   1.253 +			default:
   1.254 +				return i.strVal1.equals(strVal1)
   1.255 +				       && i.strVal2.equals(strVal2)
   1.256 +				       && i.strVal3.equals(strVal3);
   1.257 +			}
   1.258 +		}
   1.259 +	return false;
   1.260 +}
   1.261 +}