jdk/src/share/classes/sun/reflect/MethodAccessorGenerator.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/** Generator for sun.reflect.MethodAccessor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    sun.reflect.ConstructorAccessor objects using bytecodes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    implement reflection. A java.lang.reflect.Method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    java.lang.reflect.Constructor object can delegate its invoke or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    newInstance method to an accessor using native code or to one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    generated by this class. (Methods and Constructors were merged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    together in this class to ensure maximum code sharing.) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class MethodAccessorGenerator extends AccessorGenerator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final short NUM_BASE_CPOOL_ENTRIES   = (short) 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // One for invoke() plus one for constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final short NUM_METHODS              = (short) 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // Only used if forSerialization is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final short NUM_SERIALIZATION_CPOOL_ENTRIES = (short) 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static volatile int methodSymnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static volatile int constructorSymnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static volatile int serializationConstructorSymnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Class   declaringClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private Class[] parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Class   returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean isConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean forSerialization;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private short targetMethodRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private short invokeIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private short invokeDescriptorIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Constant pool index of CONSTANT_Class_info for first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // non-primitive parameter type. Should be incremented by 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private short nonPrimitiveParametersBaseIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    MethodAccessorGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** This routine is not thread-safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public MethodAccessor generateMethod(Class declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                         String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                         Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                         Class   returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                         Class[] checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                         int modifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return (MethodAccessor) generate(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                         name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                         parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                         returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                         checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                         modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                         false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                         false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                         null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** This routine is not thread-safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public ConstructorAccessor generateConstructor(Class declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                                   Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                                   Class[] checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                                   int modifiers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return (ConstructorAccessor) generate(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                              "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                              parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                              Void.TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                              checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                              modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                              true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                              false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                              null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** This routine is not thread-safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public SerializationConstructorAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    generateSerializationConstructor(Class declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                     Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                     Class[] checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                     int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                     Class targetConstructorClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return (SerializationConstructorAccessorImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            generate(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                     "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                     parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                     Void.TYPE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                     checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                     modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                     true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                     true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                     targetConstructorClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** This routine is not thread-safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private MagicAccessorImpl generate(final Class declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                       String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                       Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                       Class   returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                       Class[] checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                       int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                       boolean isConstructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                       boolean forSerialization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                       Class serializationTargetClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        ByteVector vec = ByteVectorFactory.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        asm = new ClassFileAssembler(vec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.declaringClass = declaringClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.parameterTypes = parameterTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.returnType = returnType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.modifiers = modifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.isConstructor = isConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.forSerialization = forSerialization;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        asm.emitMagicAndVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // Constant pool entries:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        // ( * = Boxing information: optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // (+  = Shared entries provided by AccessorGenerator)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // (^  = Only present if generating SerializationConstructorAccessor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        //     [UTF-8] [This class's name]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        //     [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        //     [UTF-8] "sun/reflect/{MethodAccessorImpl,ConstructorAccessorImpl,SerializationConstructorAccessorImpl}"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        //     [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        //     [UTF-8] [Target class's name]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        //     [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // ^   [UTF-8] [Serialization: Class's name in which to invoke constructor]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // ^   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        //     [UTF-8] target method or constructor name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        //     [UTF-8] target method or constructor signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        //     [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        //     [CONSTANT_Methodref_info or CONSTANT_InterfaceMethodref_info] for target method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        //     [UTF-8] "invoke" or "newInstance"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        //     [UTF-8] invoke or newInstance descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        //     [UTF-8] descriptor for type of non-primitive parameter 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        //     [CONSTANT_Class_info] for type of non-primitive parameter 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        //     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //     [UTF-8] descriptor for type of non-primitive parameter n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        //     [CONSTANT_Class_info] for type of non-primitive parameter n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // +   [UTF-8] "java/lang/Exception"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // +   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // +   [UTF-8] "java/lang/ClassCastException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // +   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // +   [UTF-8] "java/lang/NullPointerException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // +   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // +   [UTF-8] "java/lang/IllegalArgumentException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // +   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // +   [UTF-8] "java/lang/InvocationTargetException"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // +   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // +   [UTF-8] "<init>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // +   [UTF-8] "()V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // +   [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // +   [CONSTANT_Methodref_info] for NullPointerException's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // +   [CONSTANT_Methodref_info] for IllegalArgumentException's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // +   [UTF-8] "(Ljava/lang/String;)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // +   [CONSTANT_NameAndType_info] for "<init>(Ljava/lang/String;)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // +   [CONSTANT_Methodref_info] for IllegalArgumentException's constructor taking a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // +   [UTF-8] "(Ljava/lang/Throwable;)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // +   [CONSTANT_NameAndType_info] for "<init>(Ljava/lang/Throwable;)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // +   [CONSTANT_Methodref_info] for InvocationTargetException's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // +   [CONSTANT_Methodref_info] for "super()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // +   [UTF-8] "java/lang/Object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // +   [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // +   [UTF-8] "toString"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // +   [UTF-8] "()Ljava/lang/String;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // +   [CONSTANT_NameAndType_info] for "toString()Ljava/lang/String;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // +   [CONSTANT_Methodref_info] for Object's toString method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // +   [UTF-8] "Code"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // +   [UTF-8] "Exceptions"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        //  *  [UTF-8] "java/lang/Boolean"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        //  *  [UTF-8] "(Z)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        //  *  [UTF-8] "booleanValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        //  *  [UTF-8] "()Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        //  *  [UTF-8] "java/lang/Byte"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        //  *  [UTF-8] "(B)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        //  *  [UTF-8] "byteValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        //  *  [UTF-8] "()B"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        //  *  [UTF-8] "java/lang/Character"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        //  *  [UTF-8] "(C)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        //  *  [UTF-8] "charValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        //  *  [UTF-8] "()C"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        //  *  [UTF-8] "java/lang/Double"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        //  *  [UTF-8] "(D)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        //  *  [UTF-8] "doubleValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        //  *  [UTF-8] "()D"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        //  *  [UTF-8] "java/lang/Float"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        //  *  [UTF-8] "(F)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        //  *  [UTF-8] "floatValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        //  *  [UTF-8] "()F"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        //  *  [UTF-8] "java/lang/Integer"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        //  *  [UTF-8] "(I)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        //  *  [UTF-8] "intValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //  *  [UTF-8] "()I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        //  *  [UTF-8] "java/lang/Long"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        //  *  [UTF-8] "(J)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        //  *  [UTF-8] "longValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        //  *  [UTF-8] "()J"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        //  *  [UTF-8] "java/lang/Short"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        //  *  [CONSTANT_Class_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        //  *  [UTF-8] "(S)V"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        //  *  [UTF-8] "shortValue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        //  *  [UTF-8] "()S"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        //  *  [CONSTANT_NameAndType_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        //  *  [CONSTANT_Methodref_info] for above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        short numCPEntries = NUM_BASE_CPOOL_ENTRIES + NUM_COMMON_CPOOL_ENTRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        boolean usesPrimitives = usesPrimitiveTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (usesPrimitives) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            numCPEntries += NUM_BOXING_CPOOL_ENTRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (forSerialization) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            numCPEntries += NUM_SERIALIZATION_CPOOL_ENTRIES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Add in variable-length number of entries to be able to describe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // non-primitive parameter types and checked exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        numCPEntries += (short) (2 * numNonPrimitiveParameterTypes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        asm.emitShort(add(numCPEntries, S1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        final String generatedName = generateName(isConstructor, forSerialization);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        asm.emitConstantPoolUTF8(generatedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        asm.emitConstantPoolClass(asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        thisClass = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (forSerialization) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                asm.emitConstantPoolUTF8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    ("sun/reflect/SerializationConstructorAccessorImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                asm.emitConstantPoolUTF8("sun/reflect/ConstructorAccessorImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            asm.emitConstantPoolUTF8("sun/reflect/MethodAccessorImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        asm.emitConstantPoolClass(asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        superClass = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        asm.emitConstantPoolUTF8(getClassName(declaringClass, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        asm.emitConstantPoolClass(asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        targetClass = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        short serializationTargetClassIdx = (short) 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (forSerialization) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            asm.emitConstantPoolUTF8(getClassName(serializationTargetClass, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            asm.emitConstantPoolClass(asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            serializationTargetClassIdx = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        asm.emitConstantPoolUTF8(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        asm.emitConstantPoolUTF8(buildInternalSignature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        asm.emitConstantPoolNameAndType(sub(asm.cpi(), S1), asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            asm.emitConstantPoolInterfaceMethodref(targetClass, asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (forSerialization) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                asm.emitConstantPoolMethodref(serializationTargetClassIdx, asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                asm.emitConstantPoolMethodref(targetClass, asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        targetMethodRef = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            asm.emitConstantPoolUTF8("newInstance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            asm.emitConstantPoolUTF8("invoke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        invokeIdx = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            asm.emitConstantPoolUTF8("([Ljava/lang/Object;)Ljava/lang/Object;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            asm.emitConstantPoolUTF8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                ("(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        invokeDescriptorIdx = asm.cpi();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // Output class information for non-primitive parameter types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        nonPrimitiveParametersBaseIdx = add(asm.cpi(), S2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            Class c = parameterTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if (!isPrimitive(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                asm.emitConstantPoolUTF8(getClassName(c, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                asm.emitConstantPoolClass(asm.cpi());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Entries common to FieldAccessor, MethodAccessor and ConstructorAccessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        emitCommonConstantPoolEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Boxing entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (usesPrimitives) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            emitBoxingContantPoolEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (asm.cpi() != numCPEntries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw new InternalError("Adjust this code (cpi = " + asm.cpi() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                    ", numCPEntries = " + numCPEntries + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // Access flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        asm.emitShort(ACC_PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        asm.emitShort(thisClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // Superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        asm.emitShort(superClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // Interfaces count and interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        asm.emitShort(S0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // Fields count and fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        asm.emitShort(S0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Methods count and methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        asm.emitShort(NUM_METHODS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        emitConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        emitInvoke();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // Additional attributes (none)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        asm.emitShort(S0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // Load class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        vec.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        final byte[] bytes = vec.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Note: the class loader is the only thing that really matters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // here -- it's important to get the generated code into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // same namespace as the target class. Since the generated code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // is privileged anyway, the protection domain probably doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        // matter.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   395
        return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   396
            new PrivilegedAction<MagicAccessorImpl>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   397
                public MagicAccessorImpl run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   399
                        return (MagicAccessorImpl)
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   400
                        ClassDefiner.defineClass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                (generatedName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                 bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                 bytes.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                 declaringClass.getClassLoader()).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                            throw (InternalError)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                new InternalError().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                            throw (InternalError)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                new InternalError().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /** This emits the code for either invoke() or newInstance() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private void emitInvoke() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // NOTE that this code will only handle 65535 parameters since we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        // use the sipush instruction to get the array index on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // operand stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (parameterTypes.length > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new InternalError("Can't handle more than 65535 parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // Generate code into fresh code buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        ClassFileAssembler cb = new ClassFileAssembler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // 1 incoming argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            cb.setMaxLocals(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            // 2 incoming arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            cb.setMaxLocals(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        short illegalArgStartPC = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            // Instantiate target class before continuing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // new <target class type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            cb.opc_new(targetClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            // Setup before iterating down argument list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (isPrimitive(returnType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                // new <boxing type for primitive type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // ... (see below:)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                // invokespecial <constructor for boxing type for primitive type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // areturn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                cb.opc_new(indexForPrimitiveType(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // Get target object on operand stack if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            // We need to do an explicit null check here; we won't see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // NullPointerExceptions from the invoke bytecode, since it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            // covered by an exception handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (!isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                // aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                // ifnonnull <checkcast label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                // new <NullPointerException>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                // invokespecial <NullPointerException ctor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                // athrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                // <checkcast label:>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                // aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                // checkcast <target class's type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                Label l = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                cb.opc_ifnonnull(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                cb.opc_new(nullPointerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                cb.opc_invokespecial(nullPointerCtorIdx, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                l.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                illegalArgStartPC = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                cb.opc_checkcast(targetClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // Have to check length of incoming array and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // IllegalArgumentException if not correct. A concession to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // JCK (isn't clearly specified in the spec): we allow null in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        // case where the argument list is zero length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // if no-arg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        //   aload_2 | aload_1 (Method | Constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        //   ifnull <success label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        // aload_2 | aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // arraylength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // sipush <num parameter types>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // if_icmpeq <success label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // new <IllegalArgumentException>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // invokespecial <IllegalArgumentException ctor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // athrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // <success label:>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        Label successLabel = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (parameterTypes.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            cb.opc_ifnull(successLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        cb.opc_arraylength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        cb.opc_sipush((short) parameterTypes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        cb.opc_if_icmpeq(successLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        cb.opc_new(illegalArgumentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        cb.opc_invokespecial(illegalArgumentCtorIdx, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        successLabel.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // Iterate through incoming actual parameters, ensuring that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        // is compatible with the formal parameter type, and pushing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // actual on the operand stack (unboxing and widening if necessary).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        short paramTypeCPIdx = nonPrimitiveParametersBaseIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Label nextParamLabel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        byte count = 1; // both invokeinterface opcode's "count" as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // num args of other invoke bytecodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            Class paramType = parameterTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            count += (byte) typeSizeInStackSlots(paramType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (nextParamLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                nextParamLabel.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                nextParamLabel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // aload_2 | aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            // sipush <index>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            // aaload
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            cb.opc_sipush((short) i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            cb.opc_aaload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            if (isPrimitive(paramType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                // Unboxing code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                // Put parameter into temporary local variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                // astore_3 | astore_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    cb.opc_astore_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    cb.opc_astore_3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                // repeat for all possible widening conversions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                //   aload_3 | aload_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                //   instanceof <primitive boxing type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                //   ifeq <next unboxing label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                //   aload_3 | aload_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                //   checkcast <primitive boxing type> // Note: this is "redundant",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                //                                     // but necessary for the verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                //   invokevirtual <unboxing method>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                //   <widening conversion bytecode, if necessary>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                //   goto <next parameter label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                // <next unboxing label:> ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                // last unboxing label:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                //   new <IllegalArgumentException>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                //   dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                //   invokespecial <IllegalArgumentException ctor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                //   athrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                Label l = null; // unboxing label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                nextParamLabel = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                for (int j = 0; j < primitiveTypes.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    Class c = primitiveTypes[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    if (canWidenTo(c, paramType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                            l.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        // Emit checking and unboxing code for this type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                            cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                            cb.opc_aload_3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        cb.opc_instanceof(indexForPrimitiveType(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        l = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        cb.opc_ifeq(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            cb.opc_aload_3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        cb.opc_checkcast(indexForPrimitiveType(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        cb.opc_invokevirtual(unboxingMethodForPrimitiveType(c),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                             0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                             typeSizeInStackSlots(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        emitWideningBytecodeForPrimitiveConversion(cb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                                                   c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                                                   paramType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        cb.opc_goto(nextParamLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    throw new InternalError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        ("Must have found at least identity conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                // Fell through; given object is null or invalid. According to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                // the spec, we can throw IllegalArgumentException for both of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                // these cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                l.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                cb.opc_new(illegalArgumentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                cb.opc_invokespecial(illegalArgumentCtorIdx, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                // Emit appropriate checkcast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                cb.opc_checkcast(paramTypeCPIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                paramTypeCPIdx = add(paramTypeCPIdx, S2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                // Fall through to next argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // Bind last goto if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (nextParamLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            nextParamLabel.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        short invokeStartPC = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        // OK, ready to perform the invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            cb.opc_invokespecial(targetMethodRef, count, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                cb.opc_invokestatic(targetMethodRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                    count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                    typeSizeInStackSlots(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    cb.opc_invokeinterface(targetMethodRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                           count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                           count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                           typeSizeInStackSlots(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    cb.opc_invokevirtual(targetMethodRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                         count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                         typeSizeInStackSlots(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        short invokeEndPC = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (!isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            // Box return value if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if (isPrimitive(returnType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                cb.opc_invokespecial(ctorIndexForPrimitiveType(returnType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                                     typeSizeInStackSlots(returnType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                     0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            } else if (returnType == Void.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                cb.opc_aconst_null();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        cb.opc_areturn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        // We generate two exception handlers; one which is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // for catching ClassCastException and NullPointerException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // throwing IllegalArgumentException, and the other which catches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        // all java/lang/Throwable objects thrown from the target method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // and wraps them in InvocationTargetExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        short classCastHandler = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        // ClassCast, etc. exception handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        cb.setStack(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        cb.opc_invokespecial(toStringIdx, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        cb.opc_new(illegalArgumentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        cb.opc_dup_x1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        cb.opc_swap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        cb.opc_invokespecial(illegalArgumentStringCtorIdx, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        short invocationTargetHandler = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        // InvocationTargetException exception handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        cb.setStack(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        cb.opc_new(invocationTargetClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        cb.opc_dup_x1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        cb.opc_swap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        cb.opc_invokespecial(invocationTargetCtorIdx, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Generate exception table. We cover the entire code sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // with an exception handler which catches ClassCastException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        // converts it into an IllegalArgumentException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        ClassFileAssembler exc = new ClassFileAssembler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        exc.emitShort(illegalArgStartPC);       // start PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        exc.emitShort(invokeStartPC);           // end PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        exc.emitShort(classCastHandler);        // handler PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        exc.emitShort(classCastClass);          // catch type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        exc.emitShort(illegalArgStartPC);       // start PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        exc.emitShort(invokeStartPC);           // end PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        exc.emitShort(classCastHandler);        // handler PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        exc.emitShort(nullPointerClass);        // catch type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        exc.emitShort(invokeStartPC);           // start PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        exc.emitShort(invokeEndPC);             // end PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        exc.emitShort(invocationTargetHandler); // handler PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        exc.emitShort(throwableClass);          // catch type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        emitMethod(invokeIdx, cb.getMaxLocals(), cb, exc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                   new short[] { invocationTargetClass });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    private boolean usesPrimitiveTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // We need to emit boxing/unboxing constant pool information if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        // the method takes a primitive type for any of its parameters or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        // returns a primitive value (except void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (returnType.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (parameterTypes[i].isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    private int numNonPrimitiveParameterTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (!parameterTypes[i].isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                ++num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        return num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    private boolean isInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return declaringClass.isInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private String buildInternalSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        buf.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            buf.append(getClassName(parameterTypes[i], true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        buf.append(getClassName(returnType, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private static synchronized String generateName(boolean isConstructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                                    boolean forSerialization)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            if (forSerialization) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                int num = ++serializationConstructorSymnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                return "sun/reflect/GeneratedSerializationConstructorAccessor" + num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                int num = ++constructorSymnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                return "sun/reflect/GeneratedConstructorAccessor" + num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            int num = ++methodSymnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            return "sun/reflect/GeneratedMethodAccessor" + num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
}