Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb5778253468a96b70181d318e49d3573d35e813 (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
/*******************************************************************************
 * Copyright (c) 2009, 2013 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
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractfunction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.internal.ui.refactoring.NameInformation;
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer;

abstract class SimilarFinderVisitor extends ASTVisitor {
	protected final ExtractFunctionRefactoring refactoring;
	protected final NodeContainer extractedNodes;
	protected NodeContainer similarContainer;
	protected final List<IASTStatement> stmtToReplace = new ArrayList<IASTStatement>();
	private final List<IASTNode> trail;
	private final List<IASTNode> statements;
	private int statementCount;

	SimilarFinderVisitor(ExtractFunctionRefactoring refactoring, NodeContainer extractedNodes, List<IASTNode> trail,
			List<IASTNode> statements) {
		this.refactoring = refactoring;
		this.extractedNodes = extractedNodes;
		this.trail = trail;
		this.statements = statements;
		this.similarContainer = new NodeContainer();
		shouldVisitStatements = true;
	}

	@Override
	public int visit(IASTStatement statement) {
		if (!isInSelection(statement) && refactoring.isStatementInTrail(statement, trail)) {
			stmtToReplace.add(statement);
			similarContainer.add(statement);
			++statementCount;

			if (statementCount == statements.size()) {
				// Found similar code
				boolean similarOnReturnWays = true;
				for (NameInformation nameInfo : similarContainer.getParameterCandidates()) {
					if (refactoring.names.containsKey(nameInfo.getDeclarationName().getRawSignature())) {
						Integer nameOrderNumber = refactoring.names
								.get(nameInfo.getDeclarationName().getRawSignature());
						if (refactoring.nameTrail.containsValue(nameOrderNumber)) {
							String orgName = null;
							boolean found = false;
							for (Entry<String, Integer> entry : refactoring.nameTrail.entrySet()) {
								if (entry.getValue().equals(nameOrderNumber)) {
									orgName = entry.getKey();
									break;
								}
							}
							if (orgName != null) {
								for (NameInformation orgNameInfo : extractedNodes.getParameterCandidates()) {
									if (orgName.equals(orgNameInfo.getDeclarationName().getRawSignature())
											&& (orgNameInfo.isOutput() || !nameInfo.isOutput())) {
										found = true;
										break;
									}
								}
							}

							if (!found) {
								similarOnReturnWays = false;
							}
						}
					}
				}

				if (similarOnReturnWays) {
					foundSimilar();
				}
				clear();
			}
			return PROCESS_SKIP;
		} else {
			clear();
			return super.visit(statement);
		}
	}

	protected abstract void foundSimilar();

	private boolean isInSelection(IASTStatement stmt) {
		List<IASTNode> nodes = extractedNodes.getNodesToWrite();
		for (IASTNode node : nodes) {
			if (node.equals(stmt)) {
				return true;
			}
		}
		return false;
	}

	private void clear() {
		statementCount = 0;
		refactoring.names.clear();
		similarContainer = new NodeContainer();
		refactoring.namesCounter.setObject(ExtractFunctionRefactoring.NULL_INTEGER);
		refactoring.trailPos.setObject(ExtractFunctionRefactoring.NULL_INTEGER);
		stmtToReplace.clear();
	}
}

Back to the top