Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

List of usage examples for org.objectweb.asm Opcodes ACC_PUBLIC

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ACC_PUBLIC.

Prototype

int ACC_PUBLIC

To view the source code for org.objectweb.asm Opcodes ACC_PUBLIC.

Click Source Link

Usage

From source file:de.sanandrew.core.manpack.transformer.TransformEntityThrowable.java

License:Creative Commons License

private static byte[] transformLqThrowable(byte[] bytes) {
    ClassNode classNode = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID);

    method.visitCode();//from ww  w.ja  va  2 s  .c o  m
    Label label1 = new Label();
    method.visitLabel(label1);
    method.visitInsn(Opcodes.ICONST_0);
    method.visitInsn(Opcodes.IRETURN);
    Label label2 = new Label();
    method.visitLabel(label2);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_THROWABLE, null, label1, label2, 0);
    method.visitMaxs(0, 0);
    method.visitEnd();
    classNode.methods.add(method);

    method = ASMHelper.findMethod(classNode, ASMNames.MD_THROWABLE_ON_UPDATE);

    InsnList needle = new InsnList();
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_MOTION_Z));
    needle.add(new InsnNode(Opcodes.DADD));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_VEC3_CREATE_VECTOR_HELPER, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, 2));
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 2));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS, false));
    needle.add(new VarInsnNode(Opcodes.ASTORE, -1));

    VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new LabelNode());
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 1));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 2));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID, false));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS_Z, false));
    injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var));

    method.instructions.insert(insertPoint, injectList);

    return ASMHelper.createBytes(classNode, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
}

From source file:de.sanandrew.core.manpack.transformer.TransformPlayerDismountCtrl.java

License:Creative Commons License

/**
 * Transforms the Entity.class by adding a new method called _SAP_canDismountOnInput.<br>
 * This method can be overridden by any entity to control wether or not the rider can dismount via sneaking (usually by pressing LSHIFT for the player).
 *
 * @param bytes     the class bytes to be transformed
 * @return the transformed class bytes//w  w  w.  j ava  2  s . c  o  m
 */
private static byte[] transformEntity(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_DISMOUNT_ON_INPUT);
    method.visitCode();
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitInsn(Opcodes.ICONST_1);
    method.visitInsn(Opcodes.IRETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0);
    method.visitLocalVariable("player", ASMNames.CL_T_ENTITY_PLAYER, null, l0, l1, 1);
    method.visitMaxs(1, 2);
    method.visitEnd();

    clazz.methods.add(method);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
    return bytes;
}

From source file:de.scoopgmbh.copper.instrument.MethodInfo.java

License:Apache License

public String getDeclaration() {
    StringBuilder sb = new StringBuilder();
    if ((access & Opcodes.ACC_PRIVATE) != 0)
        sb.append("private ");
    if ((access & Opcodes.ACC_PROTECTED) != 0)
        sb.append("protected ");
    if ((access & Opcodes.ACC_PUBLIC) != 0)
        sb.append("public ");
    if ((access & Opcodes.ACC_STATIC) != 0)
        sb.append("static ");
    if ((access & Opcodes.ACC_FINAL) != 0)
        sb.append("final ");
    sb.append(Type.getReturnType(descriptor).getClassName()).append(' ').append(methodName).append('(');

    boolean first = true;
    for (Type t : Type.getArgumentTypes(descriptor)) {
        if (!first)
            sb.append(", ");
        first = false;//from w  w w.  j a v a 2  s .c  om
        sb.append(t.getClassName());
    }
    sb.append(')');
    return sb.toString();
}

From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilder.java

License:Open Source License

