All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qitsoft.qimono.internal.modifiers.NonPublicMethodsModifier Maven / Gradle / Ivy

Go to download

The framework library base on Mockito used to stub methods with private visibility, final methods or static methods. Also this library could be used to assign/retrieve values from private fields and to substitute date and time in tests.

The newest version!
/*
 * Copyright (C) 2011 QitSoft Inc.
 *
 * This file is part of Qimono.
 *
 * Qimono is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package com.qitsoft.qimono.internal.modifiers;

import com.qitsoft.qimono.internal.ClassModifierUtils;
import com.qitsoft.qimono.internal.ClassModifierAdapter;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * Class Modifier used to make all non public method to be public.
 * 
 * @author Serghei Soloviov 
 */
public class NonPublicMethodsModifier extends ClassModifierAdapter {

    private String className;

    /**
     * {@inheritDoc }
     */
    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        className = name;
        super.visit(version, access, name, signature, superName, interfaces);
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        int newAccess;

        if ("".equals(name)
                || "".equals(name)
                || (access & Opcodes.ACC_STATIC) != 0) {
            newAccess = access;
        } else {
            newAccess = ClassModifierUtils.makePublicAccess(access);
        }

        MethodVisitor methodVisitor = super.visitMethod(newAccess, name, desc, signature, exceptions);

        return new MethodAdapter(methodVisitor) {

            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String desc) {
                if (opcode == Opcodes.INVOKESPECIAL
                        && !"".equals(name)
                        && !"".equals(name)
                        && className.equals(owner)) {
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, desc);
                } else {
                    super.visitMethodInsn(opcode, owner, name, desc);
                }
            }
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy