当前位置: 首页>>代码示例>>Java>>正文


Java InstructionAdapter.visitInsn方法代码示例

本文整理汇总了Java中jdk.internal.org.objectweb.asm.commons.InstructionAdapter.visitInsn方法的典型用法代码示例。如果您正苦于以下问题:Java InstructionAdapter.visitInsn方法的具体用法?Java InstructionAdapter.visitInsn怎么用?Java InstructionAdapter.visitInsn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jdk.internal.org.objectweb.asm.commons.InstructionAdapter的用法示例。


在下文中一共展示了InstructionAdapter.visitInsn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateOverridingConstructorWithObjectParam

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
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);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:18,代码来源:JavaAdapterBytecodeGenerator.java

示例2: convertReturnValue

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private static void convertReturnValue(final InstructionAdapter mv, final Class<?> origReturnType) {
    if (origReturnType == void.class) {
        mv.pop();
    } else if (origReturnType == Object.class) {
        // Must hide ConsString (and potentially other internal Nashorn types) from callers
        EXPORT_RETURN_VALUE.invoke(mv);
    } else if (origReturnType == byte.class) {
        mv.visitInsn(I2B);
    } else if (origReturnType == short.class) {
        mv.visitInsn(I2S);
    } else if (origReturnType == float.class) {
        mv.visitInsn(D2F);
    } else if (origReturnType == char.class) {
        TO_CHAR_PRIMITIVE.invoke(mv);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:JavaAdapterBytecodeGenerator.java

示例3: generateFinalizerDelegate

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private void generateFinalizerDelegate(final String finalizerDelegateName) {
    // Generate a delegate that will be invoked from the no-permission trampoline. Note it can be private, as we'll
    // refer to it with a MethodHandle constant pool entry in the overridden finalize() method (see
    // generateFinalizerOverride()).
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PRIVATE | ACC_STATIC,
            finalizerDelegateName, Type.getMethodDescriptor(Type.VOID_TYPE, OBJECT_TYPE), null, null));

    // Simply invoke super.finalize()
    mv.visitVarInsn(ALOAD, 0);
    mv.checkcast(Type.getType(generatedClassName));
    mv.invokespecial(superClassName, "finalize", Type.getMethodDescriptor(Type.VOID_TYPE), false);

    mv.visitInsn(RETURN);
    endMethod(mv);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:16,代码来源:JavaAdapterBytecodeGenerator.java

示例4: generateFinalizerOverride

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private void generateFinalizerOverride(final String finalizerDelegateName) {
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, "finalize",
            VOID_NOARG_METHOD_DESCRIPTOR, null, null));
    // Overridden finalizer will take a MethodHandle to the finalizer delegating method, ...
    mv.aconst(new Handle(Opcodes.H_INVOKESTATIC, generatedClassName, finalizerDelegateName,
            Type.getMethodDescriptor(Type.VOID_TYPE, OBJECT_TYPE)));
    mv.visitVarInsn(ALOAD, 0);
    // ...and invoke it through JavaAdapterServices.invokeNoPermissions
    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "invokeNoPermissions",
            Type.getMethodDescriptor(METHOD_HANDLE_TYPE, OBJECT_TYPE), false);
    mv.visitInsn(RETURN);
    endMethod(mv);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:14,代码来源:JavaAdapterBytecodeGenerator.java

示例5: convertParam

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
/**
 * Emits instruction for converting a parameter on the top of the stack to
 * a type that is understood by Nashorn.
 * @param mv the current method visitor
 * @param t the type on the top of the stack
 * @param varArg if the invocation will be variable arity
 */
private static void convertParam(final InstructionAdapter mv, final Type t, final boolean varArg) {
    // We perform conversions of some primitives to accommodate types that
    // Nashorn can handle.
    switch(t.getSort()) {
    case Type.CHAR:
        // Chars are boxed, as we don't know if the JS code wants to treat
        // them as an effective "unsigned short" or as a single-char string.
        CHAR_VALUE_OF.invoke(mv);
        break;
    case Type.FLOAT:
        // Floats are widened to double.
        mv.visitInsn(Opcodes.F2D);
        if (varArg) {
            // We'll be boxing everything anyway for the vararg invocation,
            // so we might as well do it proactively here and thus not cause
            // a widening in the number of slots, as that could even make
            // the array creation invocation go over 255 param slots.
            DOUBLE_VALUE_OF.invoke(mv);
        }
        break;
    case Type.LONG:
        // Longs are boxed as Nashorn can't represent them precisely as a
        // primitive number.
        LONG_VALUE_OF.invoke(mv);
        break;
    case Type.OBJECT:
        if(t.equals(OBJECT_TYPE)) {
            // Object can carry a ScriptObjectMirror and needs to be unwrapped
            // before passing into a Nashorn function.
            UNWRAP.invoke(mv);
        }
        break;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:42,代码来源:JavaAdapterBytecodeGenerator.java

示例6: generateFinalizerDelegate

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private void generateFinalizerDelegate() {
    // Generate a delegate that will be invoked from the no-permission trampoline. Note it can be private, as we'll
    // refer to it with a MethodHandle constant pool entry in the overridden finalize() method (see
    // generateFinalizerOverride()).
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PRIVATE | ACC_STATIC,
            FINALIZER_DELEGATE_NAME, FINALIZER_DELEGATE_METHOD_DESCRIPTOR, null, null));

    // Simply invoke super.finalize()
    mv.visitVarInsn(ALOAD, 0);
    mv.checkcast(Type.getType(generatedClassName));
    mv.invokespecial(superClassName, "finalize", VOID_METHOD_DESCRIPTOR, false);

    mv.visitInsn(RETURN);
    endMethod(mv);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:JavaAdapterBytecodeGenerator.java

示例7: generateFinalizerOverride

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private void generateFinalizerOverride() {
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, "finalize",
            VOID_METHOD_DESCRIPTOR, null, null));
    // Overridden finalizer will take a MethodHandle to the finalizer delegating method, ...
    mv.aconst(new Handle(Opcodes.H_INVOKESTATIC, generatedClassName, FINALIZER_DELEGATE_NAME,
            FINALIZER_DELEGATE_METHOD_DESCRIPTOR, false));
    mv.visitVarInsn(ALOAD, 0);
    // ...and invoke it through JavaAdapterServices.invokeNoPermissions
    INVOKE_NO_PERMISSIONS.invoke(mv);
    mv.visitInsn(RETURN);
    endMethod(mv);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:JavaAdapterBytecodeGenerator.java

