src/java.base/share/classes/jdk/internal/reflect/MethodAccessorGenerator.java
author dholmes
Sat, 23 Jun 2018 01:32:41 -0400
changeset 50735 2f2af62dfac7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8010319: Implementation of JEP 181: Nest-Based Access Control Reviewed-by: alanb, psandoz, mchung, coleenp, acorn, mcimadamore, forax, jlahoda, sspitsyn, abuckley Contributed-by: alex.buckley@oracle.com, maurizio.mimadamore@oracle.com, mandy.chung@oracle.com, tobias.hartmann@oracle.com, david.holmes@oracle.com, vladimir.x.ivanov@oracle.com, karen.kinnear@oracle.com, vladimir.kozlov@oracle.com, john.r.rose@oracle.com, daniel.smith@oracle.com, serguei.spitsyn@oracle.com, kumardotsrinivasan@gmail.com, boris.ulasevich@bell-sw.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, 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
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
    26
package jdk.internal.reflect;
2
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
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 26456
diff changeset
    47
    private static volatile int methodSymnum;
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 26456
diff changeset
    48
    private static volatile int constructorSymnum;
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 26456
diff changeset
    49
    private static volatile int serializationConstructorSymnum;
2
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
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   151
        //     [UTF-8] "jdk/internal/reflect/{MethodAccessorImpl,ConstructorAccessorImpl,SerializationConstructorAccessorImpl}"
