Java Code Examples for jdk.internal.org.objectweb.asm.commons.InstructionAdapter#visitCode()

The following examples show how to use jdk.internal.org.objectweb.asm.commons.InstructionAdapter#visitCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaAdapterBytecodeGenerator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void generateOverridingConstructorWithObjectParam(final InstructionAdapter mv, final Constructor<?> ctor, final String ctorDescriptor) {
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argTypes.length; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, ctorDescriptor, false);
    mv.visitVarInsn(ALOAD, offset);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ACONST_NULL);
    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", GET_HANDLE_OBJECT_DESCRIPTOR, false);
    endInitMethod(mv);
}
 
Example 2
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void generateDelegatingConstructor(final Constructor<?> ctor) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] argTypes = originalCtorType.getArgumentTypes();

    // All constructors must be public, even if in the superclass they were protected.
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), argTypes), null, null));

    mv.visitCode();
    // Invoke super constructor with the same arguments.
    mv.visitVarInsn(ALOAD, 0);
    int offset = 1; // First arg is at position 1, after this.
    for (Type argType: argTypes) {
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor());

    endInitMethod(mv);
}
 
Example 3
Source File: JavaAdapterBytecodeGenerator.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private void generateDelegatingConstructor(final Constructor<?> ctor) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] argTypes = originalCtorType.getArgumentTypes();

    // All constructors must be public, even if in the superclass they were protected.
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC |
            (ctor.isVarArgs() ? ACC_VARARGS : 0), INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), argTypes), null, null));

    mv.visitCode();
    // Invoke super constructor with the same arguments.
    mv.visitVarInsn(ALOAD, 0);
    int offset = 1; // First arg is at position 1, after this.
    for (final Type argType: argTypes) {
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor(), false);

    endInitMethod(mv);
}
 
Example 4
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void generateDelegatingConstructor(final Constructor<?> ctor) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] argTypes = originalCtorType.getArgumentTypes();

    // All constructors must be public, even if in the superclass they were protected.
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC |
            (ctor.isVarArgs() ? ACC_VARARGS : 0), INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), argTypes), null, null));

    mv.visitCode();
    // Invoke super constructor with the same arguments.
    mv.visitVarInsn(ALOAD, 0);
    int offset = 1; // First arg is at position 1, after this.
    for (final Type argType: argTypes) {
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor(), false);

    endInitMethod(mv);
}
 
Example 5
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void generateOverridingConstructorWithObjectParam(final InstructionAdapter mv, final Constructor<?> ctor, final String ctorDescriptor) {
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argTypes.length; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, ctorDescriptor, false);
    mv.visitVarInsn(ALOAD, offset);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ACONST_NULL);
    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", GET_HANDLE_OBJECT_DESCRIPTOR, false);
    endInitMethod(mv);
}
 
Example 6
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void generateSuperMethod(final MethodInfo mi) {
    final Method method = mi.method;

    final String methodDesc = mi.type.toMethodDescriptorString();
    final String name = mi.getName();

    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(getAccessModifiers(method),
            SUPER_PREFIX + name, methodDesc, null, getExceptionNames(method.getExceptionTypes())));
    mv.visitCode();

    emitSuperCall(mv, method.getDeclaringClass(), name, methodDesc);

    endMethod(mv);
}
 
Example 7
Source File: JavaAdapterBytecodeGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void generateSuperMethod(final MethodInfo mi) {
    final Method method = mi.method;

    final String methodDesc = mi.type.toMethodDescriptorString();
    final String name = mi.getName();

    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(getAccessModifiers(method),
            SUPER_PREFIX + name, methodDesc, null, getExceptionNames(method.getExceptionTypes())));
    mv.visitCode();

    emitSuperCall(mv, method.getDeclaringClass(), name, methodDesc);

    endMethod(mv);
}
 
Example 8
Source File: JavaAdapterBytecodeGenerator.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates a constructor for the adapter class. This constructor will take the same arguments as the supertype
 * constructor passed as the argument here, and delegate to it. However, it will take an additional argument of
 * either ScriptObject or ScriptFunction type (based on the value of the "fromFunction" parameter), and initialize
 * all the method handle fields of the adapter instance with functions from the script object (or the script
 * function itself, if that's what's passed). There is one method handle field in the adapter class for every method
 * that can be implemented or overridden; the name of every field is same as the name of the method, with a number
 * suffix that makes it unique in case of overloaded methods. The generated constructor will invoke
 * {@link #getHandle(ScriptFunction, MethodType, boolean)} or {@link #getHandle(Object, String, MethodType,
 * boolean)} to obtain the method handles; these methods make sure to add the necessary conversions and arity
 * adjustments so that the resulting method handles can be invoked from generated methods using {@code invokeExact}.
 * The constructor that takes a script function will only initialize the methods with the same name as the single
 * abstract method. The constructor will also store the Nashorn global that was current at the constructor
 * invocation time in a field named "global". The generated constructor will be public, regardless of whether the
 * supertype constructor was public or protected. The generated constructor will not be variable arity, even if the
 * supertype constructor was.
 * @param ctor the supertype constructor that is serving as the base for the generated constructor.
 * @param fromFunction true if we're generating a constructor that initializes SAM types from a single
 * ScriptFunction passed to it, false if we're generating a constructor that initializes an arbitrary type from a
 * ScriptObject passed to it.
 */
private void generateOverridingConstructor(final Constructor<?> ctor, final boolean fromFunction) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] originalArgTypes = originalCtorType.getArgumentTypes();
    final int argLen = originalArgTypes.length;
    final Type[] newArgTypes = new Type[argLen + 1];

    // Insert ScriptFunction|Object as the last argument to the constructor
    final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : OBJECT_TYPE;
    newArgTypes[argLen] = extraArgumentType;
    System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen);

    // All constructors must be public, even if in the superclass they were protected.
    // Existing super constructor <init>(this, args...) triggers generating <init>(this, scriptObj, args...).
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));

    mv.visitCode();
    // First, invoke super constructor with original arguments. If the form of the constructor we're generating is
    // <init>(this, args..., scriptFn), then we're invoking super.<init>(this, args...).
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argLen; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor());

    // Get a descriptor to the appropriate "JavaAdapterFactory.getHandle" method.
    final String getHandleDescriptor = fromFunction ? GET_HANDLE_FUNCTION_DESCRIPTOR : GET_HANDLE_OBJECT_DESCRIPTOR;

    // Assign MethodHandle fields through invoking getHandle()
    for (final MethodInfo mi : methodInfos) {
        mv.visitVarInsn(ALOAD, 0);
        if (fromFunction && !mi.getName().equals(samName)) {
            // Constructors initializing from a ScriptFunction only initialize methods with the SAM name.
            // NOTE: if there's a concrete overloaded method sharing the SAM name, it'll be overriden too. This
            // is a deliberate design choice. All other method handles are initialized to null.
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, offset);
            if(!fromFunction) {
                mv.aconst(mi.getName());
            }
            mv.aconst(Type.getMethodType(mi.type.toMethodDescriptorString()));
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", getHandleDescriptor);
        }
        mv.putfield(generatedClassName, mi.methodHandleInstanceFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    // Assign "this.global = Context.getGlobal()"
    mv.visitVarInsn(ALOAD, 0);
    invokeGetGlobalWithNullCheck(mv);
    mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);

    endInitMethod(mv);
}
 