private static MethodNode createMethodNode(final ClassNode node) {
    final MethodNode constructor = new MethodNode();
    constructor.access = Opcodes.ACC_PUBLIC;
    constructor.name = "<init>";
    constructor.exceptions = Collections.emptyList();
    final InsnList list = new InsnList();
    // currently only call to noarg super constructor is supported
    final AbstractInsnNode nThis = new VarInsnNode(Opcodes.ALOAD, 0);
    final AbstractInsnNode nSuperConstructor = new MethodInsnNode(Opcodes.INVOKESPECIAL, node.superName,
            "<init>", "()V");
    list.add(nThis);//from w w  w  .  j  a  va  2s.com
    list.add(nSuperConstructor);
    constructor.instructions = list;
    return constructor;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

private int getEffectiveModifier(final int... modifiers) {
    int modifier = 0;
    if (modifiers == null || modifiers.length == 0) {
        modifier = Opcodes.ACC_PUBLIC;
    } else {/*from w  w w  . ja va 2  s. c o  m*/
        for (int i = 0; i < modifiers.length; ++i) {
            modifier |= modifiers[i];
        }
    }
    return modifier;
}

From source file:de.zib.sfs.instrument.AbstractSfsAdapter.java

License:BSD License

@Override
public void visitEnd() {
    // public void setInstrumentationActive(boolean instrumentationActive) {
    MethodVisitor setInstrumentationActiveMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC,
            "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), null,
            null);//from   w  w w .  ja v a  2 s.  c om
    setInstrumentationActiveMV.visitCode();

    // this.instrumentationActive.setInstrumentationActive(instrumentationActive);
    setInstrumentationActiveMV.visitVarInsn(Opcodes.ALOAD, 0);
    setInstrumentationActiveMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName,
            "instrumentationActive", Type.getDescriptor(InstrumentationActive.class));
    setInstrumentationActiveMV.visitVarInsn(Opcodes.ILOAD, 1);
    setInstrumentationActiveMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
            Type.getInternalName(InstrumentationActive.class), "setInstrumentationActive",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

    // }
    setInstrumentationActiveMV.visitInsn(Opcodes.RETURN);
    setInstrumentationActiveMV.visitMaxs(0, 0);
    setInstrumentationActiveMV.visitEnd();

    // public boolean isInstrumentationActive() {
    MethodVisitor isInstrumentationActiveMV = this.cv.visitMethod(Opcodes.ACC_PUBLIC, "isInstrumentationActive",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), null, null);
    isInstrumentationActiveMV.visitCode();

    // return instrumentationActive.isInstrumentationActive();
    // }
    isInstrumentationActiveMV.visitVarInsn(Opcodes.ALOAD, 0);
    isInstrumentationActiveMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName,
            "instrumentationActive", Type.getDescriptor(InstrumentationActive.class));
    isInstrumentationActiveMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
            Type.getInternalName(InstrumentationActive.class), "isInstrumentationActive",
            Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
    isInstrumentationActiveMV.visitInsn(Opcodes.IRETURN);
    isInstrumentationActiveMV.visitMaxs(0, 0);
    isInstrumentationActiveMV.visitEnd();

    appendWrappedMethods(this.cv);

    this.cv.visitEnd();
}

From source file:de.zib.sfs.instrument.DirectByteBufferAdapter.java

License:BSD License

