jaxp/src/com/sun/org/apache/bcel/internal/generic/ClassGen.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/ClassGen.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.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 java class. May be initialized with an
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * existing java class (file).
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * @see JavaClass
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
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
public class ClassGen extends AccessFlags implements Cloneable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
  /* Corresponds to the fields found in a JavaClass object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
  private String   class_name, super_class_name, file_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  private int      class_name_index = -1, superclass_name_index = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  private int      major = Constants.MAJOR_1_1, minor = Constants.MINOR_1_1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  private ConstantPoolGen cp; // Template for building up constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  // ArrayLists instead of arrays to gather fields, methods, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  private ArrayList   field_vec     = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  private ArrayList   method_vec    = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  private ArrayList   attribute_vec = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  private ArrayList   interface_vec = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  /** Convenience constructor to set up some important values initially.
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   * @param class_name fully qualified class name
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   * @param super_class_name fully qualified superclass name
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   * @param file_name source file name
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   * @param access_flags access qualifiers
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
   * @param interfaces implemented interfaces
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   * @param cp constant pool to use
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
  public ClassGen(String class_name, String super_class_name, String file_name,
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                  int access_flags, String[] interfaces, ConstantPoolGen cp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    this.class_name       = class_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    this.super_class_name = super_class_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    this.file_name        = file_name;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    this.access_flags     = access_flags;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    this.cp               = cp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    // Put everything needed by default into the constant pool and the vectors
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    if(file_name != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      addAttribute(new SourceFile(cp.addUtf8("SourceFile"), 2,
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                                  cp.addUtf8(file_name), cp.getConstantPool()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    class_name_index      = cp.addClass(class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    superclass_name_index = cp.addClass(super_class_name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    if(interfaces != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
      for(int i=0; i < interfaces.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        addInterface(interfaces[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
  /** Convenience constructor to set up some important values initially.
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   * @param class_name fully qualified class name
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   * @param super_class_name fully qualified superclass name
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
   * @param file_name source file name
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
   * @param access_flags access qualifiers
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
   * @param interfaces implemented interfaces
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  public ClassGen(String class_name, String super_class_name, String file_name,
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
                  int access_flags, String[] interfaces) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    this(class_name, super_class_name, file_name, access_flags, interfaces,
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
         new ConstantPoolGen());
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   * Initialize with existing class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   * @param clazz JavaClass object (e.g. read from file)
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
  public ClassGen(JavaClass clazz) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    class_name_index      = clazz.getClassNameIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    superclass_name_index = clazz.getSuperclassNameIndex();
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    class_name            = clazz.getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    super_class_name      = clazz.getSuperclassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    file_name             = clazz.getSourceFileName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    access_flags          = clazz.getAccessFlags();
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    cp                    = new ConstantPoolGen(clazz.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    major                 = clazz.getMajor();
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    minor                 = clazz.getMinor();
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    Attribute[] attributes = clazz.getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    Method[]    methods    = clazz.getMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    Field[]     fields     = clazz.getFields();
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    String[]    interfaces = clazz.getInterfaceNames();
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    for(int i=0; i < interfaces.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      addInterface(interfaces[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    for(int i=0; i < attributes.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
      addAttribute(attributes[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    for(int i=0; i < methods.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
      addMethod(methods[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    for(int i=0; i < fields.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
      addField(fields[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   * @return the (finally) built up Java class object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
  public JavaClass getJavaClass() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    int[]        interfaces = getInterfaces();
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    Field[]      fields     = getFields();
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    Method[]     methods    = getMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    Attribute[]  attributes = getAttributes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    // Must be last since the above calls may still add something to it
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    ConstantPool cp         = this.cp.getFinalConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    return new JavaClass(class_name_index, superclass_name_index,
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
                         file_name, major, minor, access_flags,
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
                         cp, interfaces, fields, methods, attributes);
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
   * Add an interface to this class, i.e., this class has to implement it.
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
   * @param name interface to implement (fully qualified class name)
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
  public void addInterface(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    interface_vec.add(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
   * Remove an interface from this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * @param name interface to remove (fully qualified name)
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
  public void removeInterface(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    interface_vec.remove(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
   * @return major version number of class file
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
  public int  getMajor()      { return major; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
  /** Set major version number of class file, default value is 45 (JDK 1.1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
   * @param major major version number
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
  public void setMajor(int major) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    this.major = major;
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
  /** Set minor version number of class file, default value is 3 (JDK 1.1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
   * @param minor minor version number
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
  public void setMinor(int minor) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    this.minor = minor;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
  }
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 minor version number of class file
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  public int  getMinor()      { return minor; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
   * Add an attribute to this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
   * @param a attribute to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
  public void addAttribute(Attribute a)    { attribute_vec.add(a); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
   * Add a method to this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   * @param m method to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  public void addMethod(Method m)          { method_vec.add(m); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
   * Convenience method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   * Add an empty constructor to this class that does nothing but calling super().
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
   * @param access rights for constructor
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
  public void addEmptyConstructor(int access_flags) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    il.append(InstructionConstants.THIS); // Push `this'
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
    il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name,
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
                                                "<init>", "()V")));
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    il.append(InstructionConstants.RETURN);
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null,
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
                       "<init>", class_name, il, cp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    mg.setMaxStack(1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
    addMethod(mg.getMethod());
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
   * Add a field to this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
   * @param f field to add
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
  public void addField(Field f)            { field_vec.add(f); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
  public boolean containsField(Field f)    { return field_vec.contains(f); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
  /** @return field object with given name, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
  public Field containsField(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    for(Iterator e=field_vec.iterator(); e.hasNext(); ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
      Field f = (Field)e.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
      if(f.getName().equals(name))
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
        return f;
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
  /** @return method object with given name and signature, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
  public Method containsMethod(String name, String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
    for(Iterator e=method_vec.iterator(); e.hasNext();) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
      Method m = (Method)e.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
      if(m.getName().equals(name) && m.getSignature().equals(signature))
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
        return m;
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   * Remove an attribute from this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   * @param a attribute to remove
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
  public void removeAttribute(Attribute a) { attribute_vec.remove(a); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   * Remove a method from this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
   * @param m method to remove
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
  public void removeMethod(Method m)       { method_vec.remove(m); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
  /** Replace given method with new one. If the old one does not exist
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
   * add the new_ method to the class anyway.
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
  public void replaceMethod(Method old, Method new_) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
    if(new_ == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
      throw new ClassGenException("Replacement method must not be null");
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
    int i = method_vec.indexOf(old);
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
    if(i < 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
      method_vec.add(new_);
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
      method_vec.set(i, new_);
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
  /** Replace given field with new one. If the old one does not exist
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
   * add the new_ field to the class anyway.
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
  public void replaceField(Field old, Field new_) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
    if(new_ == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
      throw new ClassGenException("Replacement method must not be null");
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
    int i = field_vec.indexOf(old);
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
    if(i < 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
      field_vec.add(new_);
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
      field_vec.set(i, new_);
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
   * Remove a field to this class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
   * @param f field to remove
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
  public void removeField(Field f)         { field_vec.remove(f); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
  public String getClassName()      { return class_name; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
  public String getSuperclassName() { return super_class_name; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
  public String getFileName()       { return file_name; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
  public void setClassName(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
    class_name = name.replace('/', '.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
    class_name_index = cp.addClass(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
  public void setSuperclassName(String name) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
    super_class_name = name.replace('/', '.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
    superclass_name_index = cp.addClass(name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
  public Method[] getMethods() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
    Method[] methods = new Method[method_vec.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
    method_vec.toArray(methods);
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    return methods;
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
  public void setMethods(Method[] methods) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
    method_vec.clear();
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
    for(int m=0; m<methods.length; m++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
      addMethod(methods[m]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
  public void setMethodAt(Method method, int pos) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    method_vec.set(pos, method);
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
  public Method getMethodAt(int pos) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
    return (Method)method_vec.get(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
  public String[] getInterfaceNames() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
    int      size = interface_vec.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
    String[] interfaces = new String[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
    interface_vec.toArray(interfaces);
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
    return interfaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
  public int[] getInterfaces() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
    int   size = interface_vec.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
    int[] interfaces = new int[size];
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
    for(int i=0; i < size; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
      interfaces[i] = cp.addClass((String)interface_vec.get(i));
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
    return interfaces;
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
  public Field[] getFields() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
    Field[] fields = new Field[field_vec.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    field_vec.toArray(fields);
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
    return fields;
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
  public Attribute[] getAttributes() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
    Attribute[] attributes = new Attribute[attribute_vec.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
    attribute_vec.toArray(attributes);
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    return attributes;
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
  public ConstantPoolGen getConstantPool() { return cp; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
  public void setConstantPool(ConstantPoolGen constant_pool) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
    cp = constant_pool;
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
  public void setClassNameIndex(int class_name_index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
    this.class_name_index = class_name_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    class_name = cp.getConstantPool().
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
      getConstantString(class_name_index, Constants.CONSTANT_Class).replace('/', '.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
  public void setSuperclassNameIndex(int superclass_name_index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
    this.superclass_name_index = superclass_name_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
    super_class_name = cp.getConstantPool().
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
      getConstantString(superclass_name_index, Constants.CONSTANT_Class).replace('/', '.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
  public int getSuperclassNameIndex() { return superclass_name_index; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
  public int getClassNameIndex()   { return class_name_index; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
  private ArrayList observers;
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
  /** Add observer for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
  public void addObserver(ClassObserver o) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
    if(observers == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
      observers = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
    observers.add(o);
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
  /** Remove observer for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
  public void removeObserver(ClassObserver o) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
    if(observers != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
      observers.remove(o);
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
  /** Call notify() method on all observers. This method is not called
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
   * automatically whenever the state has changed, but has to be
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
   * called by the user after he has finished editing the object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
  public void update() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
    if(observers != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
      for(Iterator e = observers.iterator(); e.hasNext(); )
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
        ((ClassObserver)e.next()).notify(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
  public Object clone() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
      return super.clone();
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
    } catch(CloneNotSupportedException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
      System.err.println(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
}