Example 9
Source File: JavaAdapterServices.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 10
Source File: JavaAdapterServices.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 11
Source File: JavaAdapterBytecodeGenerator.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates a constructor for the instance adapter class. This constructor will take the same arguments as the supertype
 * constructor passed as the argument here, and delegate to it. However, it will take an additional argument of
 * either ScriptObject or ScriptFunction type (based on the value of the "fromFunction" parameter), and initialize
 * all the method handle fields of the adapter instance with functions from the script object (or the script
 * function itself, if that's what's passed). There is one method handle field in the adapter class for every method
 * that can be implemented or overridden; the name of every field is same as the name of the method, with a number
 * suffix that makes it unique in case of overloaded methods. The generated constructor will invoke
 * {@link #getHandle(ScriptFunction, MethodType, boolean)} or {@link #getHandle(Object, String, MethodType,
 * boolean)} to obtain the method handles; these methods make sure to add the necessary conversions and arity
 * adjustments so that the resulting method handles can be invoked from generated methods using {@code invokeExact}.
 * The constructor that takes a script function will only initialize the methods with the same name as the single
 * abstract method. The constructor will also store the Nashorn global that was current at the constructor
 * invocation time in a field named "global". The generated constructor will be public, regardless of whether the
 * supertype constructor was public or protected. The generated constructor will not be variable arity, even if the
 * supertype constructor was.
 * @param ctor the supertype constructor that is serving as the base for the generated constructor.
 * @param fromFunction true if we're generating a constructor that initializes SAM types from a single
 * ScriptFunction passed to it, false if we're generating a constructor that initializes an arbitrary type from a
 * ScriptObject passed to it.
 */
private void generateOverridingConstructor(final Constructor<?> ctor, final boolean fromFunction) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] originalArgTypes = originalCtorType.getArgumentTypes();
    final int argLen = originalArgTypes.length;
    final Type[] newArgTypes = new Type[argLen + 1];

    // Insert ScriptFunction|ScriptObject as the last argument to the constructor
    final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : SCRIPT_OBJECT_TYPE;
    newArgTypes[argLen] = extraArgumentType;
    System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen);

    // All constructors must be public, even if in the superclass they were protected.
    // Existing super constructor <init>(this, args...) triggers generating <init>(this, args..., scriptObj).
    // Any variable arity constructors become fixed-arity with explicit array arguments.
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));

    mv.visitCode();
    // First, invoke super constructor with original arguments. If the form of the constructor we're generating is
    // <init>(this, args..., scriptFn), then we're invoking super.<init>(this, args...).
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argLen; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor(), false);

    // Get a descriptor to the appropriate "JavaAdapterFactory.getHandle" method.
    final String getHandleDescriptor = fromFunction ? GET_HANDLE_FUNCTION_DESCRIPTOR : GET_HANDLE_OBJECT_DESCRIPTOR;

    // Assign MethodHandle fields through invoking getHandle()
    for (final MethodInfo mi : methodInfos) {
        mv.visitVarInsn(ALOAD, 0);
        if (fromFunction && !mi.getName().equals(samName)) {
            // Constructors initializing from a ScriptFunction only initialize methods with the SAM name.
            // NOTE: if there's a concrete overloaded method sharing the SAM name, it'll be overridden too. This
            // is a deliberate design choice. All other method handles are initialized to null.
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, offset);
            if(!fromFunction) {
                mv.aconst(mi.getName());
            }
            loadMethodTypeAndGetHandle(mv, mi, getHandleDescriptor);
        }
        mv.putfield(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    // Assign "this.global = Context.getGlobal()"
    mv.visitVarInsn(ALOAD, 0);
    invokeGetGlobalWithNullCheck(mv);
    mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);

    // Initialize converters
    generateConverterInit(mv, fromFunction);
    endInitMethod(mv);

    if (! fromFunction) {
        newArgTypes[argLen] = OBJECT_TYPE;
        final InstructionAdapter mv2 = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
                Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));
        generateOverridingConstructorWithObjectParam(mv2, ctor, originalCtorType.getDescriptor());
    }
}
 
Example 12
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates a constructor for the instance adapter class. This constructor will take the same arguments as the supertype
 * constructor passed as the argument here, and delegate to it. However, it will take an additional argument of
 * either ScriptObject or ScriptFunction type (based on the value of the "fromFunction" parameter), and initialize
 * all the method handle fields of the adapter instance with functions from the script object (or the script
 * function itself, if that's what's passed). There is one method handle field in the adapter class for every method
 * that can be implemented or overridden; the name of every field is same as the name of the method, with a number
 * suffix that makes it unique in case of overloaded methods. The generated constructor will invoke
 * {@link #getHandle(ScriptFunction, MethodType, boolean)} or {@link #getHandle(Object, String, MethodType,
 * boolean)} to obtain the method handles; these methods make sure to add the necessary conversions and arity
 * adjustments so that the resulting method handles can be invoked from generated methods using {@code invokeExact}.
 * The constructor that takes a script function will only initialize the methods with the same name as the single
 * abstract method. The constructor will also store the Nashorn global that was current at the constructor
 * invocation time in a field named "global". The generated constructor will be public, regardless of whether the
 * supertype constructor was public or protected. The generated constructor will not be variable arity, even if the
 * supertype constructor was.
 * @param ctor the supertype constructor that is serving as the base for the generated constructor.
 * @param fromFunction true if we're generating a constructor that initializes SAM types from a single
 * ScriptFunction passed to it, false if we're generating a constructor that initializes an arbitrary type from a
 * ScriptObject passed to it.
 */
