jaxp/src/share/classes/com/sun/org/apache/bcel/internal/generic/Type.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
permissions -rw-r--r--
Initial load
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
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 * Abstract super class for all possible java types, namely basic types
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * such as int, object types like String and array types, e.g. int[]
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
public abstract class Type implements java.io.Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  protected byte   type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  protected String signature; // signature for the type
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  /** Predefined constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  public static final BasicType     VOID         = new BasicType(Constants.T_VOID);
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  public static final BasicType     BOOLEAN      = new BasicType(Constants.T_BOOLEAN);
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
  public static final BasicType     INT          = new BasicType(Constants.T_INT);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  public static final BasicType     SHORT        = new BasicType(Constants.T_SHORT);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
  public static final BasicType     BYTE         = new BasicType(Constants.T_BYTE);
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
  public static final BasicType     LONG         = new BasicType(Constants.T_LONG);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  public static final BasicType     DOUBLE       = new BasicType(Constants.T_DOUBLE);
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  public static final BasicType     FLOAT        = new BasicType(Constants.T_FLOAT);
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  public static final BasicType     CHAR         = new BasicType(Constants.T_CHAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  public static final ObjectType    OBJECT       = new ObjectType("java.lang.Object");
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
  public static final ObjectType    STRING       = new ObjectType("java.lang.String");
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  public static final ObjectType    STRINGBUFFER = new ObjectType("java.lang.StringBuffer");
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  public static final ObjectType    THROWABLE    = new ObjectType("java.lang.Throwable");
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
  public static final Type[]        NO_ARGS      = new Type[0];
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  public static final ReferenceType NULL         = new ReferenceType(){};
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  public static final Type          UNKNOWN      = new Type(Constants.T_UNKNOWN,
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                                                            "<unknown object>"){};
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  protected Type(byte t, String s) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    type      = t;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    signature = s;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   * @return signature for given type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
  public String getSignature() { return signature; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
   * @return type as defined in Constants
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
  public byte getType() { return type; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * @return stack size of this type (2 for long and double, 0 for void, 1 otherwise)
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  public int getSize() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    switch(type) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    case Constants.T_DOUBLE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    case Constants.T_LONG: return 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    case Constants.T_VOID: return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    default:     return 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
   * @return Type string, e.g. `int[]'
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
  public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    return ((this.equals(Type.NULL) || (type >= Constants.T_UNKNOWN)))? signature :
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
      Utility.signatureToString(signature, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   * Convert type to Java method signature, e.g. int[] f(java.lang.String x)
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   * becomes (Ljava/lang/String;)[I
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   * @param return_type what the method returns
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   * @param arg_types what are the argument types
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
   * @return method signature for given type(s).
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
  public static String getMethodSignature(Type return_type, Type[] arg_types) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    StringBuffer buf = new StringBuffer("(");
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    int length = (arg_types == null)? 0 : arg_types.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    for(int i=0; i < length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
      buf.append(arg_types[i].getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    buf.append(')');
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    buf.append(return_type.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
  private static int consumed_chars=0; // Remember position in string, see getArgumentTypes
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
   * Convert signature to a Type object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   * @param signature signature string such as Ljava/lang/String;
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   * @return type object
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  public static final Type getType(String signature)
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    throws StringIndexOutOfBoundsException
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    byte type = Utility.typeOfSignature(signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    if(type <= Constants.T_VOID) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
      consumed_chars = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
      return BasicType.getType(type);
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    } else if(type == Constants.T_ARRAY) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
      int dim=0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
      do { // Count dimensions
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        dim++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
      } while(signature.charAt(dim) == '[');
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
      // Recurse, but just once, if the signature is ok
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
      Type t = getType(signature.substring(dim));
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
      consumed_chars += dim; // update counter
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
      return new ArrayType(t, dim);
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    } else { // type == T_REFERENCE
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
      int index = signature.indexOf(';'); // Look for closing `;'
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
      if(index < 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        throw new ClassFormatException("Invalid signature: " + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
      consumed_chars = index + 1; // "Lblabla;" `L' and `;' are removed
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
      return new ObjectType(signature.substring(1, index).replace('/', '.'));
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    }
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
   * Convert return value of a method (signature) to a Type object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   * @param signature signature string such as (Ljava/lang/String;)V
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
   * @return return type
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
  public static Type getReturnType(String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
      // Read return type after `)'
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
      int index = signature.lastIndexOf(')') + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
      return getType(signature.substring(index));
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    } catch(StringIndexOutOfBoundsException e) { // Should never occur
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
      throw new ClassFormatException("Invalid method signature: " + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   * Convert arguments of a method (signature) to an array of Type objects.
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   * @param signature signature string such as (Ljava/lang/String;)V
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
   * @return array of argument types
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
  public static Type[] getArgumentTypes(String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
    ArrayList vec = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    int       index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
    Type[]     types;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    try { // Read all declarations between for `(' and `)'
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
      if(signature.charAt(0) != '(')
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        throw new ClassFormatException("Invalid method signature: " + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
      index = 1; // current string position
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
      while(signature.charAt(index) != ')') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
        vec.add(getType(signature.substring(index)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
        index += consumed_chars; // update position
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    } catch(StringIndexOutOfBoundsException e) { // Should never occur
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
      throw new ClassFormatException("Invalid method signature: " + signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    types = new Type[vec.size()];
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    vec.toArray(types);
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
    return types;
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
  /** Convert runtime java.lang.Class to BCEL Type object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   * @param cl Java class
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   * @return corresponding Type object
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
  public static Type getType(java.lang.Class cl) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
    if(cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
      throw new IllegalArgumentException("Class must not be null");
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    /* That's an amzingly easy case, because getName() returns
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
     * the signature. That's what we would have liked anyway.
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    if(cl.isArray()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
      return getType(cl.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    } else if(cl.isPrimitive()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
      if(cl == Integer.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
        return INT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
      } else if(cl == Void.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
        return VOID;
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
      } else if(cl == Double.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
        return DOUBLE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
      } else if(cl == Float.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
        return FLOAT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
      } else if(cl == Boolean.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        return BOOLEAN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
      } else if(cl == Byte.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
        return BYTE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
      } else if(cl == Short.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        return SHORT;
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
      } else if(cl == Byte.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
        return BYTE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
      } else if(cl == Long.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
        return LONG;
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
      } else if(cl == Character.TYPE) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
        return CHAR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
      } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        throw new IllegalStateException("Ooops, what primitive type is " + cl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    } else { // "Real" class
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
      return new ObjectType(cl.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
  public static String getSignature(java.lang.reflect.Method meth) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    StringBuffer sb = new StringBuffer("(");
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    Class[] params = meth.getParameterTypes(); // avoid clone
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
    for(int j = 0; j < params.length; j++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
      sb.append(getType(params[j]).getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
    sb.append(")");
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
    sb.append(getType(meth.getReturnType()).getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
    return sb.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
}