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

com.ecfeed.core.operations.MethodParameterShiftOperation Maven / Gradle / Ivy

/*******************************************************************************
 *
 * Copyright (c) 2016 ecFeed AS.                                                
 * All rights reserved. This program and the accompanying materials              
 * are made available under the terms of the Eclipse Public License v1.0         
 * which accompanies this distribution, and is available at                      
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 *******************************************************************************/

package com.ecfeed.core.operations;

import java.util.Arrays;
import java.util.List;

import com.ecfeed.core.model.AbstractNode;
import com.ecfeed.core.model.AbstractParameterNode;
import com.ecfeed.core.model.ClassNodeHelper;
import com.ecfeed.core.model.MethodNode;
import com.ecfeed.core.model.MethodParameterNode;
import com.ecfeed.core.model.ModelOperationException;
import com.ecfeed.core.model.TestCaseNode;

public class MethodParameterShiftOperation extends GenericShiftOperation {

	private List fParameters;

	public MethodParameterShiftOperation(List parameters, AbstractNode shifted, boolean up) {
		this(parameters, Arrays.asList(new AbstractNode[]{shifted}), up);
	}

	public MethodParameterShiftOperation(List parameters, List shifted, boolean up) {
		this(parameters, shifted, 0);
		setShift(minAllowedShift(shifted, up));
	}

	public MethodParameterShiftOperation(List parameters, List shifted, int shift) {
		super(parameters, shifted, shift);
		fParameters = parameters;
	}

	@Override
	public void execute() throws ModelOperationException {
		MethodNode method = ((MethodParameterNode)fParameters.get(0)).getMethod();
		if(shiftAllowed(getShiftedElements(), getShift()) == false){
			ModelOperationException.report(ClassNodeHelper.generateMethodSignatureDuplicateMessage(method.getClassNode(), method.getFullName()));
		}
		List indices = indices(fParameters, getShiftedElements());
		shiftElements(fParameters, indices, getShift());
		for(TestCaseNode testCase : method.getTestCases()){
			shiftElements(testCase.getTestData(), indices, getShift());
		}
	}

	@Override
	public IModelOperation getReverseOperation(){
		return new MethodParameterShiftOperation(fParameters, getShiftedElements(), -getShift());
	}

	@Override
	protected boolean shiftAllowed(List shifted, int shift){
		if(super.shiftAllowed(shifted, shift) == false) return false;
		if(shifted.get(0) instanceof MethodParameterNode == false) return false;
		MethodNode method = ((MethodParameterNode)shifted.get(0)).getMethod();
		List parameterTypes = method.getParameterTypes();
		List indices = indices(method.getParameters(), shifted);
		shiftElements(parameterTypes, indices, shift);
		MethodNode sibling = method.getClassNode().getMethod(method.getFullName(), parameterTypes);
		if(sibling != null && sibling != method){
			return false;
		}
		return true;
	}

	@Override
	protected int minAllowedShift(List shifted, boolean up){
		int shift = up ? -1 : 1;
		while(shiftAllowed(shifted, shift) == false){
			shift += up ? -1 : 1;
			int borderIndex = (borderNode(shifted, shift) != null) ? borderNode(shifted, shift).getMyIndex() + shift : -1;
			if(borderIndex < 0 || borderIndex >= borderNode(shifted, shift).getMaxIndex()){
				return 0;
			}
		}
		return shift;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy