jdk/src/share/classes/sun/misc/ProxyGenerator.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 11119 6ff03c1202ce
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.action.GetBooleanAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ProxyGenerator contains the code to generate a dynamic proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * for the java.lang.reflect.Proxy API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The external interfaces to ProxyGenerator is the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * "generateProxyClass" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author      Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since       1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class ProxyGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * In the comments below, "JVMS" refers to The Java Virtual Machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Specification Second Edition and "JLS" refers to the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * version of The Java Language Specification, unless otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* generate 1.5-era class file version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int CLASSFILE_MAJOR_VERSION = 49;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int CLASSFILE_MINOR_VERSION = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * beginning of constants copied from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * sun.tools.java.RuntimeConstants (which no longer exists):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* constant pool tags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int CONSTANT_UTF8              = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final int CONSTANT_UNICODE           = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final int CONSTANT_INTEGER           = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final int CONSTANT_FLOAT             = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int CONSTANT_LONG              = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final int CONSTANT_DOUBLE            = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int CONSTANT_CLASS             = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int CONSTANT_STRING            = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final int CONSTANT_FIELD             = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final int CONSTANT_METHOD            = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final int CONSTANT_INTERFACEMETHOD   = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final int CONSTANT_NAMEANDTYPE       = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /* access and modifier flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final int ACC_PUBLIC                 = 0x00000001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final int ACC_PRIVATE                = 0x00000002;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
//  private static final int ACC_PROTECTED              = 0x00000004;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final int ACC_STATIC                 = 0x00000008;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final int ACC_FINAL                  = 0x00000010;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
//  private static final int ACC_SYNCHRONIZED           = 0x00000020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
//  private static final int ACC_VOLATILE               = 0x00000040;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
//  private static final int ACC_TRANSIENT              = 0x00000080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
//  private static final int ACC_NATIVE                 = 0x00000100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
//  private static final int ACC_INTERFACE              = 0x00000200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
//  private static final int ACC_ABSTRACT               = 0x00000400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static final int ACC_SUPER                  = 0x00000020;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
//  private static final int ACC_STRICT                 = 0x00000800;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /* opcodes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
//  private static final int opc_nop                    = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int opc_aconst_null            = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
//  private static final int opc_iconst_m1              = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final int opc_iconst_0               = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
//  private static final int opc_iconst_1               = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
//  private static final int opc_iconst_2               = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
//  private static final int opc_iconst_3               = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
//  private static final int opc_iconst_4               = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
//  private static final int opc_iconst_5               = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
//  private static final int opc_lconst_0               = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
//  private static final int opc_lconst_1               = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
//  private static final int opc_fconst_0               = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
//  private static final int opc_fconst_1               = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
//  private static final int opc_fconst_2               = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
//  private static final int opc_dconst_0               = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
//  private static final int opc_dconst_1               = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static final int opc_bipush                 = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static final int opc_sipush                 = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final int opc_ldc                    = 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static final int opc_ldc_w                  = 19;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
//  private static final int opc_ldc2_w                 = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final int opc_iload                  = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final int opc_lload                  = 22;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static final int opc_fload                  = 23;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static final int opc_dload                  = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final int opc_aload                  = 25;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final int opc_iload_0                = 26;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
//  private static final int opc_iload_1                = 27;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
//  private static final int opc_iload_2                = 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
//  private static final int opc_iload_3                = 29;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final int opc_lload_0                = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
//  private static final int opc_lload_1                = 31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
//  private static final int opc_lload_2                = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
//  private static final int opc_lload_3                = 33;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final int opc_fload_0                = 34;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
//  private static final int opc_fload_1                = 35;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
//  private static final int opc_fload_2                = 36;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
//  private static final int opc_fload_3                = 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static final int opc_dload_0                = 38;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
//  private static final int opc_dload_1                = 39;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
//  private static final int opc_dload_2                = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
//  private static final int opc_dload_3                = 41;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final int opc_aload_0                = 42;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
//  private static final int opc_aload_1                = 43;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
//  private static final int opc_aload_2                = 44;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
//  private static final int opc_aload_3                = 45;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
//  private static final int opc_iaload                 = 46;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
//  private static final int opc_laload                 = 47;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
//  private static final int opc_faload                 = 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
//  private static final int opc_daload                 = 49;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
//  private static final int opc_aaload                 = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
//  private static final int opc_baload                 = 51;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
//  private static final int opc_caload                 = 52;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
//  private static final int opc_saload                 = 53;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
//  private static final int opc_istore                 = 54;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
//  private static final int opc_lstore                 = 55;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
//  private static final int opc_fstore                 = 56;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
//  private static final int opc_dstore                 = 57;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final int opc_astore                 = 58;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
//  private static final int opc_istore_0               = 59;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
//  private static final int opc_istore_1               = 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
//  private static final int opc_istore_2               = 61;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
//  private static final int opc_istore_3               = 62;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
//  private static final int opc_lstore_0               = 63;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
//  private static final int opc_lstore_1               = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
//  private static final int opc_lstore_2               = 65;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
//  private static final int opc_lstore_3               = 66;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
//  private static final int opc_fstore_0               = 67;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
//  private static final int opc_fstore_1               = 68;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
//  private static final int opc_fstore_2               = 69;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
//  private static final int opc_fstore_3               = 70;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
//  private static final int opc_dstore_0               = 71;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
//  private static final int opc_dstore_1               = 72;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
//  private static final int opc_dstore_2               = 73;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
//  private static final int opc_dstore_3               = 74;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final int opc_astore_0               = 75;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
//  private static final int opc_astore_1               = 76;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
//  private static final int opc_astore_2               = 77;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
//  private static final int opc_astore_3               = 78;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
//  private static final int opc_iastore                = 79;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
//  private static final int opc_lastore                = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
//  private static final int opc_fastore                = 81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
//  private static final int opc_dastore                = 82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static final int opc_aastore                = 83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
//  private static final int opc_bastore                = 84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
//  private static final int opc_castore                = 85;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
//  private static final int opc_sastore                = 86;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private static final int opc_pop                    = 87;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
//  private static final int opc_pop2                   = 88;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static final int opc_dup                    = 89;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
//  private static final int opc_dup_x1                 = 90;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
//  private static final int opc_dup_x2                 = 91;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
//  private static final int opc_dup2                   = 92;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
//  private static final int opc_dup2_x1                = 93;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
//  private static final int opc_dup2_x2                = 94;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
//  private static final int opc_swap                   = 95;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
//  private static final int opc_iadd                   = 96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
//  private static final int opc_ladd                   = 97;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
//  private static final int opc_fadd                   = 98;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
//  private static final int opc_dadd                   = 99;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
//  private static final int opc_isub                   = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
//  private static final int opc_lsub                   = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
//  private static final int opc_fsub                   = 102;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
//  private static final int opc_dsub                   = 103;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
//  private static final int opc_imul                   = 104;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
//  private static final int opc_lmul                   = 105;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
//  private static final int opc_fmul                   = 106;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
//  private static final int opc_dmul                   = 107;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
//  private static final int opc_idiv                   = 108;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
//  private static final int opc_ldiv                   = 109;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
//  private static final int opc_fdiv                   = 110;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
//  private static final int opc_ddiv                   = 111;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
//  private static final int opc_irem                   = 112;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
//  private static final int opc_lrem                   = 113;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
//  private static final int opc_frem                   = 114;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
//  private static final int opc_drem                   = 115;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
//  private static final int opc_ineg                   = 116;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
//  private static final int opc_lneg                   = 117;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
//  private static final int opc_fneg                   = 118;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
//  private static final int opc_dneg                   = 119;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
//  private static final int opc_ishl                   = 120;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
//  private static final int opc_lshl                   = 121;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
//  private static final int opc_ishr                   = 122;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
//  private static final int opc_lshr                   = 123;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
//  private static final int opc_iushr                  = 124;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
//  private static final int opc_lushr                  = 125;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
//  private static final int opc_iand                   = 126;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
//  private static final int opc_land                   = 127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
//  private static final int opc_ior                    = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
//  private static final int opc_lor                    = 129;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
//  private static final int opc_ixor                   = 130;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
//  private static final int opc_lxor                   = 131;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
//  private static final int opc_iinc                   = 132;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
//  private static final int opc_i2l                    = 133;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
//  private static final int opc_i2f                    = 134;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
//  private static final int opc_i2d                    = 135;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
//  private static final int opc_l2i                    = 136;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
//  private static final int opc_l2f                    = 137;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
//  private static final int opc_l2d                    = 138;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
//  private static final int opc_f2i                    = 139;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
//  private static final int opc_f2l                    = 140;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
//  private static final int opc_f2d                    = 141;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
//  private static final int opc_d2i                    = 142;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
//  private static final int opc_d2l                    = 143;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
//  private static final int opc_d2f                    = 144;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
//  private static final int opc_i2b                    = 145;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
//  private static final int opc_i2c                    = 146;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
//  private static final int opc_i2s                    = 147;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
//  private static final int opc_lcmp                   = 148;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
//  private static final int opc_fcmpl                  = 149;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
//  private static final int opc_fcmpg                  = 150;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
//  private static final int opc_dcmpl                  = 151;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
//  private static final int opc_dcmpg                  = 152;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
//  private static final int opc_ifeq                   = 153;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
//  private static final int opc_ifne                   = 154;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
//  private static final int opc_iflt                   = 155;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
//  private static final int opc_ifge                   = 156;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
//  private static final int opc_ifgt                   = 157;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
//  private static final int opc_ifle                   = 158;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
//  private static final int opc_if_icmpeq              = 159;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
//  private static final int opc_if_icmpne              = 160;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
//  private static final int opc_if_icmplt              = 161;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
//  private static final int opc_if_icmpge              = 162;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
//  private static final int opc_if_icmpgt              = 163;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
//  private static final int opc_if_icmple              = 164;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
//  private static final int opc_if_acmpeq              = 165;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
//  private static final int opc_if_acmpne              = 166;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
//  private static final int opc_goto                   = 167;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
//  private static final int opc_jsr                    = 168;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
//  private static final int opc_ret                    = 169;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
//  private static final int opc_tableswitch            = 170;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
//  private static final int opc_lookupswitch           = 171;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final int opc_ireturn                = 172;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private static final int opc_lreturn                = 173;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static final int opc_freturn                = 174;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static final int opc_dreturn                = 175;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private static final int opc_areturn                = 176;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private static final int opc_return                 = 177;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private static final int opc_getstatic              = 178;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static final int opc_putstatic              = 179;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private static final int opc_getfield               = 180;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
//  private static final int opc_putfield               = 181;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private static final int opc_invokevirtual          = 182;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static final int opc_invokespecial          = 183;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private static final int opc_invokestatic           = 184;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private static final int opc_invokeinterface        = 185;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private static final int opc_new                    = 187;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
//  private static final int opc_newarray               = 188;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private static final int opc_anewarray              = 189;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
//  private static final int opc_arraylength            = 190;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static final int opc_athrow                 = 191;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private static final int opc_checkcast              = 192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
//  private static final int opc_instanceof             = 193;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
//  private static final int opc_monitorenter           = 194;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
//  private static final int opc_monitorexit            = 195;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private static final int opc_wide                   = 196;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
//  private static final int opc_multianewarray         = 197;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
//  private static final int opc_ifnull                 = 198;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
//  private static final int opc_ifnonnull              = 199;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
//  private static final int opc_goto_w                 = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
//  private static final int opc_jsr_w                  = 201;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // end of constants copied from sun.tools.java.RuntimeConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /** name of the superclass of proxy classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private final static String superclassName = "java/lang/reflect/Proxy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /** name of field for storing a proxy instance's invocation handler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private final static String handlerFieldName = "h";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /** debugging flag for saving generated class files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private final static boolean saveGeneratedFiles =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            new GetBooleanAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                "sun.misc.ProxyGenerator.saveGeneratedFiles")).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Generate a proxy class given a name and a list of proxy interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public static byte[] generateProxyClass(final String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                            Class[] interfaces)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        ProxyGenerator gen = new ProxyGenerator(name, interfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        final byte[] classFile = gen.generateClassFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (saveGeneratedFiles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   327
            new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   328
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        FileOutputStream file =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            new FileOutputStream(dotToSlash(name) + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        file.write(classFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        file.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        throw new InternalError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                            "I/O exception saving generated file: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return classFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /* preloaded Method objects for methods in java.lang.Object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private static Method hashCodeMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private static Method equalsMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private static Method toStringMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            hashCodeMethod = Object.class.getMethod("hashCode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            equalsMethod =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                Object.class.getMethod("equals", new Class[] { Object.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            toStringMethod = Object.class.getMethod("toString");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new NoSuchMethodError(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /** name of proxy class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /** proxy interfaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private Class[] interfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /** constant pool of class being generated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private ConstantPool cp = new ConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /** FieldInfo struct for each field of generated class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private List<FieldInfo> fields = new ArrayList<FieldInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /** MethodInfo struct for each method of generated class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private List<MethodInfo> methods = new ArrayList<MethodInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * maps method signature string to list of ProxyMethod objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * proxy methods with that signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private Map<String, List<ProxyMethod>> proxyMethods =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        new HashMap<String,List<ProxyMethod>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /** count of ProxyMethod objects added to proxyMethods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    private int proxyMethodCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Construct a ProxyGenerator to generate a proxy class with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * specified name and for the given interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * A ProxyGenerator object contains the state for the ongoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * generation of a particular proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    private ProxyGenerator(String className, Class[] interfaces) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        this.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        this.interfaces = interfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Generate a class file for the proxy class.  This method drives the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * class file generation process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private byte[] generateClassFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        /* ============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * Step 1: Assemble ProxyMethod objects for all methods to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * generate proxy dispatching code for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * Record that proxy methods are needed for the hashCode, equals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * and toString methods of java.lang.Object.  This is done before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * the methods from the proxy interfaces so that the methods from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * java.lang.Object take precedence over duplicate methods in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * proxy interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        addProxyMethod(hashCodeMethod, Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        addProxyMethod(equalsMethod, Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        addProxyMethod(toStringMethod, Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * Now record all of the methods from the proxy interfaces, giving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * earlier interfaces precedence over later ones with duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            Method[] methods = interfaces[i].getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            for (int j = 0; j < methods.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                addProxyMethod(methods[j], interfaces[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         * For each set of proxy methods with the same signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         * verify that the methods' return types are compatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            checkReturnTypes(sigmethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /* ============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Step 2: Assemble FieldInfo and MethodInfo structs for all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * fields and methods in the class we are generating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            methods.add(generateConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                for (ProxyMethod pm : sigmethods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    // add static field for method's Method object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    fields.add(new FieldInfo(pm.methodFieldName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        "Ljava/lang/reflect/Method;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                         ACC_PRIVATE | ACC_STATIC));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    // generate code for proxy method and add it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    methods.add(pm.generateMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            methods.add(generateStaticInitializer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } catch (IOException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   463
            throw new InternalError("unexpected I/O Exception", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (methods.size() > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            throw new IllegalArgumentException("method limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (fields.size() > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            throw new IllegalArgumentException("field limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        /* ============================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * Step 3: Write the final class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * Make sure that constant pool indexes are reserved for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * following items before starting to write the final class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        cp.getClass(dotToSlash(className));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        cp.getClass(superclassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            cp.getClass(dotToSlash(interfaces[i].getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * Disallow new constant pool additions beyond this point, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * we are about to write the final constant pool table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        cp.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        DataOutputStream dout = new DataOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
             * Write all the items of the "ClassFile" structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
             * See JVMS section 4.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                        // u4 magic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            dout.writeInt(0xCAFEBABE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                                        // u2 minor_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            dout.writeShort(CLASSFILE_MINOR_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                        // u2 major_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            dout.writeShort(CLASSFILE_MAJOR_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            cp.write(dout);             // (write constant pool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                        // u2 access_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            dout.writeShort(ACC_PUBLIC | ACC_FINAL | ACC_SUPER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                        // u2 this_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            dout.writeShort(cp.getClass(dotToSlash(className)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                        // u2 super_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            dout.writeShort(cp.getClass(superclassName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                        // u2 interfaces_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            dout.writeShort(interfaces.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                        // u2 interfaces[interfaces_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                dout.writeShort(cp.getClass(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    dotToSlash(interfaces[i].getName())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                        // u2 fields_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            dout.writeShort(fields.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                        // field_info fields[fields_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            for (FieldInfo f : fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                f.write(dout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                        // u2 methods_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            dout.writeShort(methods.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                        // method_info methods[methods_count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            for (MethodInfo m : methods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                m.write(dout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                                         // u2 attributes_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            dout.writeShort(0); // (no ClassFile attributes for proxy classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        } catch (IOException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   543
            throw new InternalError("unexpected I/O Exception", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Add another method to be proxied, either by creating a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * ProxyMethod object or augmenting an old one for a duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * "fromClass" indicates the proxy interface that the method was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * found through, which may be different from (a subinterface of)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * the method's "declaring class".  Note that the first Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * object passed for a given name and descriptor identifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Method object (and thus the declaring class) that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * passed to the invocation handler's "invoke" method for a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * set of duplicate methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    private void addProxyMethod(Method m, Class fromClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        String name = m.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        Class[] parameterTypes = m.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        Class returnType = m.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        Class[] exceptionTypes = m.getExceptionTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        String sig = name + getParameterDescriptors(parameterTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        List<ProxyMethod> sigmethods = proxyMethods.get(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (sigmethods != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            for (ProxyMethod pm : sigmethods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (returnType == pm.returnType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                     * Found a match: reduce exception types to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                     * greatest set of exceptions that can thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                     * compatibly with the throws clauses of both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                     * overridden methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   579
                    List<Class<?>> legalExceptions = new ArrayList<Class<?>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    collectCompatibleTypes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        exceptionTypes, pm.exceptionTypes, legalExceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    collectCompatibleTypes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        pm.exceptionTypes, exceptionTypes, legalExceptions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    pm.exceptionTypes = new Class[legalExceptions.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    pm.exceptionTypes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        legalExceptions.toArray(pm.exceptionTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            sigmethods = new ArrayList<ProxyMethod>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            proxyMethods.put(sig, sigmethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        sigmethods.add(new ProxyMethod(name, parameterTypes, returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                       exceptionTypes, fromClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * For a given set of proxy methods with the same signature, check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * that their return types are compatible according to the Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Specifically, if there is more than one such method, then all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * of the return types must be reference types, and there must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * one return type that is assignable to each of the rest of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private static void checkReturnTypes(List<ProxyMethod> methods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * If there is only one method with a given signature, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * cannot be a conflict.  This is the only case in which a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * primitive (or void) return type is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (methods.size() < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * List of return types that are not yet known to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         * assignable from ("covered" by) any of the others.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   621
        LinkedList<Class<?>> uncoveredReturnTypes = new LinkedList<Class<?>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    nextNewReturnType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        for (ProxyMethod pm : methods) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   625
            Class<?> newReturnType = pm.returnType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (newReturnType.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    "methods with same signature " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    getFriendlyMethodSignature(pm.methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                               pm.parameterTypes) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    " but incompatible return types: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    newReturnType.getName() + " and others");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            boolean added = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
             * Compare the new return type to the existing uncovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
             * return types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   640
            ListIterator<Class<?>> liter = uncoveredReturnTypes.listIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            while (liter.hasNext()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   642
                Class<?> uncoveredReturnType = liter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                 * If an existing uncovered return type is assignable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                 * to this new one, then we can forget the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if (newReturnType.isAssignableFrom(uncoveredReturnType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    assert !added;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    continue nextNewReturnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                 * If the new return type is assignable to an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                 * uncovered one, then should replace the existing one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                 * with the new one (or just forget the existing one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                 * if the new one has already be put in the list).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                if (uncoveredReturnType.isAssignableFrom(newReturnType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    // (we can assume that each return type is unique)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    if (!added) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        liter.set(newReturnType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        added = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        liter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
             * If we got through the list of existing uncovered return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
             * types without an assignability relationship, then add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
             * the new return type to the list of uncovered ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (!added) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                uncoveredReturnTypes.add(newReturnType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * We shouldn't end up with more than one return type that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * not assignable from any of the others.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (uncoveredReturnTypes.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            ProxyMethod pm = methods.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                "methods with same signature " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                getFriendlyMethodSignature(pm.methodName, pm.parameterTypes) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                " but incompatible return types: " + uncoveredReturnTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * A FieldInfo object contains information about a particular field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * in the class being generated.  The class mirrors the data items of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * the "field_info" structure of the class file format (see JVMS 4.5).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    private class FieldInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        public int accessFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public String descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        public FieldInfo(String name, String descriptor, int accessFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            this.descriptor = descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            this.accessFlags = accessFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
             * Make sure that constant pool indexes are reserved for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
             * following items before starting to write the final class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            cp.getUtf8(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            cp.getUtf8(descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        public void write(DataOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
             * Write all the items of the "field_info" structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
             * See JVMS section 4.5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                                        // u2 access_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            out.writeShort(accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                        // u2 name_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            out.writeShort(cp.getUtf8(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                        // u2 descriptor_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            out.writeShort(cp.getUtf8(descriptor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                                        // u2 attributes_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            out.writeShort(0);  // (no field_info attributes for proxy classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * An ExceptionTableEntry object holds values for the data items of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * an entry in the "exception_table" item of the "Code" attribute of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * "method_info" structures (see JVMS 4.7.3).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private static class ExceptionTableEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        public short startPc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        public short endPc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        public short handlerPc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        public short catchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        public ExceptionTableEntry(short startPc, short endPc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                   short handlerPc, short catchType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            this.startPc = startPc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            this.endPc = endPc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            this.handlerPc = handlerPc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            this.catchType = catchType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * A MethodInfo object contains information about a particular method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * in the class being generated.  This class mirrors the data items of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * the "method_info" structure of the class file format (see JVMS 4.6).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private class MethodInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        public int accessFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        public String descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        public short maxStack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        public short maxLocals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        public ByteArrayOutputStream code = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        public List<ExceptionTableEntry> exceptionTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            new ArrayList<ExceptionTableEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        public short[] declaredExceptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        public MethodInfo(String name, String descriptor, int accessFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            this.descriptor = descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            this.accessFlags = accessFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
             * Make sure that constant pool indexes are reserved for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
             * following items before starting to write the final class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            cp.getUtf8(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            cp.getUtf8(descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            cp.getUtf8("Code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            cp.getUtf8("Exceptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        public void write(DataOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
             * Write all the items of the "method_info" structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
             * See JVMS section 4.6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                                        // u2 access_flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            out.writeShort(accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                                        // u2 name_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            out.writeShort(cp.getUtf8(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                                        // u2 descriptor_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            out.writeShort(cp.getUtf8(descriptor));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                                        // u2 attributes_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            out.writeShort(2);  // (two method_info attributes:)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // Write "Code" attribute. See JVMS section 4.7.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                        // u2 attribute_name_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            out.writeShort(cp.getUtf8("Code"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                                        // u4 attribute_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            out.writeInt(12 + code.size() + 8 * exceptionTable.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                                        // u2 max_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            out.writeShort(maxStack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                        // u2 max_locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            out.writeShort(maxLocals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                        // u2 code_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            out.writeInt(code.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                                        // u1 code[code_length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            code.writeTo(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                        // u2 exception_table_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            out.writeShort(exceptionTable.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            for (ExceptionTableEntry e : exceptionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                                        // u2 start_pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                out.writeShort(e.startPc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                                        // u2 end_pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                out.writeShort(e.endPc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                                        // u2 handler_pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                out.writeShort(e.handlerPc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                        // u2 catch_type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                out.writeShort(e.catchType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                        // u2 attributes_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            out.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            // write "Exceptions" attribute.  See JVMS section 4.7.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                        // u2 attribute_name_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            out.writeShort(cp.getUtf8("Exceptions"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                                        // u4 attributes_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            out.writeInt(2 + 2 * declaredExceptions.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                                        // u2 number_of_exceptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            out.writeShort(declaredExceptions.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        // u2 exception_index_table[number_of_exceptions];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            for (int i = 0; i < declaredExceptions.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                out.writeShort(declaredExceptions[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * A ProxyMethod object represents a proxy method in the proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * being generated: a method whose implementation will encode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * dispatch invocations to the proxy instance's invocation handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    private class ProxyMethod {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        public String methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        public Class[] parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        public Class returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        public Class[] exceptionTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        public Class fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        public String methodFieldName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        private ProxyMethod(String methodName, Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                            Class returnType, Class[] exceptionTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                            Class fromClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            this.methodName = methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            this.parameterTypes = parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            this.returnType = returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            this.exceptionTypes = exceptionTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            this.fromClass = fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            this.methodFieldName = "m" + proxyMethodCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * Return a MethodInfo object for this method, including generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * the code and exception table entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        private MethodInfo generateMethod() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            String desc = getMethodDescriptor(parameterTypes, returnType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            MethodInfo minfo = new MethodInfo(methodName, desc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                ACC_PUBLIC | ACC_FINAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            int[] parameterSlot = new int[parameterTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            int nextSlot = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            for (int i = 0; i < parameterSlot.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                parameterSlot[i] = nextSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                nextSlot += getWordsPerType(parameterTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            int localSlot0 = nextSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            short pc, tryBegin = 0, tryEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            DataOutputStream out = new DataOutputStream(minfo.code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            code_aload(0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            out.writeByte(opc_getfield);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            out.writeShort(cp.getFieldRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                superclassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                handlerFieldName, "Ljava/lang/reflect/InvocationHandler;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            code_aload(0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            out.writeByte(opc_getstatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            out.writeShort(cp.getFieldRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                dotToSlash(className),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                methodFieldName, "Ljava/lang/reflect/Method;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            if (parameterTypes.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                code_ipush(parameterTypes.length, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                out.writeByte(opc_anewarray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                out.writeShort(cp.getClass("java/lang/Object"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    out.writeByte(opc_dup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    code_ipush(i, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                    codeWrapArgument(parameterTypes[i], parameterSlot[i], out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    out.writeByte(opc_aastore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                out.writeByte(opc_aconst_null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            out.writeByte(opc_invokeinterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            out.writeShort(cp.getInterfaceMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                "java/lang/reflect/InvocationHandler",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                "invoke",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                "(Ljava/lang/Object;Ljava/lang/reflect/Method;" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    "[Ljava/lang/Object;)Ljava/lang/Object;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            out.writeByte(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            out.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if (returnType == void.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                out.writeByte(opc_pop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                out.writeByte(opc_return);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                codeUnwrapReturnValue(returnType, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            tryEnd = pc = (short) minfo.code.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   947
            List<Class<?>> catchList = computeUniqueCatchList(exceptionTypes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if (catchList.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   950
                for (Class<?> ex : catchList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    minfo.exceptionTable.add(new ExceptionTableEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        tryBegin, tryEnd, pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        cp.getClass(dotToSlash(ex.getName()))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                out.writeByte(opc_athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                pc = (short) minfo.code.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                minfo.exceptionTable.add(new ExceptionTableEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    tryBegin, tryEnd, pc, cp.getClass("java/lang/Throwable")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                code_astore(localSlot0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                out.writeByte(opc_new);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                out.writeShort(cp.getClass(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                    "java/lang/reflect/UndeclaredThrowableException"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                out.writeByte(opc_dup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                code_aload(localSlot0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                out.writeByte(opc_invokespecial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    "java/lang/reflect/UndeclaredThrowableException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    "<init>", "(Ljava/lang/Throwable;)V"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                out.writeByte(opc_athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            if (minfo.code.size() > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                throw new IllegalArgumentException("code size limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            minfo.maxStack = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            minfo.maxLocals = (short) (localSlot0 + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            minfo.declaredExceptions = new short[exceptionTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            for (int i = 0; i < exceptionTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                minfo.declaredExceptions[i] = cp.getClass(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    dotToSlash(exceptionTypes[i].getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            return minfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         * Generate code for wrapping an argument of the given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * whose value can be found at the specified local variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * index, in order for it to be passed (as an Object) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         * invocation handler's "invoke" method.  The code is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         * to the supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        private void codeWrapArgument(Class type, int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                                      DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (type.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                if (type == int.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                    type == boolean.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    type == byte.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    type == char.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                    type == short.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    code_iload(slot, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                } else if (type == long.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    code_lload(slot, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                } else if (type == float.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    code_fload(slot, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                } else if (type == double.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    code_dload(slot, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                out.writeByte(opc_invokestatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    prim.wrapperClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    "valueOf", prim.wrapperValueOfDesc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                code_aload(slot, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * Generate code for unwrapping a return value of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * type from the invocation handler's "invoke" method (as type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         * Object) to its correct type.  The code is written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        private void codeUnwrapReturnValue(Class type, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            if (type.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                PrimitiveTypeInfo prim = PrimitiveTypeInfo.get(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                out.writeByte(opc_checkcast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                out.writeShort(cp.getClass(prim.wrapperClassName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                out.writeByte(opc_invokevirtual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    prim.wrapperClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    prim.unwrapMethodName, prim.unwrapMethodDesc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                if (type == int.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    type == boolean.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    type == byte.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    type == char.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    type == short.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    out.writeByte(opc_ireturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                } else if (type == long.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    out.writeByte(opc_lreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                } else if (type == float.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    out.writeByte(opc_freturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                } else if (type == double.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    out.writeByte(opc_dreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                out.writeByte(opc_checkcast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                out.writeShort(cp.getClass(dotToSlash(type.getName())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                out.writeByte(opc_areturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         * Generate code for initializing the static field that stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         * the Method object for this proxy method.  The code is written
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
         * to the supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        private void codeFieldInitialization(DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            codeClassForName(fromClass, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            code_ldc(cp.getString(methodName), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            code_ipush(parameterTypes.length, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            out.writeByte(opc_anewarray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            out.writeShort(cp.getClass("java/lang/Class"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                out.writeByte(opc_dup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                code_ipush(i, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                if (parameterTypes[i].isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    PrimitiveTypeInfo prim =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                        PrimitiveTypeInfo.get(parameterTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    out.writeByte(opc_getstatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    out.writeShort(cp.getFieldRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                        prim.wrapperClassName, "TYPE", "Ljava/lang/Class;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    codeClassForName(parameterTypes[i], out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                out.writeByte(opc_aastore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            out.writeByte(opc_invokevirtual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                "java/lang/Class",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                "getMethod",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                "(Ljava/lang/String;[Ljava/lang/Class;)" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                "Ljava/lang/reflect/Method;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            out.writeByte(opc_putstatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            out.writeShort(cp.getFieldRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                dotToSlash(className),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                methodFieldName, "Ljava/lang/reflect/Method;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * Generate the constructor method for the proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    private MethodInfo generateConstructor() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        MethodInfo minfo = new MethodInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            "<init>", "(Ljava/lang/reflect/InvocationHandler;)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            ACC_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        DataOutputStream out = new DataOutputStream(minfo.code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        code_aload(0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        code_aload(1, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        out.writeByte(opc_invokespecial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            superclassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            "<init>", "(Ljava/lang/reflect/InvocationHandler;)V"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        out.writeByte(opc_return);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        minfo.maxStack = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        minfo.maxLocals = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        minfo.declaredExceptions = new short[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        return minfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Generate the static initializer method for the proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    private MethodInfo generateStaticInitializer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        MethodInfo minfo = new MethodInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            "<clinit>", "()V", ACC_STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        int localSlot0 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        short pc, tryBegin = 0, tryEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        DataOutputStream out = new DataOutputStream(minfo.code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            for (ProxyMethod pm : sigmethods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                pm.codeFieldInitialization(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        out.writeByte(opc_return);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        tryEnd = pc = (short) minfo.code.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        minfo.exceptionTable.add(new ExceptionTableEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            tryBegin, tryEnd, pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            cp.getClass("java/lang/NoSuchMethodException")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        code_astore(localSlot0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        out.writeByte(opc_new);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        out.writeShort(cp.getClass("java/lang/NoSuchMethodError"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        out.writeByte(opc_dup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        code_aload(localSlot0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        out.writeByte(opc_invokevirtual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            "java/lang/Throwable", "getMessage", "()Ljava/lang/String;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        out.writeByte(opc_invokespecial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            "java/lang/NoSuchMethodError", "<init>", "(Ljava/lang/String;)V"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        out.writeByte(opc_athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        pc = (short) minfo.code.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        minfo.exceptionTable.add(new ExceptionTableEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            tryBegin, tryEnd, pc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            cp.getClass("java/lang/ClassNotFoundException")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        code_astore(localSlot0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        out.writeByte(opc_new);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        out.writeShort(cp.getClass("java/lang/NoClassDefFoundError"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        out.writeByte(opc_dup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        code_aload(localSlot0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        out.writeByte(opc_invokevirtual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            "java/lang/Throwable", "getMessage", "()Ljava/lang/String;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        out.writeByte(opc_invokespecial);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            "java/lang/NoClassDefFoundError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            "<init>", "(Ljava/lang/String;)V"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        out.writeByte(opc_athrow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (minfo.code.size() > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            throw new IllegalArgumentException("code size limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        minfo.maxStack = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        minfo.maxLocals = (short) (localSlot0 + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        minfo.declaredExceptions = new short[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        return minfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * =============== Code Generation Utility Methods ===============
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * The following methods generate code for the load or store operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * indicated by their name for the given local variable.  The code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * written to the supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    private void code_iload(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        codeLocalLoadStore(lvar, opc_iload, opc_iload_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    private void code_lload(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        codeLocalLoadStore(lvar, opc_lload, opc_lload_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    private void code_fload(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        codeLocalLoadStore(lvar, opc_fload, opc_fload_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    private void code_dload(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        codeLocalLoadStore(lvar, opc_dload, opc_dload_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    private void code_aload(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        codeLocalLoadStore(lvar, opc_aload, opc_aload_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
//  private void code_istore(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
//      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
//  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
//      codeLocalLoadStore(lvar, opc_istore, opc_istore_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
//  private void code_lstore(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
//      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
//  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
//      codeLocalLoadStore(lvar, opc_lstore, opc_lstore_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
//  private void code_fstore(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
//      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
//  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
//      codeLocalLoadStore(lvar, opc_fstore, opc_fstore_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
//  private void code_dstore(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
//      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
//  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
//      codeLocalLoadStore(lvar, opc_dstore, opc_dstore_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
//  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    private void code_astore(int lvar, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        codeLocalLoadStore(lvar, opc_astore, opc_astore_0, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * Generate code for a load or store instruction for the given local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * variable.  The code is written to the supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * "opcode" indicates the opcode form of the desired load or store
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * instruction that takes an explicit local variable index, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * "opcode_0" indicates the corresponding form of the instruction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * with the implicit index 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    private void codeLocalLoadStore(int lvar, int opcode, int opcode_0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                                    DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        assert lvar >= 0 && lvar <= 0xFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        if (lvar <= 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            out.writeByte(opcode_0 + lvar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        } else if (lvar <= 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            out.writeByte(opcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            out.writeByte(lvar & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
             * Use the "wide" instruction modifier for local variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
             * indexes that do not fit into an unsigned byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            out.writeByte(opc_wide);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            out.writeByte(opcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            out.writeShort(lvar & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * Generate code for an "ldc" instruction for the given constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * index (the "ldc_w" instruction is used if the index does not fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * into an unsigned byte).  The code is written to the supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    private void code_ldc(int index, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        assert index >= 0 && index <= 0xFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        if (index <= 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            out.writeByte(opc_ldc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            out.writeByte(index & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            out.writeByte(opc_ldc_w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            out.writeShort(index & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Generate code to push a constant integer value on to the operand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * stack, using the "iconst_<i>", "bipush", or "sipush" instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * depending on the size of the value.  The code is written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * supplied stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    private void code_ipush(int value, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        if (value >= -1 && value <= 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            out.writeByte(opc_iconst_0 + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            out.writeByte(opc_bipush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            out.writeByte(value & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            out.writeByte(opc_sipush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            out.writeShort(value & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * Generate code to invoke the Class.forName with the name of the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * class to get its Class object at runtime.  The code is written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * the supplied stream.  Note that the code generated by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * may caused the checked ClassNotFoundException to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    private void codeClassForName(Class cl, DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        code_ldc(cp.getString(cl.getName()), out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        out.writeByte(opc_invokestatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        out.writeShort(cp.getMethodRef(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            "java/lang/Class",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            "forName", "(Ljava/lang/String;)Ljava/lang/Class;"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * ==================== General Utility Methods ====================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * Convert a fully qualified class name that uses '.' as the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * separator, the external representation used by the Java language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * and APIs, to a fully qualified class name that uses '/' as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * package separator, the representation used in the class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * format (see JVMS section 4.2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    private static String dotToSlash(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        return name.replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * Return the "method descriptor" string for a method with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * parameter types and return type.  See JVMS section 4.3.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    private static String getMethodDescriptor(Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                                              Class returnType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        return getParameterDescriptors(parameterTypes) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            ((returnType == void.class) ? "V" : getFieldType(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * Return the list of "parameter descriptor" strings enclosed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * parentheses corresponding to the given parameter types (in other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * words, a method descriptor without a return descriptor).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * string is useful for constructing string keys for methods without
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * regard to their return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    private static String getParameterDescriptors(Class[] parameterTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        StringBuilder desc = new StringBuilder("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            desc.append(getFieldType(parameterTypes[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        desc.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        return desc.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * Return the "field type" string for the given type, appropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * a field descriptor, a parameter descriptor, or a return descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * other than "void".  See JVMS section 4.3.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    private static String getFieldType(Class type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        if (type.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            return PrimitiveTypeInfo.get(type).baseTypeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        } else if (type.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
             * According to JLS 20.3.2, the getName() method on Class does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
             * return the VM type descriptor format for array classes (only);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
             * using that should be quicker than the otherwise obvious code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
             *     return "[" + getTypeDescriptor(type.getComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            return type.getName().replace('.', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            return "L" + dotToSlash(type.getName()) + ";";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * Returns a human-readable string representing the signature of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * method with the given name and parameter types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    private static String getFriendlyMethodSignature(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                                                     Class[] parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        StringBuilder sig = new StringBuilder(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        sig.append('(');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                sig.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            Class parameterType = parameterTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            int dimensions = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            while (parameterType.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                parameterType = parameterType.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                dimensions++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            sig.append(parameterType.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            while (dimensions-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                sig.append("[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        sig.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        return sig.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * Return the number of abstract "words", or consecutive local variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * indexes, required to contain a value of the given type.  See JVMS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * section 3.6.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * Note that the original version of the JVMS contained a definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * this abstract notion of a "word" in section 3.4, but that definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * was removed for the second edition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    private static int getWordsPerType(Class type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        if (type == long.class || type == double.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * Add to the given list all of the types in the "from" array that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * are not already contained in the list and are assignable to at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * least one of the types in the "with" array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * This method is useful for computing the greatest common set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * declared exceptions from duplicate methods inherited from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * different interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1524
    private static void collectCompatibleTypes(Class<?>[] from,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1525
                                               Class<?>[] with,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1526
                                               List<Class<?>> list)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        for (int i = 0; i < from.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            if (!list.contains(from[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                for (int j = 0; j < with.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    if (with[j].isAssignableFrom(from[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                        list.add(from[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * Given the exceptions declared in the throws clause of a proxy method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * compute the exceptions that need to be caught from the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * handler's invoke method and rethrown intact in the method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * implementation before catching other Throwables and wrapping them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * in UndeclaredThrowableExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * The exceptions to be caught are returned in a List object.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * exception in the returned list is guaranteed to not be a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * any of the other exceptions in the list, so the catch blocks for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * these exceptions may be generated in any order relative to each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Error and RuntimeException are each always contained by the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * list (if none of their superclasses are contained), since those
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * unchecked exceptions should always be rethrown intact, and thus their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * subclasses will never appear in the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * The returned List will be empty if java.lang.Throwable is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * given list of declared exceptions, indicating that no exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * need to be caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1561
    private static List<Class<?>> computeUniqueCatchList(Class<?>[] exceptions) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1562
        List<Class<?>> uniqueList = new ArrayList<Class<?>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                                                // unique exceptions to catch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        uniqueList.add(Error.class);            // always catch/rethrow these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        uniqueList.add(RuntimeException.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    nextException:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        for (int i = 0; i < exceptions.length; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1570
            Class<?> ex = exceptions[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (ex.isAssignableFrom(Throwable.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                 * If Throwable is declared to be thrown by the proxy method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                 * then no catch blocks are necessary, because the invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                 * can, at most, throw Throwable anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                uniqueList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            } else if (!Throwable.class.isAssignableFrom(ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                 * Ignore types that cannot be thrown by the invoke method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
             * Compare this exception against the current list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
             * exceptions that need to be caught:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            for (int j = 0; j < uniqueList.size();) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1590
                Class<?> ex2 = uniqueList.get(j);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                if (ex2.isAssignableFrom(ex)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                     * if a superclass of this exception is already on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                     * the list to catch, then ignore this one and continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                    continue nextException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                } else if (ex.isAssignableFrom(ex2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                     * if a subclass of this exception is on the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                     * to catch, then remove it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                    uniqueList.remove(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                    j++;        // else continue comparing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            // This exception is unique (so far): add it to the list to catch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            uniqueList.add(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        return uniqueList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * A PrimitiveTypeInfo object contains assorted information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * a primitive type in its public fields.  The struct for a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * primitive type can be obtained using the static "get" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    private static class PrimitiveTypeInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        /** "base type" used in various descriptors (see JVMS section 4.3.2) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        public String baseTypeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        /** name of corresponding wrapper class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        public String wrapperClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        /** method descriptor for wrapper class "valueOf" factory method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        public String wrapperValueOfDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        /** name of wrapper class method for retrieving primitive value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        public String unwrapMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        /** descriptor of same method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        public String unwrapMethodDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        private static Map<Class,PrimitiveTypeInfo> table =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            new HashMap<Class,PrimitiveTypeInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            add(byte.class, Byte.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            add(char.class, Character.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            add(double.class, Double.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            add(float.class, Float.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            add(int.class, Integer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            add(long.class, Long.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            add(short.class, Short.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            add(boolean.class, Boolean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        private static void add(Class primitiveClass, Class wrapperClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            table.put(primitiveClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                      new PrimitiveTypeInfo(primitiveClass, wrapperClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        private PrimitiveTypeInfo(Class primitiveClass, Class wrapperClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            assert primitiveClass.isPrimitive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            baseTypeString =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                Array.newInstance(primitiveClass, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                .getClass().getName().substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            wrapperClassName = dotToSlash(wrapperClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            wrapperValueOfDesc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                "(" + baseTypeString + ")L" + wrapperClassName + ";";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            unwrapMethodName = primitiveClass.getName() + "Value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            unwrapMethodDesc = "()" + baseTypeString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        public static PrimitiveTypeInfo get(Class cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            return table.get(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * A ConstantPool object represents the constant pool of a class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * being generated.  This representation of a constant pool is designed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * specifically for use by ProxyGenerator; in particular, it assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * that constant pool entries will not need to be resorted (for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * by their type, as the Java compiler does), so that the final index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * value can be assigned and used when an entry is first created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * Note that new entries cannot be created after the constant pool has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * been written to a class file.  To prevent such logic errors, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * ConstantPool instance can be marked "read only", so that further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * attempts to add new entries will fail with a runtime exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * See JVMS section 4.4 for more information about the constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * of a class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    private static class ConstantPool {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
         * list of constant pool entries, in constant pool index order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
         * This list is used when writing the constant pool to a stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
         * and for assigning the next index value.  Note that element 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
         * of this list corresponds to constant pool index 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        private List<Entry> pool = new ArrayList<Entry>(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
         * maps constant pool data of all types to constant pool indexes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
         * This map is used to look up the index of an existing entry for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
         * values of all types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        private Map<Object,Short> map = new HashMap<Object,Short>(16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        /** true if no new constant pool entries may be added */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        private boolean readOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
         * Get or assign the index for a CONSTANT_Utf8 entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        public short getUtf8(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            return getValue(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
         * Get or assign the index for a CONSTANT_Integer entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        public short getInteger(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            return getValue(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
         * Get or assign the index for a CONSTANT_Float entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        public short getFloat(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            return getValue(new Float(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
         * Get or assign the index for a CONSTANT_Class entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        public short getClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            short utf8Index = getUtf8(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            return getIndirect(new IndirectEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                CONSTANT_CLASS, utf8Index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
         * Get or assign the index for a CONSTANT_String entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        public short getString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            short utf8Index = getUtf8(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            return getIndirect(new IndirectEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                CONSTANT_STRING, utf8Index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
         * Get or assign the index for a CONSTANT_FieldRef entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        public short getFieldRef(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                                 String name, String descriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            short classIndex = getClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            short nameAndTypeIndex = getNameAndType(name, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            return getIndirect(new IndirectEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                CONSTANT_FIELD, classIndex, nameAndTypeIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
         * Get or assign the index for a CONSTANT_MethodRef entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        public short getMethodRef(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                                  String name, String descriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            short classIndex = getClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            short nameAndTypeIndex = getNameAndType(name, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            return getIndirect(new IndirectEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                CONSTANT_METHOD, classIndex, nameAndTypeIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         * Get or assign the index for a CONSTANT_InterfaceMethodRef entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        public short getInterfaceMethodRef(String className, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                                           String descriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            short classIndex = getClass(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            short nameAndTypeIndex = getNameAndType(name, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            return getIndirect(new IndirectEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                CONSTANT_INTERFACEMETHOD, classIndex, nameAndTypeIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
         * Get or assign the index for a CONSTANT_NameAndType entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        public short getNameAndType(String name, String descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            short nameIndex = getUtf8(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            short descriptorIndex = getUtf8(descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            return getIndirect(new IndirectEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                CONSTANT_NAMEANDTYPE, nameIndex, descriptorIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
         * Set this ConstantPool instance to be "read only".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
         * After this method has been called, further requests to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
         * an index for a non-existent entry will cause an InternalError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
         * to be thrown instead of creating of the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        public void setReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            readOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
         * Write this constant pool to a stream as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
         * the class file format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
         * This consists of writing the "constant_pool_count" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
         * "constant_pool[]" items of the "ClassFile" structure, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
         * described in JVMS section 4.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        public void write(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            DataOutputStream dataOut = new DataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            // constant_pool_count: number of entries plus one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            dataOut.writeShort(pool.size() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            for (Entry e : pool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                e.write(dataOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
         * Add a new constant pool entry and return its index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        private short addEntry(Entry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            pool.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
             * Note that this way of determining the index of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
             * added entry is wrong if this pool supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
             * CONSTANT_Long or CONSTANT_Double entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            if (pool.size() >= 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                    "constant pool size limit exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            return (short) pool.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
         * Get or assign the index for an entry of a type that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
         * a direct value.  The type of the given object determines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
         * type of the desired entry as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
         *      java.lang.String        CONSTANT_Utf8
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
         *      java.lang.Integer       CONSTANT_Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
         *      java.lang.Float         CONSTANT_Float
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
         *      java.lang.Long          CONSTANT_Long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
         *      java.lang.Double        CONSTANT_DOUBLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        private short getValue(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            Short index = map.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                return index.shortValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                if (readOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                    throw new InternalError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                        "late constant pool addition: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                short i = addEntry(new ValueEntry(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                map.put(key, new Short(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
         * Get or assign the index for an entry of a type that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
         * references to other constant pool entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        private short getIndirect(IndirectEntry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            Short index = map.get(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                return index.shortValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                if (readOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                    throw new InternalError("late constant pool addition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                short i = addEntry(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                map.put(e, new Short(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
         * Entry is the abstact superclass of all constant pool entry types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
         * that can be stored in the "pool" list; its purpose is to define a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
         * common method for writing constant pool entries to a class file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        private static abstract class Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            public abstract void write(DataOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
         * ValueEntry represents a constant pool entry of a type that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
         * contains a direct value (see the comments for the "getValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
         * method for a list of such types).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
         * ValueEntry objects are not used as keys for their entries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
         * Map "map", so no useful hashCode or equals methods are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        private static class ValueEntry extends Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            public ValueEntry(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            public void write(DataOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                if (value instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                    out.writeByte(CONSTANT_UTF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                    out.writeUTF((String) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                } else if (value instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                    out.writeByte(CONSTANT_INTEGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                    out.writeInt(((Integer) value).intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                } else if (value instanceof Float) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                    out.writeByte(CONSTANT_FLOAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                    out.writeFloat(((Float) value).floatValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                } else if (value instanceof Long) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                    out.writeByte(CONSTANT_LONG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                    out.writeLong(((Long) value).longValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                } else if (value instanceof Double) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                    out.writeDouble(CONSTANT_DOUBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                    out.writeDouble(((Double) value).doubleValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                    throw new InternalError("bogus value entry: " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         * IndirectEntry represents a constant pool entry of a type that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
         * references other constant pool entries, i.e., the following types:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
         *      CONSTANT_Class, CONSTANT_String, CONSTANT_Fieldref,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
         *      CONSTANT_Methodref, CONSTANT_InterfaceMethodref, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
         *      CONSTANT_NameAndType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
         * Each of these entry types contains either one or two indexes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
         * other constant pool entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
         * IndirectEntry objects are used as the keys for their entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
         * the Map "map", so the hashCode and equals methods are overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
         * to allow matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        private static class IndirectEntry extends Entry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            private int tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            private short index0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            private short index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
             * Construct an IndirectEntry for a constant pool entry type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
             * that contains one index of another entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            public IndirectEntry(int tag, short index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                this.index0 = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                this.index1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
             * Construct an IndirectEntry for a constant pool entry type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
             * that contains two indexes for other entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            public IndirectEntry(int tag, short index0, short index1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                this.index0 = index0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                this.index1 = index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            public void write(DataOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                out.writeByte(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                out.writeShort(index0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                 * If this entry type contains two indexes, write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                 * out the second, too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                if (tag == CONSTANT_FIELD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    tag == CONSTANT_METHOD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                    tag == CONSTANT_INTERFACEMETHOD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                    tag == CONSTANT_NAMEANDTYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                    out.writeShort(index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                return tag + index0 + index1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                if (obj instanceof IndirectEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                    IndirectEntry other = (IndirectEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    if (tag == other.tag &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                        index0 == other.index0 && index1 == other.index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
}