<commit_before>/*
 * Copyright (c) 2016, <NAME>
 *
 * BSD 3-clause License
 *
 * Author: <NAME> (rvprasad)
 */

package dyco4j.instrumentation.internals;

import dyco4j.LoggingHelper;
import dyco4j.instrumentation.LoggerInitializingMethodVisitor;
import dyco4j.instrumentation.logging.Logger.Action;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.Method;

final class TracingMethodVisitor extends LoggerInitializingMethodVisitor<TracingClassVisitor> {
    private final String methodId;
    private final Method method;
    private final boolean isStatic;
    private Label beginOutermostHandler;

    TracingMethodVisitor(final int access, final String name, final String desc, final String signature,
                         final String[] exceptions, final MethodVisitor mv, final TracingClassVisitor owner) {
        super(CLI.ASM_VERSION, mv, name, owner);
        this.method = new Method(name, desc);
        this.isStatic = (access & Opcodes.ACC_STATIC) != 0;
        this.beginOutermostHandler = new Label();
        this.methodId = owner.getMethodId(name, desc);
    }

    @Override
    public void visitCodeAfterLoggerInitialization() {
        this.mv.visitCode();
        LoggingHelper.emitLogMethodEntry(this.mv, this.methodId);

        if (this.cv.cmdLineOptions.traceMethodArgs) {
            // emit code to trace each arg
            int _position = 0;
            if (!this.isStatic)
                _position += LoggingHelper.emitLogArgument(this.mv, _position, Type.getType(Object.class));

            for (final Type _argType : this.method.getArgumentTypes()) {
                _position += LoggingHelper.emitLogArgument(this.mv, _position, _argType);
            }
        }

        this.mv.visitLabel(this.beginOutermostHandler);
    }

    @Override
    public void visitInsn(final int opcode) {
        if (this.cv.cmdLineOptions.traceMethodRetValue) {
            if (opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes .FRETURN ||
            opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN ) {
                LoggingHelper.emitLogReturn(this.mv, method.getReturnType());
                LoggingHelper.emitLogMethodExit(this.mv, this.methodId, LoggingHelper.ExitKind.NORMAL);
                this.mv.visitInsn(opcode);
            }
        } else if (this.cv.cmdLineOptions.traceArrayAccess) {
            if (opcode == Opcodes.AASTORE || opcode == Opcodes.BASTORE || opcode == Opcodes.CASTORE ||
                opcode == Opcodes.DASTORE || opcode == Opcodes.FASTORE || opcode == Opcodes.IASTORE ||
                opcode == Opcodes.LASTORE || opcode == Opcodes.SASTORE) {
                visitArrayStoreInsn(opcode);
            } else if (opcode == Opcodes.AALOAD || opcode == Opcodes.BALOAD || opcode == Opcodes.CALOAD ||
                       opcode == Opcodes.DALOAD || opcode == Opcodes.FALOAD || opcode == Opcodes.IALOAD ||
                       opcode == Opcodes.LALOAD || opcode == Opcodes.SALOAD) {
                visitArrayLoadInsn(opcode);
            }
        } else {
            this.mv.visitInsn(opcode);
        }
    }