private void generateOverridingConstructor(final Constructor<?> ctor, final boolean fromFunction) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] originalArgTypes = originalCtorType.getArgumentTypes();
    final int argLen = originalArgTypes.length;
    final Type[] newArgTypes = new Type[argLen + 1];

    // Insert ScriptFunction|ScriptObject as the last argument to the constructor
    final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : SCRIPT_OBJECT_TYPE;
    newArgTypes[argLen] = extraArgumentType;
    System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen);

    // All constructors must be public, even if in the superclass they were protected.
    // Existing super constructor <init>(this, args...) triggers generating <init>(this, args..., scriptObj).
    // Any variable arity constructors become fixed-arity with explicit array arguments.
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));

    mv.visitCode();
    // First, invoke super constructor with original arguments. If the form of the constructor we're generating is
    // <init>(this, args..., scriptFn), then we're invoking super.<init>(this, args...).
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argLen; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor(), false);

    // Get a descriptor to the appropriate "JavaAdapterFactory.getHandle" method.
    final String getHandleDescriptor = fromFunction ? GET_HANDLE_FUNCTION_DESCRIPTOR : GET_HANDLE_OBJECT_DESCRIPTOR;

    // Assign MethodHandle fields through invoking getHandle()
    for (final MethodInfo mi : methodInfos) {
        mv.visitVarInsn(ALOAD, 0);
        if (fromFunction && !mi.getName().equals(samName)) {
            // Constructors initializing from a ScriptFunction only initialize methods with the SAM name.
            // NOTE: if there's a concrete overloaded method sharing the SAM name, it'll be overridden too. This
            // is a deliberate design choice. All other method handles are initialized to null.
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, offset);
            if(!fromFunction) {
                mv.aconst(mi.getName());
            }
            loadMethodTypeAndGetHandle(mv, mi, getHandleDescriptor);
        }
        mv.putfield(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    // Assign "this.global = Context.getGlobal()"
    mv.visitVarInsn(ALOAD, 0);
    invokeGetGlobalWithNullCheck(mv);
    mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);

    // Initialize converters
    generateConverterInit(mv, fromFunction);
    endInitMethod(mv);

    if (! fromFunction) {
        newArgTypes[argLen] = OBJECT_TYPE;
        final InstructionAdapter mv2 = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
                Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));
        generateOverridingConstructorWithObjectParam(mv2, ctor, originalCtorType.getDescriptor());
    }
}
 
Example 13
Source File: JavaAdapterServices.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 14
Source File: JavaAdapterServices.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 15
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates a constructor for the instance adapter class. This constructor will take the same arguments as the supertype
 * constructor passed as the argument here, and delegate to it. However, it will take an additional argument of
 * either ScriptObject or ScriptFunction type (based on the value of the "fromFunction" parameter), and initialize
 * all the method handle fields of the adapter instance with functions from the script object (or the script
 * function itself, if that's what's passed). There is one method handle field in the adapter class for every method
 * that can be implemented or overridden; the name of every field is same as the name of the method, with a number
 * suffix that makes it unique in case of overloaded methods. The generated constructor will invoke
 * {@link #getHandle(ScriptFunction, MethodType, boolean)} or {@link #getHandle(Object, String, MethodType,
 * boolean)} to obtain the method handles; these methods make sure to add the necessary conversions and arity
 * adjustments so that the resulting method handles can be invoked from generated methods using {@code invokeExact}.
 * The constructor that takes a script function will only initialize the methods with the same name as the single
 * abstract method. The constructor will also store the Nashorn global that was current at the constructor
 * invocation time in a field named "global". The generated constructor will be public, regardless of whether the
 * supertype constructor was public or protected. The generated constructor will not be variable arity, even if the
 * supertype constructor was.
 * @param ctor the supertype constructor that is serving as the base for the generated constructor.
 * @param fromFunction true if we're generating a constructor that initializes SAM types from a single
 * ScriptFunction passed to it, false if we're generating a constructor that initializes an arbitrary type from a
 * ScriptObject passed to it.
 */