示例8: generateFinalizerOverride

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
private void generateFinalizerOverride() {
    final InstructionAdapter mv = new InstructionAdapter(cw.visitMethod(ACC_PUBLIC, "finalize",
            VOID_METHOD_DESCRIPTOR, null, null));
    // Overridden finalizer will take a MethodHandle to the finalizer delegating method, ...
    mv.aconst(new Handle(Opcodes.H_INVOKESTATIC, generatedClassName, FINALIZER_DELEGATE_NAME,
            FINALIZER_DELEGATE_METHOD_DESCRIPTOR));
    mv.visitVarInsn(ALOAD, 0);
    // ...and invoke it through JavaAdapterServices.invokeNoPermissions
    INVOKE_NO_PERMISSIONS.invoke(mv);
    mv.visitInsn(RETURN);
    endMethod(mv);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:14,代码来源:JavaAdapterBytecodeGenerator.java

示例9: endInitMethod

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private static void endInitMethod(final InstructionAdapter mv) {
    mv.visitInsn(RETURN);
    endMethod(mv);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:5,代码来源:JavaAdapterBytecodeGenerator.java

示例10: convertReturnValue

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
private void convertReturnValue(final InstructionAdapter mv, final Class<?> returnType, final Type asmReturnType) {
    switch(asmReturnType.getSort()) {
    case Type.VOID:
        mv.pop();
        break;
    case Type.BOOLEAN:
        JSType.TO_BOOLEAN.invoke(mv);
        break;
    case Type.BYTE:
        JSType.TO_INT32.invoke(mv);
        mv.visitInsn(Opcodes.I2B);
        break;
    case Type.SHORT:
        JSType.TO_INT32.invoke(mv);
        mv.visitInsn(Opcodes.I2S);
        break;
    case Type.CHAR:
        // JSType doesn't have a TO_CHAR, so we have services supply us one.
        mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "toCharPrimitive", TO_CHAR_PRIMITIVE_METHOD_DESCRIPTOR, false);
        break;
    case Type.INT:
        JSType.TO_INT32.invoke(mv);
        break;
    case Type.LONG:
        JSType.TO_LONG.invoke(mv);
        break;
    case Type.FLOAT:
        JSType.TO_NUMBER.invoke(mv);
        mv.visitInsn(Opcodes.D2F);
        break;
    case Type.DOUBLE:
        JSType.TO_NUMBER.invoke(mv);
        break;
    default:
        if(asmReturnType.equals(OBJECT_TYPE)) {
            // Must hide ConsString (and potentially other internal Nashorn types) from callers
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "exportReturnValue", EXPORT_RETURN_VALUE_METHOD_DESCRIPTOR, false);
        } else if(asmReturnType.equals(STRING_TYPE)){
            // Well-known conversion to String. Not using the JSType one as we want to preserve null as null instead
            // of the string "n,u,l,l".
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "toString", TO_STRING_METHOD_DESCRIPTOR, false);
        } else {
            // Invoke converter method handle for everything else. Note that we could have just added an asType or
            // filterReturnValue to the invoked handle instead, but then every instance would have the function
            // method handle wrapped in a separate converter method handle, making handle.invokeExact() megamorphic.
            if(classOverride) {
                mv.getstatic(generatedClassName, converterFields.get(returnType), METHOD_HANDLE_TYPE_DESCRIPTOR);
            } else {
                mv.visitVarInsn(ALOAD, 0);
                mv.getfield(generatedClassName, converterFields.get(returnType), METHOD_HANDLE_TYPE_DESCRIPTOR);
            }
            mv.swap();
            emitInvokeExact(mv, MethodType.methodType(returnType, Object.class));
        }
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:57,代码来源:JavaAdapterBytecodeGenerator.java

示例11: createNoPermissionsInvoker

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:44,代码来源:JavaAdapterServices.java

示例12: createNoPermissionsInvoker

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:44,代码来源:JavaAdapterServices.java

示例13: generateOverridingConstructor

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:RedlineResearch,项目名称:OLD-OpenJDK8,代码行数:80,代码来源:JavaAdapterBytecodeGenerator.java

示例14: createNoPermissionsInvoker

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:RedlineResearch,项目名称:OLD-OpenJDK8,代码行数:44,代码来源:JavaAdapterServices.java

示例15: generateOverridingConstructor

import jdk.internal.org.objectweb.asm.commons.InstructionAdapter; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:wro4j,项目名称:nashorn-backport,代码行数:80,代码来源:JavaAdapterBytecodeGenerator.java


注:本文中的jdk.internal.org.objectweb.asm.commons.InstructionAdapter.visitInsn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。