Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d4b4d297883aa2be502490b5f14bff42e5f667fb (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences and others
 * 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  
 *  
 * 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.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;

import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.ui.refactoring.CTextFileChange;

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 ImplementMethodRefactoring refactoring;
	
	public ParameterHandler(ImplementMethodRefactoring refactoring) {
		this.refactoring = refactoring;
	}
	
	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(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(refactoring.getMethodDeclaration().getDeclarators().length < 1) {
			return null;
		}
		return ((ICPPASTFunctionDeclarator) refactoring.getMethodDeclaration().getDeclarators()[0]).getParameters();
	}
	
	public String createFunctionDefinitionSignature() {
		try {
			CompositeChange compositeChange = (CompositeChange) refactoring.createChange(new NullProgressMonitor());
			InsertEdit insertEdit = getInsertEdit(compositeChange);
			return insertEdit.getText().trim();
		} catch (OperationCanceledException e) {
			return Messages.PreviewGenerationNotPossible;
		} catch (CoreException e) {
			return Messages.PreviewGenerationNotPossible;
		}
	}

	private InsertEdit getInsertEdit(CompositeChange compositeChange) {
		for(Change actChange : compositeChange.getChildren()) {
			if(actChange instanceof CompositeChange) {
				return getInsertEdit((CompositeChange) actChange);
			} else if (actChange instanceof CTextFileChange) {
				CTextFileChange textFileChange = (CTextFileChange) actChange;
				MultiTextEdit multiEdit = (MultiTextEdit) textFileChange.getEdit();
				if(multiEdit.getChildrenSize() == 0) {
					continue;
				}
				return (InsertEdit) multiEdit.getChildren()[0];
			}
		}
		return null;
	}

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

Back to the top