    @Override
    public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
        if (this.cv.cmdLineOptions.traceFieldAccess) {
            final Type _fieldType = Type.getType(desc);
            final String _fieldId = this.cv.getFieldId(name, owner, desc);
            final boolean _isFieldStatic = opcode == Opcodes.GETSTATIC || opcode == Opcodes.PUTSTATIC;
            if (opcode == Opcodes.GETSTATIC || opcode == Opcodes.GETFIELD) {
                if (_isFieldStatic)
                    this.mv.visitInsn(Opcodes.ACONST_NULL);<commit_msg>Fixes #8 Every return statement is instrumented to log method_exit (independent of if traceMethodReturnValue option) Also, some of the buggy logic in visitInsn was fixed.<commit_after>/*
 * Copyright (c) 2016, <NAME>
 *
 * BSD 3-clause License
 *
 * Author: <NAME> (rvprasad)
 */

package dyco4j.instrumentation.internals;

import dyco4j.LoggingHelper;
import dyco4j.instrumentation.LoggerInitializingMethodVisitor;
import dyco4j.instrumentation.logging.Logger.Action;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.Method;

final class TracingMethodVisitor extends LoggerInitializingMethodVisitor<TracingClassVisitor> {
    private final String methodId;
    private final Method method;
    private final boolean isStatic;
    private Label beginOutermostHandler;

    TracingMethodVisitor(final int access, final String name, final String desc, final String signature,
                         final String[] exceptions, final MethodVisitor mv, final TracingClassVisitor owner) {
        super(CLI.ASM_VERSION, mv, name, owner);
        this.method = new Method(name, desc);
        this.isStatic = (access & Opcodes.ACC_STATIC) != 0;
        this.beginOutermostHandler = new Label();
        this.methodId = owner.getMethodId(name, desc);
    }

    @Override
    public void visitCodeAfterLoggerInitialization() {
        this.mv.visitCode();
        LoggingHelper.emitLogMethodEntry(this.mv, this.methodId);

        if (this.cv.cmdLineOptions.traceMethodArgs) {
            // emit code to trace each arg
            int _position = 0;
            if (!this.isStatic)
                _position += LoggingHelper.emitLogArgument(this.mv, _position, Type.getType(Object.class));

            for (final Type _argType : this.method.getArgumentTypes()) {
                _position += LoggingHelper.emitLogArgument(this.mv, _position, _argType);
            }
        }

        this.mv.visitLabel(this.beginOutermostHandler);
    }

    @Override
    public void visitInsn(final int opcode) {
        if (opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN ||
            opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN) {
            if (this.cv.cmdLineOptions.traceMethodRetValue)
                LoggingHelper.emitLogReturn(this.mv, method.getReturnType());
            LoggingHelper.emitLogMethodExit(this.mv, this.methodId, LoggingHelper.ExitKind.NORMAL);
            this.mv.visitInsn(opcode);
        } else if (opcode == Opcodes.AASTORE || opcode == Opcodes.BASTORE || opcode == Opcodes.CASTORE ||
                   opcode == Opcodes.DASTORE || opcode == Opcodes.FASTORE || opcode == Opcodes.IASTORE ||
                   opcode == Opcodes.LASTORE || opcode == Opcodes.SASTORE) {
            if (this.cv.cmdLineOptions.traceArrayAccess)
                visitArrayStoreInsn(opcode);
            else
                this.mv.visitInsn(opcode);
        } else if (opcode == Opcodes.AALOAD || opcode == Opcodes.BALOAD || opcode == Opcodes.CALOAD ||
                   opcode == Opcodes.DALOAD || opcode == Opcodes.FALOAD || opcode == Opcodes.IALOAD ||
                   opcode == Opcodes.LALOAD || opcode == Opcodes.SALOAD) {
            if (this.cv.cmdLineOptions.traceArrayAccess)
                visitArrayLoadInsn(opcode);
            else
                this.mv.visitInsn(opcode);
        } else
            this.mv.visitInsn(opcode);
    }

    @Override
    public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
        if (this.cv.cmdLineOptions.traceFieldAccess) {
            final Type _fieldType = Type.getType(desc);
            final String _fieldId = this.cv.getFieldId(name, owner, desc);
            final boolean _isFieldStatic = opcode == Opcodes.GETSTATIC || opcode == Opcodes.PUTSTATIC;
            if (opcode == Opcodes.GETSTATIC || opcode == Opcodes.GETFIELD) {
                if (_isFieldStatic)
                    this.mv.visitInsn(Opcodes.ACONST_NULL);
{'fraction_non_alphanumeric': 0.10343100425745054, 'fraction_numerical': 0.002880040070122715, 'mean_word_length': 2.635411925352754, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4944673', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2348, 'n_words': 496}