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