Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9d320158df166b8f9e171f57f8b0df32ea7c1374 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences and others
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Institute for Software - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.implementmethod;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.internal.ui.refactoring.utils.NameHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.PseudoNameGenerator;

/**
 * Manages and creates Method Parameter Infos.
 *
 * @author Lukas Felber
 *
 */
public class ParameterHandler {
	private boolean needsAditionalArgumentNames;
	private PseudoNameGenerator pseudoNameGenerator;
	private ArrayList<ParameterInfo> parameterInfos;
	private IASTSimpleDeclaration method;

	public ParameterHandler(IASTSimpleDeclaration method) {
		this.method = method;
		initArgumentNames();
	}

	public boolean needsAdditionalArgumentNames() {
		return needsAditionalArgumentNames;
	}

	public void initArgumentNames() {
		if (parameterInfos != null) {
			return;
		}
		needsAditionalArgumentNames = false;
		parameterInfos = new ArrayList<ParameterInfo>();
		for (IASTParameterDeclaration actParam : getParametersFromMethodNode()) {
			String actName = actParam.getDeclarator().getName().toString();
			boolean isChangable = false;
			if (actParam.getDeclSpecifier() instanceof IASTSimpleDeclSpecifier
					&& ((IASTSimpleDeclSpecifier) actParam.getDeclSpecifier())
							.getType() == IASTSimpleDeclSpecifier.t_void) {
				actName = ""; //$NON-NLS-1$
				isChangable = false;
			} else if (actName.length() == 0) {
				needsAditionalArgumentNames = true;
				isChangable = true;
				actName = findNameForParameter(NameHelper.getTypeName(actParam));
			}
			parameterInfos.add(new ParameterInfo(actParam, actName, isChangable));
		}
	}

	private String findNameForParameter(String typeName) {
		if (pseudoNameGenerator == null) {
			pseudoNameGenerator = new PseudoNameGenerator();

			for (IASTParameterDeclaration parameter : getParametersFromMethodNode()) {
				if (parameter.getDeclarator().getName().toString().length() != 0) {
					pseudoNameGenerator.addExistingName(parameter.getDeclarator().getName().toString());
				}
			}
		}
		return pseudoNameGenerator.generateNewName(typeName);
	}

	private IASTParameterDeclaration[] getParametersFromMethodNode() {
		if (method.getDeclarators().length < 1) {
			return null;
		}
		return ((ICPPASTFunctionDeclarator) method.getDeclarators()[0]).getParameters();
	}

	public Collection<ParameterInfo> getParameterInfos() {
		return parameterInfos;
	}
}

Back to the top