2
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
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   293
                    ("jdk/internal/reflect/SerializationConstructorAccessorImpl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } else {
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   295
                asm.emitConstantPoolUTF8("jdk/internal/reflect/ConstructorAccessorImpl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } else {
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   298
            asm.emitConstantPoolUTF8("jdk/internal/reflect/MethodAccessorImpl");
2
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>() {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37363
diff changeset
   395
                @SuppressWarnings("deprecation") // Class.newInstance
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   396
                public MagicAccessorImpl run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   398
                        return (MagicAccessorImpl)
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   399
                        ClassDefiner.defineClass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                (generatedName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                 bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                 bytes.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                 declaringClass.getClassLoader()).newInstance();
10355
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 10342
diff changeset
   405
                        } catch (InstantiationException | IllegalAccessException e) {
a976ff46116b 7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents: 10342
diff changeset
   406
                            throw new InternalError(e);
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /** This emits the code for either invoke() or newInstance() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private void emitInvoke() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // NOTE that this code will only handle 65535 parameters since we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // use the sipush instruction to get the array index on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // operand stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (parameterTypes.length > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw new InternalError("Can't handle more than 65535 parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // Generate code into fresh code buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        ClassFileAssembler cb = new ClassFileAssembler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // 1 incoming argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            cb.setMaxLocals(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            // 2 incoming arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            cb.setMaxLocals(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        short illegalArgStartPC = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // Instantiate target class before continuing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            // new <target class type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            cb.opc_new(targetClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // Get target object on operand stack if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            // We need to do an explicit null check here; we won't see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            // NullPointerExceptions from the invoke bytecode, since it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            // covered by an exception handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (!isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                // aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                // ifnonnull <checkcast label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                // new <NullPointerException>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                // invokespecial <NullPointerException ctor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                // athrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                // <checkcast label:>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                // aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                // checkcast <target class's type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                Label l = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                cb.opc_ifnonnull(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                cb.opc_new(nullPointerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                cb.opc_invokespecial(nullPointerCtorIdx, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                l.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                illegalArgStartPC = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                cb.opc_checkcast(targetClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // Have to check length of incoming array and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // IllegalArgumentException if not correct. A concession to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // JCK (isn't clearly specified in the spec): we allow null in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // case where the argument list is zero length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // if no-arg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        //   aload_2 | aload_1 (Method | Constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        //   ifnull <success label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // aload_2 | aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // arraylength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // sipush <num parameter types>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // if_icmpeq <success label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        // new <IllegalArgumentException>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        // dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        // invokespecial <IllegalArgumentException ctor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // athrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // <success label:>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        Label successLabel = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (parameterTypes.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            cb.opc_ifnull(successLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        cb.opc_arraylength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        cb.opc_sipush((short) parameterTypes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        cb.opc_if_icmpeq(successLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        cb.opc_new(illegalArgumentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        cb.opc_invokespecial(illegalArgumentCtorIdx, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        successLabel.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        // Iterate through incoming actual parameters, ensuring that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        // is compatible with the formal parameter type, and pushing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        // actual on the operand stack (unboxing and widening if necessary).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        short paramTypeCPIdx = nonPrimitiveParametersBaseIdx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        Label nextParamLabel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        byte count = 1; // both invokeinterface opcode's "count" as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // num args of other invoke bytecodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        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
   517
            Class<?> paramType = parameterTypes[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            count += (byte) typeSizeInStackSlots(paramType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (nextParamLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                nextParamLabel.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                nextParamLabel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            // aload_2 | aload_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // sipush <index>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            // aaload
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                cb.opc_aload_1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            cb.opc_sipush((short) i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            cb.opc_aaload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (isPrimitive(paramType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                // Unboxing code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                // Put parameter into temporary local variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                // astore_3 | astore_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    cb.opc_astore_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    cb.opc_astore_3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                // repeat for all possible widening conversions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                //   aload_3 | aload_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                //   instanceof <primitive boxing type>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                //   ifeq <next unboxing label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                //   aload_3 | aload_2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                //   checkcast <primitive boxing type> // Note: this is "redundant",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                //                                     // but necessary for the verifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                //   invokevirtual <unboxing method>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                //   <widening conversion bytecode, if necessary>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                //   goto <next parameter label>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                // <next unboxing label:> ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                // last unboxing label:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                //   new <IllegalArgumentException>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                //   dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                //   invokespecial <IllegalArgumentException ctor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                //   athrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                Label l = null; // unboxing label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                nextParamLabel = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                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
   564
                    Class<?> c = primitiveTypes[j];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    if (canWidenTo(c, paramType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                            l.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        // Emit checking and unboxing code for this type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                            cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                            cb.opc_aload_3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        cb.opc_instanceof(indexForPrimitiveType(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        l = new Label();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        cb.opc_ifeq(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            cb.opc_aload_2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            cb.opc_aload_3();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        cb.opc_checkcast(indexForPrimitiveType(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        cb.opc_invokevirtual(unboxingMethodForPrimitiveType(c),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                             0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                             typeSizeInStackSlots(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        emitWideningBytecodeForPrimitiveConversion(cb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                                                                   c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                                                   paramType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        cb.opc_goto(nextParamLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    throw new InternalError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        ("Must have found at least identity conversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // Fell through; given object is null or invalid. According to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // the spec, we can throw IllegalArgumentException for both of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                // these cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                l.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                cb.opc_new(illegalArgumentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                cb.opc_dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                cb.opc_invokespecial(illegalArgumentCtorIdx, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                // Emit appropriate checkcast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                cb.opc_checkcast(paramTypeCPIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                paramTypeCPIdx = add(paramTypeCPIdx, S2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                // Fall through to next argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // Bind last goto if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (nextParamLabel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            nextParamLabel.bind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        short invokeStartPC = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        // OK, ready to perform the invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            cb.opc_invokespecial(targetMethodRef, count, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                cb.opc_invokestatic(targetMethodRef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                    count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                    typeSizeInStackSlots(returnType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                if (isInterface()) {
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   632
                    cb.opc_invokeinterface(targetMethodRef,
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   633
                                           count,
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   634
                                           count,
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   635
                                           typeSizeInStackSlots(returnType));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    cb.opc_invokevirtual(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
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        short invokeEndPC = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (!isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            // Box return value if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (isPrimitive(returnType)) {
26456
24d86ff9740f 5043030: (reflect) unnecessary object creation in reflection
jfranck
parents: 25859
diff changeset
   649
                cb.opc_invokestatic(boxingMethodForPrimitiveType(returnType),
24d86ff9740f 5043030: (reflect) unnecessary object creation in reflection
jfranck
parents: 25859
diff changeset
   650
                                    typeSizeInStackSlots(returnType),
24d86ff9740f 5043030: (reflect) unnecessary object creation in reflection
jfranck
parents: 25859
diff changeset
   651
                                    0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            } else if (returnType == Void.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                cb.opc_aconst_null();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        cb.opc_areturn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        // We generate two exception handlers; one which is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        // for catching ClassCastException and NullPointerException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        // throwing IllegalArgumentException, and the other which catches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // all java/lang/Throwable objects thrown from the target method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // and wraps them in InvocationTargetExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        short classCastHandler = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        // ClassCast, etc. exception handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        cb.setStack(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        cb.opc_invokespecial(toStringIdx, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        cb.opc_new(illegalArgumentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        cb.opc_dup_x1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        cb.opc_swap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        cb.opc_invokespecial(illegalArgumentStringCtorIdx, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        cb.opc_athrow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        short invocationTargetHandler = cb.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        // InvocationTargetException exception handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        cb.setStack(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        cb.opc_new(invocationTargetClass);
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(invocationTargetCtorIdx, 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
        // Generate exception table. We cover the entire code sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // with an exception handler which catches ClassCastException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // converts it into an IllegalArgumentException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        ClassFileAssembler exc = new ClassFileAssembler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        exc.emitShort(illegalArgStartPC);       // start PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        exc.emitShort(invokeStartPC);           // end PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        exc.emitShort(classCastHandler);        // handler PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        exc.emitShort(classCastClass);          // catch type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        exc.emitShort(illegalArgStartPC);       // start PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        exc.emitShort(invokeStartPC);           // end PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        exc.emitShort(classCastHandler);        // handler PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        exc.emitShort(nullPointerClass);        // catch type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        exc.emitShort(invokeStartPC);           // start PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        exc.emitShort(invokeEndPC);             // end PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        exc.emitShort(invocationTargetHandler); // handler PC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        exc.emitShort(throwableClass);          // catch type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        emitMethod(invokeIdx, cb.getMaxLocals(), cb, exc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                   new short[] { invocationTargetClass });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    private boolean usesPrimitiveTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        // We need to emit boxing/unboxing constant pool information if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        // the method takes a primitive type for any of its parameters or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        // returns a primitive value (except void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (returnType.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        for (int i = 0; i < parameterTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (parameterTypes[i].isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    private int numNonPrimitiveParameterTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        int num = 0;
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
                ++num;
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 num;
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 boolean isInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return declaringClass.isInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    private String buildInternalSignature() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   740
        StringBuilder sb = new StringBuilder();
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   741
        sb.append("(");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        for (int i = 0; i < parameterTypes.length; i++) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   743
            sb.append(getClassName(parameterTypes[i], true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   745
        sb.append(")");
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   746
        sb.append(getClassName(returnType, true));
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23010
diff changeset
   747
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    private static synchronized String generateName(boolean isConstructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                                                    boolean forSerialization)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if (isConstructor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if (forSerialization) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                int num = ++serializationConstructorSymnum;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   756
                return "jdk/internal/reflect/GeneratedSerializationConstructorAccessor" + num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                int num = ++constructorSymnum;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   759
                return "jdk/internal/reflect/GeneratedConstructorAccessor" + num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int num = ++methodSymnum;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34774
diff changeset
   763
            return "jdk/internal/reflect/GeneratedMethodAccessor" + num;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
}