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

com.qitsoft.qimono.internal.modifiers.SystemTimeInterceptModifier 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.ClassModifierAdapter;
import com.qitsoft.qimono.internal.QimonoClassLoader;
import com.qitsoft.qimono.internal.substitutors.DateSubstitutor;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import static org.objectweb.asm.Opcodes.*;

/**
 * Modifies the classes to intercept usage of {@link java.lang.System#currentTimeMillis()}.
 * It intercepts that the value obtained by this method returns the time set by 
 * {@link com.qitsoft.qimono.internal.substitutors.DateSubstitutor}.
 * 
 * @author Serghei Soloviov 
 */
public class SystemTimeInterceptModifier extends ClassModifierAdapter {

    /**
     * Configures classloader to use this class as a modifier.
     * 
     * @param classloader the class loader to configure.
     */
    public static void configureClassloader(QimonoClassLoader classloader) {
        classloader.addClassModifier(new SystemTimeInterceptModifier());
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        return new MethodAdapter(super.visitMethod(access, name, descriptor, signature, exceptions)) {

            @Override
            public void visitMethodInsn(int opcode, String owner, String name, String descriptor) {
                if (opcode == INVOKESTATIC
                        && Type.getInternalName(System.class).equals(owner)
                        && "currentTimeMillis".equals(name)) {
                    
                    super.visitMethodInsn(INVOKESTATIC, Type.getInternalName(DateSubstitutor.class), name, descriptor);
                } else {
                    super.visitMethodInsn(opcode, owner, name, descriptor);
                }
            }
            
        };
    }
    
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy