Example usage for org.objectweb.asm.commons InstructionAdapter invokestatic

List of usage examples for org.objectweb.asm.commons InstructionAdapter invokestatic

Introduction

In this page you can find the example usage for org.objectweb.asm.commons InstructionAdapter invokestatic.

Prototype

public void invokestatic(final String owner, final String name, final String descriptor,
        final boolean isInterface) 

Source Link

Document

Generates the instruction to call the given static method.

Usage

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private void generateClassInit() {
    final InstructionAdapter mv = new InstructionAdapter(
            cw.visitMethod(ACC_STATIC, CLASS_INIT, Type.getMethodDescriptor(Type.VOID_TYPE), null, null));

    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getClassOverrides", GET_CLASS_INITIALIZER_DESCRIPTOR, false);
    final Label initGlobal;
    if (samName != null) {
        // If the class is a SAM, allow having a ScriptFunction passed as class overrides
        final Label notAFunction = new Label();
        mv.dup();/*from w  w  w  .j  ava2s.  co m*/
        mv.instanceOf(SCRIPT_FUNCTION_TYPE);
        mv.ifeq(notAFunction);
        mv.checkcast(SCRIPT_FUNCTION_TYPE);

        // Assign MethodHandle fields through invoking getHandle() for a ScriptFunction, only assigning the SAM
        // method(s).
        for (final MethodInfo mi : methodInfos) {
            if (mi.getName().equals(samName)) {
                mv.dup();
                loadMethodTypeAndGetHandle(mv, mi, GET_HANDLE_FUNCTION_DESCRIPTOR);
            } else {
                mv.visitInsn(ACONST_NULL);
            }
            mv.putstatic(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
        }
        initGlobal = new Label();
        mv.goTo(initGlobal);
        mv.visitLabel(notAFunction);
    } else {
        initGlobal = null;
    }
    // Assign MethodHandle fields through invoking getHandle() for a ScriptObject
    for (final MethodInfo mi : methodInfos) {
        mv.dup();
        mv.aconst(mi.getName());
        loadMethodTypeAndGetHandle(mv, mi, GET_HANDLE_OBJECT_DESCRIPTOR);
        mv.putstatic(generatedClassName, mi.methodHandleFieldName, METHOD_HANDLE_TYPE_DESCRIPTOR);
    }

    if (initGlobal != null) {
        mv.visitLabel(initGlobal);
    }
    // Assign "global = Context.getGlobal()"
    invokeGetGlobalWithNullCheck(mv);
    mv.putstatic(generatedClassName, GLOBAL_FIELD_NAME, GLOBAL_TYPE_DESCRIPTOR);

    generateConverterInit(mv, false);
    endInitMethod(mv);
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private void generateConverterInit(final InstructionAdapter mv, final boolean samOnly) {
    assert !samOnly || !classOverride;
    for (final Map.Entry<Class<?>, String> converterField : converterFields.entrySet()) {
        final Class<?> returnType = converterField.getKey();
        if (!classOverride) {
            mv.visitVarInsn(ALOAD, 0);//from   ww w . j a  va2 s .c  om
        }

        if (samOnly && !samReturnTypes.contains(returnType)) {
            mv.visitInsn(ACONST_NULL);
        } else {
            mv.aconst(Type.getType(converterField.getKey()));
            mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getObjectConverter", GET_CONVERTER_METHOD_DESCRIPTOR,
                    false);
        }

        if (classOverride) {
            mv.putstatic(generatedClassName, converterField.getValue(), METHOD_HANDLE_TYPE_DESCRIPTOR);
        } else {
            mv.putfield(generatedClassName, converterField.getValue(), METHOD_HANDLE_TYPE_DESCRIPTOR);
        }
    }
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private static void loadMethodTypeAndGetHandle(final InstructionAdapter mv, final MethodInfo mi,
        final String getHandleDescriptor) {
    // NOTE: we're using generic() here because we'll be linking to the "generic" invoker version of
    // the functions anyway, so we cut down on megamorphism in the invokeExact() calls in adapter
    // bodies. Once we start linking to type-specializing invokers, this should be changed.
    mv.aconst(Type.getMethodType(mi.type.generic().toMethodDescriptorString()));
    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getHandle", getHandleDescriptor, false);
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private void generateOverridingConstructorWithObjectParam(final InstructionAdapter mv,
        final Constructor<?> ctor, final String ctorDescriptor) {
    mv.visitCode();/*w  ww  . j  av a  2  s  .c  o m*/
    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);
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private static void invokeGetGlobal(final InstructionAdapter mv) {
    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "getGlobal", GET_GLOBAL_METHOD_DESCRIPTOR, false);
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private static void invokeSetGlobal(final InstructionAdapter mv) {
    mv.invokestatic(SERVICES_CLASS_TYPE_NAME, "setGlobal", SET_GLOBAL_METHOD_DESCRIPTOR, false);
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private void convertReturnValue(final InstructionAdapter mv, final Class<?> returnType,
        final Type asmReturnType) {
    switch (asmReturnType.getSort()) {
    case Type.VOID:
        mv.pop();//from  w  ww. j ava2  s.  c o m
        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));
        }
    }
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private static void boxStackTop(final InstructionAdapter mv, final Type t) {
    switch (t.getSort()) {
    case Type.BOOLEAN:
        invokeValueOf(mv, "Boolean", 'Z');
        break;/*from   w  ww  . jav a 2 s  .com*/
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        // bytes and shorts get boxed as integers
        invokeValueOf(mv, "Integer", 'I');
        break;
    case Type.CHAR:
        invokeValueOf(mv, "Character", 'C');
        break;
    case Type.FLOAT:
        // floats get boxed as doubles
        mv.visitInsn(Opcodes.F2D);
        invokeValueOf(mv, "Double", 'D');
        break;
    case Type.LONG:
        invokeValueOf(mv, "Long", 'J');
        break;
    case Type.DOUBLE:
        invokeValueOf(mv, "Double", 'D');
        break;
    case Type.ARRAY:
    case Type.METHOD:
        // Already boxed
        break;
    case Type.OBJECT:
        if (t.equals(OBJECT_TYPE)) {
            mv.invokestatic(SCRIPTUTILS_TYPE_NAME, "unwrap", UNWRAP_METHOD_DESCRIPTOR, false);
        }
        break;
    default:
        // Not expecting anything else (e.g. VOID)
        assert false;
        break;
    }
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

private static void invokeValueOf(final InstructionAdapter mv, final String boxedType, final char unboxedType) {
    mv.invokestatic("java/lang/" + boxedType, "valueOf", "(" + unboxedType + ")Ljava/lang/" + boxedType + ";",
            false);//w w w  .  j av a 2 s  .  c o  m
}

From source file:com.gargoylesoftware.js.nashorn.internal.runtime.linker.JavaAdapterBytecodeGenerator.java

License:Open Source License

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);/*from  w ww  .ja  va  2s  .  c  o  m*/
    // ...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);
}