private void generateOverridingConstructor(final Constructor<?> ctor, final boolean fromFunction) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] originalArgTypes = originalCtorType.getArgumentTypes();
    final int argLen = originalArgTypes.length;
    final Type[] newArgTypes = new Type[argLen + 1];

    // Insert ScriptFunction|Object as the last argument to the constructor
    final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : OBJECT_TYPE;
    newArgTypes[argLen] = extraArgumentType;
    System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen);

    // All constructors must be public, even if in the superclass they were protected.
    // Existing super constructor <init>(this, args...) triggers generating <init>(this, scriptObj, args...).
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));

    mv.visitCode();
    // First, invoke super constructor with original arguments. If the form of the constructor we're generating is
    // <init>(this, args..., scriptFn), then we're invoking super.<init>(this, args...).
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argLen; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor());

    // Get a descriptor to the appropriate "JavaAdapterFactory.getHandle" method.
    final String getHandleDescriptor = fromFunction ? GET_HANDLE_FUNCTION_DESCRIPTOR : GET_HANDLE_OBJECT_DESCRIPTOR;

    // Assign MethodHandle fields through invoking getHandle()
    for (final MethodInfo mi : methodInfos) {
        mv.visitVarInsn(ALOAD, 0);
        if (fromFunction && !mi.getName().equals(samName)) {
            // Constructors initializing from a ScriptFunction only initialize methods with the SAM name.
            // NOTE: if there's a concrete overloaded method sharing the SAM name, it'll be overriden too. This
            // is a deliberate design choice. All other method handles are initialized to null.
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, offset);
            if(!fromFunction) {
                mv.aconst(mi.getName());
            }
            mv.aconst(Type.getMethodType(mi.type.toMethodDescriptorString()));
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", getHandleDescriptor);
        }
        mv.putfield(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    // Assign "this.global = Context.getGlobal()"
    mv.visitVarInsn(ALOAD, 0);
    invokeGetGlobalWithNullCheck(mv);
    mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);

    endInitMethod(mv);
}
 
Example 16
Source File: JavaAdapterServices.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 17
Source File: JavaAdapterBytecodeGenerator.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates a constructor for the instance adapter class. This constructor will take the same arguments as the supertype
 * constructor passed as the argument here, and delegate to it. However, it will take an additional argument of
 * either ScriptObject or ScriptFunction type (based on the value of the "fromFunction" parameter), and initialize
 * all the method handle fields of the adapter instance with functions from the script object (or the script
 * function itself, if that's what's passed). There is one method handle field in the adapter class for every method
 * that can be implemented or overridden; the name of every field is same as the name of the method, with a number
 * suffix that makes it unique in case of overloaded methods. The generated constructor will invoke
 * {@link #getHandle(ScriptFunction, MethodType, boolean)} or {@link #getHandle(Object, String, MethodType,
 * boolean)} to obtain the method handles; these methods make sure to add the necessary conversions and arity
 * adjustments so that the resulting method handles can be invoked from generated methods using {@code invokeExact}.
 * The constructor that takes a script function will only initialize the methods with the same name as the single
 * abstract method. The constructor will also store the Nashorn global that was current at the constructor
 * invocation time in a field named "global". The generated constructor will be public, regardless of whether the
 * supertype constructor was public or protected. The generated constructor will not be variable arity, even if the
 * supertype constructor was.
 * @param ctor the supertype constructor that is serving as the base for the generated constructor.
 * @param fromFunction true if we're generating a constructor that initializes SAM types from a single
 * ScriptFunction passed to it, false if we're generating a constructor that initializes an arbitrary type from a
 * ScriptObject passed to it.
 */
