jaxp/src/com/sun/org/apache/bcel/internal/generic/InstructionHandle.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/bcel/internal/generic/InstructionHandle.java@7f561c08de6b
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
package com.sun.org.apache.bcel.internal.generic;
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
/* ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * The Apache Software License, Version 1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * Redistribution and use in source and binary forms, with or without
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * modification, are permitted provided that the following conditions
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * are met:
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * 1. Redistributions of source code must retain the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 *    notice, this list of conditions and the following disclaimer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 * 2. Redistributions in binary form must reproduce the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 *    notice, this list of conditions and the following disclaimer in
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 *    the documentation and/or other materials provided with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 *    distribution.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
 * 3. The end-user documentation included with the redistribution,
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
 *    if any, must include the following acknowledgment:
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
 *       "This product includes software developed by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 *        Apache Software Foundation (http://www.apache.org/)."
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 *    Alternately, this acknowledgment may appear in the software itself,
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 *    if and wherever such third-party acknowledgments normally appear.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * 4. The names "Apache" and "Apache Software Foundation" and
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *    "Apache BCEL" must not be used to endorse or promote products
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *    derived from this software without prior written permission. For
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *    written permission, please contact apache@apache.org.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * 5. Products derived from this software may not be called "Apache",
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *    "Apache BCEL", nor may "Apache" appear in their name, without
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *    prior written permission of the Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * SUCH DAMAGE.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * This software consists of voluntary contributions made by many
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * individuals on behalf of the Apache Software Foundation.  For more
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * information on the Apache Software Foundation, please see
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * <http://www.apache.org/>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
import com.sun.org.apache.bcel.internal.classfile.Utility;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
import java.util.HashSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import java.util.Collection;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
import java.util.HashMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * Instances of this class give users a handle to the instructions contained in
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * an InstructionList. Instruction objects may be used more than once within a
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * list, this is useful because it saves memory and may be much faster.
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * Within an InstructionList an InstructionHandle object is wrapped
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 * around all instructions, i.e., it implements a cell in a
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 * doubly-linked list. From the outside only the next and the
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
 * previous instruction (handle) are accessible. One
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
 * can traverse the list via an Enumeration returned by
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
 * InstructionList.elements().
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
 * @see Instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
 * @see BranchHandle
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
 * @see InstructionList
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
public class InstructionHandle implements java.io.Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  InstructionHandle next, prev;  // Will be set from the outside
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  Instruction       instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  protected int     i_position = -1; // byte code offset of instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
  private HashSet   targeters;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  private HashMap   attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  public final InstructionHandle getNext()        { return next; }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  public final InstructionHandle getPrev()        { return prev; }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  public final Instruction       getInstruction() { return instruction; }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   * Replace current instruction contained in this handle.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   * Old instruction is disposed using Instruction.dispose().
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
  public void setInstruction(Instruction i) { // Overridden in BranchHandle
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    if(i == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      throw new ClassGenException("Assigning null to handle");
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    if((this.getClass() != BranchHandle.class) && (i instanceof BranchInstruction))
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
      throw new ClassGenException("Assigning branch instruction " + i + " to plain handle");
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    if(instruction != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      instruction.dispose();
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    instruction = i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * Temporarily swap the current instruction, without disturbing
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   * anything. Meant to be used by a debugger, implementing
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * breakpoints. Current instruction is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
  public Instruction swapInstruction(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    Instruction oldInstruction = instruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    instruction = i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    return oldInstruction;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
  /*private*/ protected InstructionHandle(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    setInstruction(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  private static InstructionHandle ih_list = null; // List of reusable handles
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  /** Factory method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  static final InstructionHandle getInstructionHandle(Instruction i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    if(ih_list == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
      return new InstructionHandle(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      InstructionHandle ih = ih_list;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
      ih_list = ih.next;
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
      ih.setInstruction(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
      return ih;
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
   * Called by InstructionList.setPositions when setting the position for every
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
   * instruction. In the presence of variable length instructions `setPositions()'
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
   * performs multiple passes over the instruction list to calculate the
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
   * correct (byte) positions and offsets by calling this function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
   * @param offset additional offset caused by preceding (variable length) instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
   * @param max_offset the maximum offset that may be caused by these instructions
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   * @return additional offset caused by possible change of this instruction's length
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
  protected int updatePosition(int offset, int max_offset) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    i_position += offset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  /** @return the position, i.e., the byte code offset of the contained
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   * instruction. This is accurate only after
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   * InstructionList.setPositions() has been called.
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
  public int getPosition() { return i_position; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
  /** Set the position, i.e., the byte code offset of the contained
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
   * instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
  void setPosition(int pos) { i_position = pos; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
  /** Overridden in BranchHandle
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
  protected void addHandle() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    next    = ih_list;
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    ih_list = this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   * Delete contents, i.e., remove user access and make handle reusable.
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
  void dispose() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    next = prev = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    instruction.dispose();
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    instruction = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    i_position = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    attributes = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    removeAllTargeters();
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    addHandle();
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  /** Remove all targeters, if any.
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
  public void removeAllTargeters() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    if(targeters != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
      targeters.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   * Denote this handle isn't referenced anymore by t.
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  public void removeTargeter(InstructionTargeter t) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    targeters.remove(t);
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
   * Denote this handle is being referenced by t.
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
  public void addTargeter(InstructionTargeter t) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    if(targeters == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
      targeters = new HashSet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    //if(!targeters.contains(t))
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    targeters.add(t);
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
  public boolean hasTargeters() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
    return (targeters != null) && (targeters.size() > 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   * @return null, if there are no targeters
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
  public InstructionTargeter[] getTargeters() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    if(!hasTargeters())
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    InstructionTargeter[] t = new InstructionTargeter[targeters.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    targeters.toArray(t);
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    return t;
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
  /** @return a (verbose) string representation of the contained instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  public String toString(boolean verbose) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
    return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
  /** @return a string representation of the contained instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
  public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    return toString(true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
  /** Add an attribute to an instruction handle.
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
   * @param key the key object to store/retrieve the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
   * @param attr the attribute to associate with this handle
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
  public void addAttribute(Object key, Object attr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
    if(attributes == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
      attributes = new HashMap(3);
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
    attributes.put(key, attr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
  /** Delete an attribute of an instruction handle.
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   * @param key the key object to retrieve the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
  public void removeAttribute(Object key) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
    if(attributes != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
      attributes.remove(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
  /** Get attribute of an instruction handle.
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   * @param key the key object to store/retrieve the attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
  public Object getAttribute(Object key) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
    if(attributes != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
      return attributes.get(key);
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
  /** @return all attributes associated with this handle
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
  public Collection getAttributes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    return attributes.values();
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
  /** Convenience method, simply calls accept() on the contained instruction.
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
   * @param v Visitor object
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
  public void accept(Visitor v) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    instruction.accept(v);
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
}