jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/generic/FieldGen.java
author chegar
Sun, 17 Aug 2014 15:51:56 +0100
changeset 25868 686eef1e7a79
parent 12457 jaxp/src/com/sun/org/apache/bcel/internal/generic/FieldGen.java@c348e06f0e82
child 44797 8b3b3b911b8a
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
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.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
import com.sun.org.apache.bcel.internal.classfile.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import java.util.ArrayList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
import java.util.Iterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * Template class for building up a field.  The only extraordinary thing
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * one can do is to add a constant value attribute to a field (which must of
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * course be compatible with to the declared type).
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 * @see Field
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
public class FieldGen extends FieldGenOrMethodGen {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  private Object value = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * Declare a field. If it is static (isStatic() == true) and has a
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * basic type like int or String it may have an initial value
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   * associated with it as defined by setInitValue().
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * @param access_flags access qualifiers
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * @param type  field type
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   * @param name field name
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
   * @param cp constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
  public FieldGen(int access_flags, Type type, String name, ConstantPoolGen cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    setAccessFlags(access_flags);
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    setType(type);
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    setName(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    setConstantPool(cp);
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   * Instantiate from existing field.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   * @param field Field object
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   * @param cp constant pool (must contain the same entries as the field's constant pool)
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
  public FieldGen(Field field, ConstantPoolGen cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    Attribute[] attrs = field.getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    for(int i=0; i < attrs.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      if(attrs[i] instanceof ConstantValue)
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        setValue(((ConstantValue)attrs[i]).getConstantValueIndex());
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        addAttribute(attrs[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  private void setValue(int index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    ConstantPool cp  = this.cp.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    Constant     c   = cp.getConstant(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    value = ((ConstantObject)c).getConstantValue(cp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   * Set (optional) initial value of field, otherwise it will be set to null/0/false
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   * by the JVM automatically.
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  public void setInitValue(String str) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    checkType(new ObjectType("java.lang.String"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    if(str != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
      value = str;
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  public void setInitValue(long l) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    checkType(Type.LONG);
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    if(l != 0L)
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      value = new Long(l);
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
  public void setInitValue(int i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    checkType(Type.INT);
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    if(i != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
      value = new Integer(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
  public void setInitValue(short s) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    checkType(Type.SHORT);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    if(s != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
      value = new Integer(s);
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
  public void setInitValue(char c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    checkType(Type.CHAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    if(c != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
      value = new Integer(c);
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  public void setInitValue(byte b) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    checkType(Type.BYTE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    if(b != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
      value = new Integer(b);
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  public void setInitValue(boolean b) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    checkType(Type.BOOLEAN);
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    if(b)
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
      value = new Integer(1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  public void setInitValue(float f) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    checkType(Type.FLOAT);
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    if(f != 0.0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
      value = new Float(f);
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
  public void setInitValue(double d) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    checkType(Type.DOUBLE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    if(d != 0.0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
      value = new Double(d);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
  /** Remove any initial value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
  public void cancelInitValue() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
    value = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
  private void checkType(Type atype) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    if(type == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
      throw new ClassGenException("You haven't defined the type of the field yet");
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    if(!isFinal())
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
      throw new ClassGenException("Only final fields may have an initial value!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    if(!type.equals(atype))
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
      throw new ClassGenException("Types are not compatible: " + type + " vs. " + atype);
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
   * Get field object after having set up all necessary values.
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
  public Field getField() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    String      signature       = getSignature();
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    int         name_index      = cp.addUtf8(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    int         signature_index = cp.addUtf8(signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    if(value != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
      checkType(type);
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
      int index = addConstant();
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
      addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"),
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
                                     2, index, cp.getConstantPool()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
    return new Field(access_flags, name_index, signature_index, getAttributes(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
                     cp.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  private int addConstant() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    switch(type.getType()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    case Constants.T_INT: case Constants.T_CHAR: case Constants.T_BYTE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    case Constants.T_BOOLEAN: case Constants.T_SHORT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
      return cp.addInteger(((Integer)value).intValue());
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    case Constants.T_FLOAT:
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
      return cp.addFloat(((Float)value).floatValue());
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    case Constants.T_DOUBLE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
      return cp.addDouble(((Double)value).doubleValue());
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    case Constants.T_LONG:
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
      return cp.addLong(((Long)value).longValue());
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    case Constants.T_REFERENCE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
      return cp.addString(((String)value));
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
    default:
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
      throw new RuntimeException("Oops: Unhandled : " + type.getType());
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
  public String  getSignature()  { return type.getSignature(); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
  private ArrayList observers;
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
  /** Add observer for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
  public void addObserver(FieldObserver o) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
    if(observers == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
      observers = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
    observers.add(o);
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
  /** Remove observer for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
  public void removeObserver(FieldObserver o) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
    if(observers != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
      observers.remove(o);
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
  /** Call notify() method on all observers. This method is not called
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   * automatically whenever the state has changed, but has to be
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * called by the user after he has finished editing the object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
  public void update() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
    if(observers != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
      for(Iterator e = observers.iterator(); e.hasNext(); )
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
        ((FieldObserver)e.next()).notify(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
  public String getInitValue() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
    if(value != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
      return value.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
    } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
   * Return string representation close to declaration format,
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
   * `public static final short MAX = 100', e.g..
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   * @return String representation of field
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
  public final String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
    String name, signature, access; // Short cuts to constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
    access    = Utility.accessToString(access_flags);
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
    access    = access.equals("")? "" : (access + " ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
    signature = type.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
    name      = getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
    StringBuffer buf = new StringBuffer(access + signature + " " + name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
    String value = getInitValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
    if(value != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
      buf.append(" = " + value);
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
  /** @return deep copy of this field
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
  public FieldGen copy(ConstantPoolGen cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    FieldGen fg = (FieldGen)clone();
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
    fg.setConstantPool(cp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    return fg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
}