private void generateOverridingConstructor(final Constructor<?> ctor, final boolean fromFunction) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] originalArgTypes = originalCtorType.getArgumentTypes();
    final int argLen = originalArgTypes.length;
    final Type[] newArgTypes = new Type[argLen + 1];

    // Insert ScriptFunction|ScriptObject as the last argument to the constructor
    final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : SCRIPT_OBJECT_TYPE;
    newArgTypes[argLen] = extraArgumentType;
    System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen);

    // All constructors must be public, even if in the superclass they were protected.
    // Existing super constructor <init>(this, args...) triggers generating <init>(this, args..., scriptObj).
    // Any variable arity constructors become fixed-arity with explicit array arguments.
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));

    mv.visitCode();
    // First, invoke super constructor with original arguments. If the form of the constructor we're generating is
    // <init>(this, args..., scriptFn), then we're invoking super.<init>(this, args...).
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argLen; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor(), false);

    // Get a descriptor to the appropriate "JavaAdapterFactory.getHandle" method.
    final String getHandleDescriptor = fromFunction ? GET_HANDLE_FUNCTION_DESCRIPTOR : GET_HANDLE_OBJECT_DESCRIPTOR;

    // Assign MethodHandle fields through invoking getHandle()
    for (final MethodInfo mi : methodInfos) {
        mv.visitVarInsn(ALOAD, 0);
        if (fromFunction && !mi.getName().equals(samName)) {
            // Constructors initializing from a ScriptFunction only initialize methods with the SAM name.
            // NOTE: if there's a concrete overloaded method sharing the SAM name, it'll be overriden too. This
            // is a deliberate design choice. All other method handles are initialized to null.
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, offset);
            if(!fromFunction) {
                mv.aconst(mi.getName());
            }
            loadMethodTypeAndGetHandle(mv, mi, getHandleDescriptor);
        }
        mv.putfield(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    // Assign "this.global = Context.getGlobal()"
    mv.visitVarInsn(ALOAD, 0);
    invokeGetGlobalWithNullCheck(mv);
    mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);

    // Initialize converters
    generateConverterInit(mv, fromFunction);
    endInitMethod(mv);

    if (! fromFunction) {
        newArgTypes[argLen] = OBJECT_TYPE;
        final InstructionAdapter mv2 = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
                Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));
        generateOverridingConstructorWithObjectParam(mv2, ctor, originalCtorType.getDescriptor());
    }
}
 
Example 18
Source File: JavaAdapterServices.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.lookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 19
Source File: JavaAdapterServices.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static MethodHandle createNoPermissionsInvoker() {
    final String className = "NoPermissionsInvoker";

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_7, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, className, null, "java/lang/Object", null);
    final Type objectType = Type.getType(Object.class);
    final Type methodHandleType = Type.getType(MethodHandle.class);
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke",
            Type.getMethodDescriptor(Type.VOID_TYPE, methodHandleType, objectType), null, null));
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.invokevirtual(methodHandleType.getInternalName(), "invokeExact", Type.getMethodDescriptor(
            Type.VOID_TYPE, objectType), false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] bytes = cw.toByteArray();

    final ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
        @Override
        public ClassLoader run() {
            return new SecureClassLoader(null) {
                @Override
                protected Class<?> findClass(final String name) throws ClassNotFoundException {
                    if(name.equals(className)) {
                        return defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(
                                new CodeSource(null, (CodeSigner[])null), new Permissions()));
                    }
                    throw new ClassNotFoundException(name);
                }
            };
        }
    });

    try {
        return MethodHandles.publicLookup().findStatic(Class.forName(className, true, loader), "invoke",
                MethodType.methodType(void.class, MethodHandle.class, Object.class));
    } catch(final ReflectiveOperationException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
 
Example 20
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates a constructor for the instance adapter class. This constructor will take the same arguments as the supertype
 * constructor passed as the argument here, and delegate to it. However, it will take an additional argument of
 * either ScriptObject or ScriptFunction type (based on the value of the "fromFunction" parameter), and initialize
 * all the method handle fields of the adapter instance with functions from the script object (or the script
 * function itself, if that's what's passed). There is one method handle field in the adapter class for every method
 * that can be implemented or overridden; the name of every field is same as the name of the method, with a number
 * suffix that makes it unique in case of overloaded methods. The generated constructor will invoke
 * {@link #getHandle(ScriptFunction, MethodType, boolean)} or {@link #getHandle(Object, String, MethodType,
 * boolean)} to obtain the method handles; these methods make sure to add the necessary conversions and arity
 * adjustments so that the resulting method handles can be invoked from generated methods using {@code invokeExact}.
 * The constructor that takes a script function will only initialize the methods with the same name as the single
 * abstract method. The constructor will also store the Nashorn global that was current at the constructor
 * invocation time in a field named "global". The generated constructor will be public, regardless of whether the
 * supertype constructor was public or protected. The generated constructor will not be variable arity, even if the
 * supertype constructor was.
 * @param ctor the supertype constructor that is serving as the base for the generated constructor.
 * @param fromFunction true if we're generating a constructor that initializes SAM types from a single
 * ScriptFunction passed to it, false if we're generating a constructor that initializes an arbitrary type from a
 * ScriptObject passed to it.
 */