@Override
protected void appendWrappedMethods(ClassVisitor visitor) {
    // override from MappedByteBuffer so we can re-init the callback
    // properly//from  w ww .jav a  2s. c  om

    // public void setFileDescriptor(FileDescriptor fileDescriptor) {
    MethodVisitor settFileDescriptorMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "setFileDescriptor",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)), null, null);
    settFileDescriptorMV.visitCode();

    // this.fileDescriptor = fileDescriptor;
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 1);
    settFileDescriptorMV.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(MappedByteBuffer.class),
            "fileDescriptor", Type.getDescriptor(FileDescriptor.class));

    // callback.openCallback(this.fileDescriptor);
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    settFileDescriptorMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback",
            this.callbackTypeDescriptor);
    settFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    settFileDescriptorMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class),
            "fileDescriptor", Type.getDescriptor(FileDescriptor.class));
    settFileDescriptorMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, "openCallback",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)), false);

    // }
    settFileDescriptorMV.visitInsn(Opcodes.RETURN);
    settFileDescriptorMV.visitMaxs(0, 0);
    settFileDescriptorMV.visitEnd();

    // also override from MappedByteBuffer

    // protected FileDescriptor getFileDescriptorImpl() {
    MethodVisitor getFileDescriptorImplMV = visitor.visitMethod(Opcodes.ACC_PROTECTED, "getFileDescriptorImpl",
            Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), null, null);
    getFileDescriptorImplMV.visitCode();

    // return fileDescriptor;
    // }
    getFileDescriptorImplMV.visitVarInsn(Opcodes.ALOAD, 0);
    getFileDescriptorImplMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(MappedByteBuffer.class),
            "fileDescriptor", Type.getDescriptor(FileDescriptor.class));
    getFileDescriptorImplMV.visitInsn(Opcodes.ARETURN);
    getFileDescriptorImplMV.visitMaxs(0, 0);
    getFileDescriptorImplMV.visitEnd();

    if (!skipReads()) {
        wrapMethod(Opcodes.ACC_PUBLIC, "get", Type.getType(ByteBuffer.class),
                new Type[] { Type.getType(byte[].class), Type.INT_TYPE, Type.INT_TYPE }, null, null,
                "getCallback", Type.INT_TYPE, new ParameterResultPasser(3));
    }

    if (!skipWrites()) {
        wrapMethod(Opcodes.ACC_PUBLIC, "put", Type.getType(ByteBuffer.class),
                new Type[] { Type.getType(byte[].class), Type.INT_TYPE, Type.INT_TYPE }, null, null,
                "putCallback", Type.INT_TYPE, new ParameterResultPasser(3));
    }

    if (!skipWrites()) {
        // public ByteBuffer put(ByteBuffer src) {
        MethodVisitor bulkPutMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "put",
                Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class)), null,
                null);
        bulkPutMV.visitCode();

        // if (isInstrumentationActive()) {
        isInstrumentationActive(bulkPutMV);
        Label instrumentationActiveLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, instrumentationActiveLabel);

        // return nativeMethodPrefixput(src);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName,
                this.methodPrefix + "put",
                Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class)),
                false);
        bulkPutMV.visitInsn(Opcodes.ARETURN);

        // }
        bulkPutMV.visitLabel(instrumentationActiveLabel);

        // setInstrumentationActive(fromFileChannel);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "fromFileChannel",
                Type.getDescriptor(Boolean.TYPE));
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.instrumentedTypeInternalName,
                "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // boolean srcInstrumentationActive = false;
        bulkPutMV.visitInsn(Opcodes.ICONST_0);
        bulkPutMV.visitVarInsn(Opcodes.ISTORE, 2);

        // if (src instanceof MappedByteBuffer) {
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitTypeInsn(Opcodes.INSTANCEOF, Type.getInternalName(MappedByteBuffer.class));
        Label srcInstanceofMappedByteBufferLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, srcInstanceofMappedByteBufferLabel);

        // srcInstrumentationActive = src.isFromFileChannel();
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "isFromFileChannel", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false);
        bulkPutMV.visitVarInsn(Opcodes.ISTORE, 2);

        // src.setInstrumentationActive(true);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitInsn(Opcodes.ICONST_1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // }
        bulkPutMV.visitLabel(srcInstanceofMappedByteBufferLabel);

        // int length = src.remaining();
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Buffer.class), "remaining",
                Type.getMethodDescriptor(Type.INT_TYPE), false);
        bulkPutMV.visitVarInsn(Opcodes.ISTORE, 4);

        // long startTime = System.nanoTime();
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESTATIC, this.systemInternalName, "nanoTime",
                this.nanoTimeDescriptor, false);
        bulkPutMV.visitVarInsn(Opcodes.LSTORE, 5);

        // ByteBuffer result = nativeMethodPrefixput(src);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName,
                this.methodPrefix + "put",
                Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class)),
                false);
        bulkPutMV.visitVarInsn(Opcodes.ASTORE, 7);

        // long endTime = System.nanoTime();
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESTATIC, this.systemInternalName, "nanoTime",
                this.nanoTimeDescriptor, false);
        bulkPutMV.visitVarInsn(Opcodes.LSTORE, 8);

        // if (isInstrumentationActive()) {
        isInstrumentationActive(bulkPutMV);
        Label instrumentationStillActiveLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, instrumentationStillActiveLabel);

        // callback.putCallback(startTime, endTime, length);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 0);
        bulkPutMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "callback",
                this.callbackTypeDescriptor);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 5);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 8);
        bulkPutMV.visitVarInsn(Opcodes.ILOAD, 4);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.callbackTypeInternalName, "putCallback",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.LONG_TYPE, Type.LONG_TYPE, Type.INT_TYPE), false);

        // setInstrumentationActive(false);
        setInstrumentationActive(bulkPutMV, false);

        // }
        bulkPutMV.visitLabel(instrumentationStillActiveLabel);

        // if (srcInstrumentationActive) {
        bulkPutMV.visitVarInsn(Opcodes.ILOAD, 2);
        Label srcInstrumentationActiveLabel = new Label();
        bulkPutMV.visitJumpInsn(Opcodes.IFEQ, srcInstrumentationActiveLabel);

        // callback.onGetEnd(src.getFileDescriptor(), startTime, endTime,
        // length);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "getFileDescriptor", Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), false);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 5);
        bulkPutMV.visitVarInsn(Opcodes.LLOAD, 8);
        bulkPutMV.visitVarInsn(Opcodes.ILOAD, 4);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKESTATIC, this.callbackTypeInternalName, "getCallback",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class), Type.LONG_TYPE,
                        Type.LONG_TYPE, Type.INT_TYPE),
                false);

        // src.setInstrumentationActive(false);
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 1);
        bulkPutMV.visitInsn(Opcodes.ICONST_0);
        bulkPutMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
                "setInstrumentationActive", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

        // }
        bulkPutMV.visitLabel(srcInstrumentationActiveLabel);

        // return result;
        // }
        bulkPutMV.visitVarInsn(Opcodes.ALOAD, 7);
        bulkPutMV.visitInsn(Opcodes.ARETURN);
        bulkPutMV.visitMaxs(0, 0);
        bulkPutMV.visitEnd();
    }

    ResultPasser resultDiscarder = new DiscardResultPasser();

    // regular gets and puts
    for (Map.Entry<String, Type> type : TYPES.entrySet()) {
        if (!skipReads()) {
            // public TYPE getTYPE() { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "get" + type.getKey(), type.getValue(), null, null, null,
                    "get" + type.getKey() + "Callback", null, resultDiscarder);

            // public TYPE getTYPE(int index) { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "get" + type.getKey(), type.getValue(), new Type[] { Type.INT_TYPE },
                    null, null, "get" + type.getKey() + "Callback", null, resultDiscarder);
        }

        if (!skipWrites()) {
            // public ByteBuffer putTYPE(TYPE value) { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "put" + type.getKey(), Type.getType(ByteBuffer.class),
                    new Type[] { type.getValue() }, null, null, "put" + type.getKey() + "Callback", null,
                    resultDiscarder);

            // public ByteBuffer putTYPE(int index, TYPE value) { ... }
            wrapMethod(Opcodes.ACC_PUBLIC, "put" + type.getKey(), Type.getType(ByteBuffer.class),
                    new Type[] { Type.INT_TYPE, type.getValue() }, null, null,
                    "put" + type.getKey() + "Callback", null, resultDiscarder);
        }
    }

    visitor.visitEnd();
}

From source file:de.zib.sfs.instrument.DirectByteBufferAdapter.java

License:BSD License

private boolean isGetMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (Opcodes.ACC_PUBLIC != access || null != signature || (exceptions != null && exceptions.length != 0)) {
        return false;
    }/*from   w  ww  . ja  v a2  s  .  c o m*/

    // bulk get
    if ("get".equals(name)) {
        if (Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(byte[].class), Type.INT_TYPE,
                Type.INT_TYPE).equals(desc)) {
            return !skipReads();
        }
    }

    // regular gets
    for (Map.Entry<String, Type> type : TYPES.entrySet()) {
        if (("get" + type.getKey()).equals(name)) {
            if (Type.getMethodDescriptor(type.getValue()).equals(desc)
                    || Type.getMethodDescriptor(type.getValue(), Type.INT_TYPE).equals(desc)) {
                return !skipReads();
            }
        }
    }

    return false;
}

From source file:de.zib.sfs.instrument.DirectByteBufferAdapter.java

License:BSD License

private boolean isPutMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (Opcodes.ACC_PUBLIC != access || null != signature || (exceptions != null && exceptions.length != 0)) {
        return false;
    }/* w  w  w  .j a  va2 s.  co m*/

    // bulk puts
    if ("put".equals(name)) {
        if (Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(byte[].class), Type.INT_TYPE,
                Type.INT_TYPE).equals(desc)
                || Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.getType(ByteBuffer.class))
                        .equals(desc)) {
            return !skipWrites();
        }
    }

    // regular puts
    for (Map.Entry<String, Type> type : TYPES.entrySet()) {
        if (("put" + type.getKey()).equals(name)) {
            if (Type.getMethodDescriptor(Type.getType(ByteBuffer.class), type.getValue()).equals(desc)
                    || Type.getMethodDescriptor(Type.getType(ByteBuffer.class), Type.INT_TYPE, type.getValue())
                            .equals(desc)) {
                return !skipWrites();
            }
        }
    }

    return false;
}

From source file:de.zib.sfs.instrument.FileChannelImplAdapter.java

License:BSD License

@Override
protected void appendWrappedMethods(ClassVisitor visitor) {
    // public FileDescriptor getFileDescriptor() {
    MethodVisitor getFileDescriptorMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "getFileDescriptor",
            Type.getMethodDescriptor(Type.getType(FileDescriptor.class)), null, null);
    getFileDescriptorMV.visitCode();/*w w  w.  j a v  a2 s  .  c  o  m*/

    // return fileDescriptor;
    // }
    getFileDescriptorMV.visitVarInsn(Opcodes.ALOAD, 0);
    getFileDescriptorMV.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(FileChannelImpl.class), "fd",
            Type.getDescriptor(FileDescriptor.class));
    getFileDescriptorMV.visitInsn(Opcodes.ARETURN);
    getFileDescriptorMV.visitMaxs(0, 0);
    getFileDescriptorMV.visitEnd();

    if (!skipReads()) {
        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "read", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class) }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "read", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class), Type.LONG_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "read", Type.LONG_TYPE,
                new Type[] { Type.getType(ByteBuffer[].class), Type.INT_TYPE, Type.INT_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.LONG_TYPE, 0, false);

        // transferTo is basically a read

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "transferTo", Type.LONG_TYPE,
                new Type[] { Type.LONG_TYPE, Type.LONG_TYPE, Type.getType(WritableByteChannel.class) }, null,
                new String[] { Type.getInternalName(IOException.class) }, "readCallback", "writeCallback",
                Type.LONG_TYPE, 2, true);
    }

    // repeat for write methods

    if (!skipWrites()) {
        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "write", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class) }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "write", Type.INT_TYPE,
                new Type[] { Type.getType(ByteBuffer.class), Type.LONG_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.INT_TYPE, 0, false);

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "write", Type.LONG_TYPE,
                new Type[] { Type.getType(ByteBuffer[].class), Type.INT_TYPE, Type.INT_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.LONG_TYPE, 0, false);

        // transferFrom is basically a write

        wrapFileChannelImplMethod(Opcodes.ACC_PUBLIC, "transferFrom", Type.LONG_TYPE,
                new Type[] { Type.getType(ReadableByteChannel.class), Type.LONG_TYPE, Type.LONG_TYPE }, null,
                new String[] { Type.getInternalName(IOException.class) }, "writeCallback", "readCallback",
                Type.LONG_TYPE, 0, true);
    }

    String mapMethodDescriptor = Type.getMethodDescriptor(Type.getType(MappedByteBuffer.class),
            Type.getType(MapMode.class), Type.LONG_TYPE, Type.LONG_TYPE);

    // public MappedByteBuffer map(MapMode mode, long position, long size)
    // throws IOException {
    MethodVisitor mapMV = visitor.visitMethod(Opcodes.ACC_PUBLIC, "map", mapMethodDescriptor, null,
            new String[] { Type.getInternalName(IOException.class) });
    mapMV.visitCode();

    // MappedByteBuffer mbb = nativeMethodPrefixmap(mode, position, size);
    mapMV.visitVarInsn(Opcodes.ALOAD, 0);
    mapMV.visitVarInsn(Opcodes.ALOAD, 1);
    mapMV.visitVarInsn(Opcodes.LLOAD, 2);
    mapMV.visitVarInsn(Opcodes.LLOAD, 4);
    mapMV.visitMethodInsn(Opcodes.INVOKESPECIAL, this.instrumentedTypeInternalName, this.methodPrefix + "map",
            mapMethodDescriptor, false);
    mapMV.visitVarInsn(Opcodes.ASTORE, 6);

    // mbb.setFromFileChannel(true);
    mapMV.visitVarInsn(Opcodes.ALOAD, 6);
    mapMV.visitInsn(Opcodes.ICONST_1);
    mapMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
            "setFromFileChannel", Type.getMethodDescriptor(Type.VOID_TYPE, Type.BOOLEAN_TYPE), false);

    // mbb.setFileDescriptor(fd);
    mapMV.visitVarInsn(Opcodes.ALOAD, 6);
    mapMV.visitVarInsn(Opcodes.ALOAD, 0);
    mapMV.visitFieldInsn(Opcodes.GETFIELD, this.instrumentedTypeInternalName, "fd",
            Type.getDescriptor(FileDescriptor.class));
    mapMV.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(MappedByteBuffer.class),
            "setFileDescriptor", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(FileDescriptor.class)),
            false);

    // if we don't want to trace mmap calls, then map needs to reset the
    // instrumentationActive flag
    if (!this.traceMmap) {
        // setInstrumentationActive(false);
        setInstrumentationActive(mapMV, false);
    }

    // return mbb;
    // }
    mapMV.visitVarInsn(Opcodes.ALOAD, 6);
    mapMV.visitInsn(Opcodes.ARETURN);
    mapMV.visitMaxs(0, 0);
    mapMV.visitEnd();
}