Example usage for org.objectweb.asm MethodVisitor visitParameter

List of usage examples for org.objectweb.asm MethodVisitor visitParameter

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitParameter.

Prototype

public void visitParameter(final String name, final int access) 

Source Link

Document

Visits a parameter of this method.

Usage

From source file:org.moe.natj.processor.cxx.BCGen.java

License:Apache License

/**
 * Generates a constructor with the specified arguments and passes them to the super constructor.
 *
 * @param visitor  Class visitor/*from ww w.  java  2s  .  c o m*/
 * @param superCls Super class type
 * @param args     Argument types
 */
public static void constructor(ClassVisitor visitor, Type superCls, Type... args) {
    final String methodDesc = Type.getMethodDescriptor(Type.VOID_TYPE, args);

    // Create visitor
    final MethodVisitor mv = visitor.visitMethod(ACC_PUBLIC, "<init>", methodDesc, null, null);

    // Apply parameters
    mv.visitParameter("pointer", ACC_FINAL);

    // Call super
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    int index = 1;
    for (Type arg : args) {
        index += loadType(mv, arg, index);
    }
    mv.visitMethodInsn(INVOKESPECIAL, superCls.getInternalName(), "<init>", methodDesc, false);

    // Close
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.moe.natj.processor.cxx.BCGen.java

License:Apache License

/**
 * Creates a method for invoking the static native bridge method.
 *
 * @param visitor    Class visitor/*from   ww w .j a v  a2 s.c  om*/
 * @param declInfo   declaration info
 * @param bridgeType Implementing class of the bridge method
 * @param analyzer   analyzer
 */
public static void toNativeBridgeInvoker(ClassVisitor visitor, DeclInfo declInfo, Type bridgeType,
        CxxAnalyzer analyzer) {
    // Update access
    int access = declInfo.getAccess();
    access &= ~ACC_ABSTRACT & ~ACC_NATIVE;

    // Create visitor
    final MethodVisitor mv = visitor.visitMethod(access, declInfo.getName(), declInfo.getDesc().getDescriptor(),
            null, null);

    // Apply parameters
    for (ParamInfo paramInfo : declInfo.getParams()) {
        mv.visitParameter(paramInfo.getName(), paramInfo.getAccess());
    }

    // Load _cxx_rt_peer
    mv.visitCode();
    int localIndexStart = 0;
    if (declInfo.getKindInfo().isClassMember()) {
        load_cxx_rt_peer(mv);
        ++localIndexStart;
    }

    // Load parameters
    final CxxUtils.LocalManager localManager = CxxUtils.loadParameters(declInfo, localIndexStart, mv);

    // Invoke native
    mv.visitMethodInsn(INVOKESTATIC, bridgeType.getInternalName(), declInfo.getJavaStaticStubMethodName(),
            declInfo.getJavaStaticStubMethodType().getDescriptor(), false);

    // Close
    fromNativeReturnType(mv, declInfo.getType(), localManager, analyzer);
    localManager.close(mv);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.moe.natj.processor.cxx.BCGen.java

License:Apache License

/**
 * Creates a method for throwing a new UnsupportedOperationException.
 *
 * @param visitor  Class visitor/*  w w  w.  j  a va 2s. co  m*/
 * @param declInfo declaration info
 * @param message  message
 */
private static void toNativeBridgeUnsupportedOperationExeption(ClassVisitor visitor, DeclInfo declInfo,
        String message) {
    // Update access
    int access = declInfo.getAccess();
    access &= ~ACC_ABSTRACT & ~ACC_NATIVE;

    // Create visitor
    final MethodVisitor mv = visitor.visitMethod(access, declInfo.getName(), declInfo.getDesc().getDescriptor(),
            null, null);

    // Apply parameters
    for (ParamInfo paramInfo : declInfo.getParams()) {
        mv.visitParameter(paramInfo.getName(), paramInfo.getAccess());
    }

    // Throw exception
    mv.visitCode();
    throw_new_UnsupportedOperationException(mv, message);

    // Close
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.moe.natj.processor.cxx.BCGen.java

License:Apache License

/**
 * Creates a native static method for the toNative bridge method.
 *
 * @param visitor  Class visitor/*from   www  .j av a 2  s .c  o m*/
 * @param declInfo declaration info
 */
public static void toNativeBridge(ClassVisitor visitor, DeclInfo declInfo) {
    final boolean isConstructorForPureClass = declInfo instanceof ConstructorInfo
            && ((ConstructorInfo) declInfo).getClassInfo().getClassHasPureVirtualMethod();

    // Update access
    int access = declInfo.getAccess();
    access &= ~ACC_ABSTRACT & ~ACC_PRIVATE & ~ACC_PROTECTED;
    access |= ACC_PUBLIC;
    access |= ACC_STATIC;
    if (!isConstructorForPureClass) {
        access |= ACC_NATIVE;
    } else {
        access &= ~ACC_NATIVE;
    }

    // Create visitor
    final MethodVisitor mv = visitor.visitMethod(access, declInfo.getJavaStaticStubMethodName(),
            declInfo.getJavaStaticStubMethodType().getDescriptor(), null, null);

    // Apply parameters
    if (declInfo.getKindInfo().isClassMember()) {
        mv.visitParameter("_cxx_rt_peer", 0);
    }
    for (ParamInfo paramInfo : declInfo.getParams()) {
        mv.visitParameter(paramInfo.getName(), paramInfo.getAccess());
    }

    // Throw exception for pure virtual classes
    if (isConstructorForPureClass) {
        mv.visitCode();
        throw_new_UnsupportedOperationException(mv, "Classes with pure virtual methods cannot be instantiated");
        mv.visitMaxs(0, 0);
    }

    // Close
    mv.visitEnd();
}

From source file:org.moe.natj.processor.cxx.BCGen.java

License:Apache License

/**
 * Creates a static method for the toJava bridge method.
 *
 * @param visitor  Class visitor//from www. j a  v a 2 s.  co m
 * @param declInfo Declaration info
 * @param node     Method node
 * @param type     Inherited type
 * @param analyzer Analyzer
 */
public static void toJavaBridge(ClassVisitor visitor, DeclInfo declInfo, MethodNode node, Type type,
        CxxAnalyzer analyzer) {
    // Create method visitor
    int acc = declInfo.getAccess();
    acc |= ACC_STATIC;
    MethodVisitor mv = visitor.visitMethod(acc, declInfo.getJavaStaticBridgeStubMethodName(),
            declInfo.getJavaStaticStubMethodType().getDescriptor(), null,
            (String[]) node.exceptions.toArray(new String[node.exceptions.size()]));

    mv.visitParameter("_cxx_rt_java_peer", ACC_FINAL);
    if (node.parameters != null) {
        for (Object parameter : node.parameters) {
            ((ParameterNode) parameter).accept(mv);
        }
    }

    // Load parameters
    mv.visitCode();
    mv.visitVarInsn(LLOAD, 0);
    mv.visitMethodInsn(INVOKESTATIC, CxxSupport.CXX_RUNTIME.getInternalName(), "getObjectForUID",
            Type.getMethodDescriptor(Type.getObjectType("java/lang/Object"), Type.LONG_TYPE), false);
    mv.visitTypeInsn(CHECKCAST, type.getInternalName());
    final CxxUtils.LocalManager localManager = CxxUtils.loadParametersToJava(declInfo, 2, mv, analyzer);

    // Invoke native
    mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), declInfo.getName(),
            declInfo.getDesc().getDescriptor(), false);

    // Close
    BCGen.fromJavaReturnType(mv, declInfo.getType(), localManager);
    localManager.close(mv);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.spongepowered.eventimplgen.factory.FactoryInterfaceGenerator.java

License:MIT License

private static void generateRealImpl(ClassWriter cw, CtType<?> event, String eventName, List<Property> params) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, EventImplGenTask.generateMethodName(event),
            getDescriptor(event, params), null, null);

    Label start = new Label();
    Label end = new Label();

    mv.visitCode();//  ww w  .  java 2 s.c  o  m

    mv.visitLabel(start);

    mv.visitTypeInsn(NEW, eventName);
    mv.visitInsn(DUP);

    int[] slots = new int[params.size()];

    for (int i = 0, slot = 0; i < params.size(); i++, slot++) {
        Property param = params.get(i);
        slots[i] = slot;
        Type type = Type.getType(ClassGenerator.getTypeDescriptor(param.getType()));
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), slot); // Parameters start at slot 0 for static methods

        if (type.getSize() > 1) {
            slot++; // Skip over unusable following slot
        }
    }
    mv.visitMethodInsn(INVOKESPECIAL, eventName, "<init>", getDescriptor(null, params), false);

    mv.visitInsn(ARETURN);
    mv.visitLabel(end);

    for (int i = 0; i < params.size(); i++) {
        Property property = params.get(i);
        mv.visitLocalVariable(property.getName(), ClassGenerator.getTypeDescriptor(property.getType()), null,
                start, end, slots[i]);
        mv.visitParameter(property.getName(), 0);
    }

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}