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

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

Go to download

An open library used to connect to the ecFeed service. It can be also used as a standalone testing tool. It is integrated with Junit5 and generates a stream of test cases using a selected algorithm (e.g. Cartesian, N-Wise). There are no limitations associated with the off-line version but the user cannot access the on-line computation servers and the model database.

The newest version!
/*******************************************************************************
 *
 * 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.ArrayList;
import java.util.List;

import com.ecfeed.core.model.ClassNode;
import com.ecfeed.core.model.ClassNodeHelper;
import com.ecfeed.core.model.MethodNode;
import com.ecfeed.core.model.ModelOperationException;
import com.ecfeed.core.utils.StringHelper;

public class ClassOperationAddMethod extends AbstractModelOperation{

	private ClassNode fClassNode;
	private MethodNode fMethod;
	private int fIndex;
	
	private static final String UNEXPECTED_PROBLEM_WHILE_ADDING_ELEMENT = "Element could not be added to the model";

	public ClassOperationAddMethod(ClassNode target, MethodNode method, int index) {
		super(OperationNames.ADD_METHOD);
		fClassNode = target;
		fMethod = method;
		fIndex = index;
	}

	public ClassOperationAddMethod(ClassNode target, MethodNode method) {
		this(target, method, -1);
	}

	@Override
	public void execute() throws ModelOperationException {

		setOneNodeToSelect(fClassNode);
		List problems = new ArrayList();

		if(fIndex == -1){
			fIndex = fClassNode.getMethods().size();
		}

		generateUniqeMethodName(fMethod);

		if(ClassNodeHelper.isNewMethodSignatureValid(fClassNode, fMethod.getFullName(), fMethod.getParameterTypes(), problems) == false){
			ClassNodeHelper.updateNewMethodsSignatureProblemList(fClassNode, fMethod.getFullName(), fMethod.getParameterTypes(), problems);
			ModelOperationException.report(StringHelper.convertToMultilineString(problems));
		}

		if(fClassNode.addMethod(fMethod, fIndex) == false){
			ModelOperationException.report(UNEXPECTED_PROBLEM_WHILE_ADDING_ELEMENT);
		}

		markModelUpdated();
	}

	private void generateUniqeMethodName(MethodNode methodNode) {
		String oldName = methodNode.getFullName();
		String oldNameCore = StringHelper.removeFromNumericPostfix(oldName);
		String newName = ClassNodeHelper.generateNewMethodName(fClassNode, oldNameCore, methodNode.getParameterTypes());
		methodNode.setFullName(newName);
	}

	@Override
	public IModelOperation getReverseOperation() {
		return new ClassOperationRemoveMethod(fClassNode, fMethod);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy