jdk/src/java.base/share/classes/java/lang/invoke/GenerateJLIClassesHelper.java
author redestad
Wed, 10 Aug 2016 22:49:05 +0200
changeset 40259 439673b634a9
child 40408 cf7e826d4d63
permissions -rw-r--r--
8163814: JDK build has been failing after 8163373 Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40259
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     1
/*
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     4
 *
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    10
 *
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    15
 * accompanied this code).
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    16
 *
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    20
 *
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    23
 * questions.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    24
 */
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    25
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    26
package java.lang.invoke;
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    27
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    28
import java.util.Map;
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    29
import jdk.internal.org.objectweb.asm.ClassWriter;
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    30
import jdk.internal.org.objectweb.asm.Opcodes;
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    31
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    32
/**
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    33
 * Helper class to assist the GenerateJLIClassesPlugin to get access to
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    34
 * generate classes ahead of time.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    35
 */
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    36
class GenerateJLIClassesHelper {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    37
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    38
    static byte[] generateDMHClassBytes(String className,
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    39
            MethodType[] methodTypes, int[] types) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    40
        LambdaForm[] forms = new LambdaForm[methodTypes.length];
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    41
        for (int i = 0; i < forms.length; i++) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    42
            forms[i] = DirectMethodHandle.makePreparedLambdaForm(methodTypes[i],
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    43
                                                                 types[i]);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    44
            methodTypes[i] = forms[i].methodType();
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    45
        }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    46
        return generateCodeBytesForLFs(className, forms, methodTypes);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    47
    }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    48
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    49
    /*
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    50
     * Generate customized code for a set of LambdaForms of specified types into
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    51
     * a class with a specified name.
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    52
     */
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    53
    private static byte[] generateCodeBytesForLFs(String className,
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    54
            LambdaForm[] forms, MethodType[] types) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    55
        assert(forms.length == types.length);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    56
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    57
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    58
        cw.visit(Opcodes.V1_8, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    59
                className, null, InvokerBytecodeGenerator.INVOKER_SUPER_NAME, null);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    60
        cw.visitSource(className.substring(className.lastIndexOf('/') + 1), null);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    61
        for (int i = 0; i < forms.length; i++) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    62
            InvokerBytecodeGenerator g
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    63
                    = new InvokerBytecodeGenerator(className, forms[i], types[i]);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    64
            g.setClassWriter(cw);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    65
            g.addMethod();
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    66
        }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    67
        return cw.toByteArray();
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    68
    }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    69
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    70
    static Map.Entry<String, byte[]> generateConcreteBMHClassBytes(
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    71
            final String types) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    72
        for (char c : types.toCharArray()) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    73
            if ("LIJFD".indexOf(c) < 0) {
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    74
                throw new IllegalArgumentException("All characters must "
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    75
                        + "correspond to a basic field type: LIJFD");
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    76
            }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    77
        }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    78
        String shortTypes = LambdaForm.shortenSignature(types);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    79
        final String className =
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    80
                BoundMethodHandle.Factory.speciesInternalClassName(shortTypes);
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    81
        return Map.entry(className,
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    82
                         BoundMethodHandle.Factory.generateConcreteBMHClassBytes(
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    83
                                 shortTypes, types, className));
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    84
    }
439673b634a9 8163814: JDK build has been failing after 8163373
redestad
parents:
diff changeset
    85
}