private void generateOverridingConstructor(final Constructor<?> ctor, final boolean fromFunction) {
    final Type originalCtorType = Type.getType(ctor);
    final Type[] originalArgTypes = originalCtorType.getArgumentTypes();
    final int argLen = originalArgTypes.length;
    final Type[] newArgTypes = new Type[argLen + 1];

    // Insert ScriptFunction|Object as the last argument to the constructor
    final Type extraArgumentType = fromFunction ? SCRIPT_FUNCTION_TYPE : OBJECT_TYPE;
    newArgTypes[argLen] = extraArgumentType;
    System.arraycopy(originalArgTypes, 0, newArgTypes, 0, argLen);

    // All constructors must be public, even if in the superclass they were protected.
    // Existing super constructor <init>(this, args...) triggers generating <init>(this, scriptObj, args...).
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, INIT,
            Type.getMethodDescriptor(originalCtorType.getReturnType(), newArgTypes), null, null));

    mv.visitCode();
    // First, invoke super constructor with original arguments. If the form of the constructor we're generating is
    // <init>(this, args..., scriptFn), then we're invoking super.<init>(this, args...).
    mv.visitVarInsn(ALOAD, 0);
    final Class<?>[] argTypes = ctor.getParameterTypes();
    int offset = 1; // First arg is at position 1, after this.
    for (int i = 0; i < argLen; ++i) {
        final Type argType = Type.getType(argTypes[i]);
        mv.load(offset, argType);
        offset += argType.getSize();
    }
    mv.invokespecial(superClassName, INIT, originalCtorType.getDescriptor());

    // Get a descriptor to the appropriate "JavaAdapterFactory.getHandle" method.
    final String getHandleDescriptor = fromFunction ? GET_HANDLE_FUNCTION_DESCRIPTOR : GET_HANDLE_OBJECT_DESCRIPTOR;

    // Assign MethodHandle fields through invoking getHandle()
    for (final MethodInfo mi : methodInfos) {
        mv.visitVarInsn(ALOAD, 0);
        if (fromFunction && !mi.getName().equals(samName)) {
            // Constructors initializing from a ScriptFunction only initialize methods with the SAM name.
            // NOTE: if there's a concrete overloaded method sharing the SAM name, it'll be overriden too. This
            // is a deliberate design choice. All other method handles are initialized to null.
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.visitVarInsn(ALOAD, offset);
            if(!fromFunction) {
                mv.aconst(mi.getName());
            }
            mv.aconst(Type.getMethodType(mi.type.toMethodDescriptorString()));
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", getHandleDescriptor);
        }
        mv.putfield(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    // Assign "this.global = Context.getGlobal()"
    mv.visitVarInsn(ALOAD, 0);
    invokeGetGlobalWithNullCheck(mv);
    mv.putfield(generatedClassName, GLOBAL_FIELD_NAME, SCRIPT_OBJECT_TYPE_DESCRIPTOR);

    endInitMethod(mv);
}