1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Use interfaces instead of concrete classes in method signatures.

This commit is contained in:
Sergey Prigogin 2012-01-09 18:08:57 -08:00
parent 9b83d66132
commit 6985289cff
9 changed files with 71 additions and 71 deletions

View file

@ -35,7 +35,6 @@ public final class Messages extends NLS {
public static String Refactoring_CantLoadTU; public static String Refactoring_CantLoadTU;
public static String Refactoring_Ambiguity; public static String Refactoring_Ambiguity;
public static String Refactoring_ParsingError; public static String Refactoring_ParsingError;
public static String NodeContainer_Name;
public static String NO_FILE; public static String NO_FILE;
public static String RefactoringSaveHelper_unexpected_exception; public static String RefactoringSaveHelper_unexpected_exception;
public static String RefactoringSaveHelper_saving; public static String RefactoringSaveHelper_saving;

View file

@ -31,7 +31,6 @@ Refactoring_CantLoadTU=Can not load translation unit.
Refactoring_Ambiguity=Translation unit is ambiguous. Refactoring_Ambiguity=Translation unit is ambiguous.
Refactoring_ParsingError=Unable to parse {0}. Refactoring_ParsingError=Unable to parse {0}.
NO_FILE=File not found. NO_FILE=File not found.
NodeContainer_Name=name:
RefactoringSaveHelper_unexpected_exception=An unexpected exception occurred. See the error log for more details. RefactoringSaveHelper_unexpected_exception=An unexpected exception occurred. See the error log for more details.
RefactoringSaveHelper_saving=Saving Resources RefactoringSaveHelper_saving=Saving Resources
RefactoringSaveHelper_always_save=&Always save all modified resources automatically prior to refactoring RefactoringSaveHelper_always_save=&Always save all modified resources automatically prior to refactoring

View file

@ -50,14 +50,14 @@ import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter;
public class NodeContainer { public class NodeContainer {
public final NameInformation NULL_NAME_INFORMATION = new NameInformation(new CPPASTName()); public final NameInformation NULL_NAME_INFORMATION = new NameInformation(new CPPASTName());
private final ArrayList<IASTNode> vec; private final List<IASTNode> nodes;
private final ArrayList<NameInformation> names; private final List<NameInformation> names;
public class NameInformation { public class NameInformation {
private IASTName name; private IASTName name;
private IASTName declaration; private IASTName declaration;
private final ArrayList<IASTName> references; private final List<IASTName> references;
private ArrayList<IASTName> referencesAfterCached; private List<IASTName> referencesAfterCached;
private int lastCachedReferencesHash; private int lastCachedReferencesHash;
private boolean isReference; private boolean isReference;
private boolean isReturnValue; private boolean isReturnValue;
@ -103,9 +103,8 @@ public class NodeContainer {
references.add(name); references.add(name);
} }
public ArrayList<IASTName> getReferencesAfterSelection() { public List<IASTName> getReferencesAfterSelection() {
if (referencesAfterCached == null if (referencesAfterCached == null || lastCachedReferencesHash != references.hashCode()) {
|| lastCachedReferencesHash != references.hashCode()) {
lastCachedReferencesHash = references.hashCode(); lastCachedReferencesHash = references.hashCode();
referencesAfterCached = new ArrayList<IASTName>(); referencesAfterCached = new ArrayList<IASTName>();
for (IASTName ref : references) { for (IASTName ref : references) {
@ -196,18 +195,17 @@ public class NodeContainer {
return writer.write(declSpec); return writer.write(declSpec);
} }
public boolean isDeclarationInScope() { public boolean isDeclarationExtracted() {
if (declaration != null && declaration.toCharArray().length > 0) { if (declaration != null && declaration.toCharArray().length > 0) {
int declOffset = declaration.getFileLocation().getNodeOffset(); int declOffset = declaration.getFileLocation().getNodeOffset();
return declOffset >= getStartOffset() return declOffset >= getStartOffset() && declOffset <= getEndOffset();
&& declOffset <= getEndOffset();
} }
return true; return true;
} }
@Override @Override
public String toString() { public String toString() {
return Messages.NodeContainer_Name + name + ' ' + isDeclarationInScope(); return name.toString() + ": " + (isDeclarationExtracted() ? "with declaration" : "without declaration"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
} }
public boolean isReference() { public boolean isReference() {
@ -269,24 +267,24 @@ public class NodeContainer {
public NodeContainer() { public NodeContainer() {
super(); super();
vec = new ArrayList<IASTNode>(); nodes = new ArrayList<IASTNode>();
names = new ArrayList<NameInformation>(); names = new ArrayList<NameInformation>();
} }
public final int size() { public final int size() {
return vec.size(); return nodes.size();
} }
public final boolean isEmpty() { public final boolean isEmpty() {
return vec.isEmpty(); return nodes.isEmpty();
} }
public void add(IASTNode node) { public void add(IASTNode node) {
vec.add(node); nodes.add(node);
} }
public void findAllNames() { public void findAllNames() {
for (IASTNode node : vec) { for (IASTNode node : nodes) {
node.accept(new ASTVisitor() { node.accept(new ASTVisitor() {
{ {
shouldVisitNames = true; shouldVisitNames = true;
@ -343,7 +341,7 @@ public class NodeContainer {
* Returns all local names in the selection which will be used after the * Returns all local names in the selection which will be used after the
* selection expected the ones which are pointers * selection expected the ones which are pointers
*/ */
public ArrayList<NameInformation> getAllAfterUsedNames() { public List<NameInformation> getAllAfterUsedNames() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); ArrayList<IASTName> declarations = new ArrayList<IASTName>();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>(); ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
@ -364,13 +362,12 @@ public class NodeContainer {
return usedAfter; return usedAfter;
} }
public ArrayList<NameInformation> getAllAfterUsedNamesChoosenByUser() { public List<NameInformation> getAllAfterUsedNamesChoosenByUser() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); ArrayList<IASTName> declarations = new ArrayList<IASTName>();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>(); ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
for (NameInformation nameInf : names) { for (NameInformation nameInf : names) {
if (!declarations.contains(nameInf.getDeclaration())) { if (!declarations.contains(nameInf.getDeclaration())) {
declarations.add(nameInf.getDeclaration()); declarations.add(nameInf.getDeclaration());
if (nameInf.isUserSetIsReference() || nameInf.isUserSetIsReturnValue()) { if (nameInf.isUserSetIsReference() || nameInf.isUserSetIsReturnValue()) {
usedAfter.add(nameInf); usedAfter.add(nameInf);
@ -381,7 +378,7 @@ public class NodeContainer {
return usedAfter; return usedAfter;
} }
public ArrayList<NameInformation> getUsedNamesUnique() { public List<NameInformation> getUsedNamesUnique() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); ArrayList<IASTName> declarations = new ArrayList<IASTName>();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>(); ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
@ -411,18 +408,19 @@ public class NodeContainer {
* selection expected the ones which are pointers * selection expected the ones which are pointers
* XXX Was soll dieser Kommentar aussagen? --Mirko * XXX Was soll dieser Kommentar aussagen? --Mirko
*/ */
public ArrayList<NameInformation> getAllDeclaredInScope() { public List<NameInformation> getAllDeclaredInScope() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); ArrayList<IASTName> declarations = new ArrayList<IASTName>();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>(); ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
for (NameInformation nameInf : names) { for (NameInformation nameInfo : names) {
if (nameInf.isDeclarationInScope() if (nameInfo.isDeclarationExtracted() &&
&& !declarations.contains(nameInf.getDeclaration()) && nameInf.isUsedAfterReferences()) { !declarations.contains(nameInfo.getDeclaration()) &&
declarations.add(nameInf.getDeclaration()); nameInfo.isUsedAfterReferences()) {
usedAfter.add(nameInf); declarations.add(nameInfo.getDeclaration());
// is return value candidate, set return value to true and reference to false usedAfter.add(nameInfo);
nameInf.setReturnValue(true); // Is return value candidate, set return value to true and reference to false
nameInf.setReference(false); nameInfo.setReturnValue(true);
nameInfo.setReference(false);
} }
} }
@ -430,7 +428,7 @@ public class NodeContainer {
} }
public List<IASTNode> getNodesToWrite() { public List<IASTNode> getNodesToWrite() {
return vec; return nodes;
} }
public int getStartOffset() { public int getStartOffset() {
@ -444,7 +442,7 @@ public class NodeContainer {
private int getOffset(boolean includeComments) { private int getOffset(boolean includeComments) {
int start = Integer.MAX_VALUE; int start = Integer.MAX_VALUE;
for (IASTNode node : vec) { for (IASTNode node : nodes) {
int nodeStart = Integer.MAX_VALUE; int nodeStart = Integer.MAX_VALUE;
IASTNodeLocation[] nodeLocations = node.getNodeLocations(); IASTNodeLocation[] nodeLocations = node.getNodeLocations();
@ -483,7 +481,7 @@ public class NodeContainer {
private int getEndOffset(boolean includeComments) { private int getEndOffset(boolean includeComments) {
int end = 0; int end = 0;
for (IASTNode node : vec) { for (IASTNode node : nodes) {
int fileOffset = 0; int fileOffset = 0;
int length = 0; int length = 0;
@ -514,7 +512,7 @@ public class NodeContainer {
@Override @Override
public String toString() { public String toString() {
return vec.toString(); return nodes.toString();
} }
public List<NameInformation> getNames() { public List<NameInformation> getNames() {

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.internal.ui.refactoring.extractfunction; package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.custom.TableEditor;
@ -38,13 +39,13 @@ public class ChooserComposite extends Composite {
private Button voidReturn; private Button voidReturn;
private final ExtractFunctionInputPage ip; private final ExtractFunctionInputPage page;
public ChooserComposite(Composite parent, final ExtractFunctionInformation info, public ChooserComposite(Composite parent, final ExtractFunctionInformation info,
ExtractFunctionInputPage ip) { ExtractFunctionInputPage page) {
super(parent, SWT.NONE); super(parent, SWT.NONE);
this.ip = ip; this.page = page;
GridLayout layout = new GridLayout(); GridLayout layout = new GridLayout();
setLayout(layout); setLayout(layout);
@ -75,7 +76,7 @@ public class ChooserComposite extends Composite {
addColumnToTable(table, ""); //$NON-NLS-1$ addColumnToTable(table, ""); //$NON-NLS-1$
for (int i = 0; i < info.getAllUsedNames().size(); i++) { for (int i = 0; i < info.getAllUsedNames().size(); i++) {
if (!info.getAllUsedNames().get(i).isDeclarationInScope()) { if (!info.getAllUsedNames().get(i).isDeclarationExtracted()) {
TableItem item = new TableItem(table, SWT.NONE); TableItem item = new TableItem(table, SWT.NONE);
TableEditor editor = new TableEditor(table); TableEditor editor = new TableEditor(table);
@ -212,15 +213,15 @@ public class ChooserComposite extends Composite {
column.setWidth(100); column.setWidth(100);
} }
void onVisibilityOrReturnChange(ArrayList<NameInformation> name) { void onVisibilityOrReturnChange(List<NameInformation> name) {
String variableUsedAfterBlock = null; String variableUsedAfterBlock = null;
for (NameInformation information : name) { for (NameInformation information : name) {
if (information.isUsedAfterReferences() if (information.isUsedAfterReferences() &&
&& !(information.isUserSetIsReference() || information.isUserSetIsReturnValue())) { !(information.isUserSetIsReference() || information.isUserSetIsReturnValue())) {
variableUsedAfterBlock = information.getName().toString(); variableUsedAfterBlock = information.getName().toString();
} }
} }
ip.errorWithAfterUsedVariable(variableUsedAfterBlock); page.errorWithAfterUsedVariable(variableUsedAfterBlock);
} }
} }

View file

@ -53,7 +53,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
/** /**
* Handles the extraction of expression nodes, like return type determination. * Handles the extraction of expression nodes, for example, return type determination.
* *
* @author Mirko Stocker * @author Mirko Stocker
*/ */
@ -64,7 +64,8 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
public void constructMethodBody(IASTCompoundStatement compound, List<IASTNode> list, public void constructMethodBody(IASTCompoundStatement compound, List<IASTNode> list,
ASTRewrite rewrite, TextEditGroup group) { ASTRewrite rewrite, TextEditGroup group) {
CPPASTReturnStatement statement = new CPPASTReturnStatement(); CPPASTReturnStatement statement = new CPPASTReturnStatement();
IASTExpression nullReturnExp = new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, ZERO); IASTExpression nullReturnExp =
new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, ZERO);
statement.setReturnValue(nullReturnExp); statement.setReturnValue(nullReturnExp);
ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group); ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group);
@ -154,7 +155,7 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
} }
@Override @Override
protected boolean isReturnTypeAPointer(IASTNode node) { protected boolean hasPointerReturnType(IASTNode node) {
if (node instanceof ICPPASTNewExpression) { if (node instanceof ICPPASTNewExpression) {
return true; return true;
} else if (!(node instanceof IASTFunctionCallExpression)) { } else if (!(node instanceof IASTFunctionCallExpression)) {
@ -186,11 +187,13 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
} }
private static boolean hasDeclaration(CPPFunction function) { private static boolean hasDeclaration(CPPFunction function) {
return function != null && function.getDeclarations() != null && function.getDeclarations().length > 0; return function != null && function.getDeclarations() != null &&
function.getDeclarations().length > 0;
} }
@Override @Override
public IASTNode createReturnAssignment(IASTNode node, IASTExpressionStatement stmt, IASTExpression callExpression) { public IASTNode createReturnAssignment(IASTNode node, IASTExpressionStatement stmt,
IASTExpression callExpression) {
return callExpression; return callExpression;
} }
} }

View file

@ -12,6 +12,7 @@
package org.eclipse.cdt.internal.ui.refactoring.extractfunction; package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
@ -20,15 +21,11 @@ import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum;
public class ExtractFunctionInformation { public class ExtractFunctionInformation {
public final int VISIBILITY_PRIVATE = 1;
public final int VISIBILITY_PROTECTED = 3;
public final int VISIBILITY_PUBLIC = 2;
private VisibilityEnum visibility = VisibilityEnum.v_private; private VisibilityEnum visibility = VisibilityEnum.v_private;
private String methodName; private String methodName;
private boolean replaceDuplicates; private boolean replaceDuplicates;
private ArrayList<NameInformation> allAfterUsedNames; private List<NameInformation> allAfterUsedNames;
private ArrayList<NameInformation> allUsedNames; private List<NameInformation> allUsedNames;
private NameInformation inScopeDeclaredVariable; private NameInformation inScopeDeclaredVariable;
private NameInformation returnVariable; private NameInformation returnVariable;
private ICPPASTFunctionDeclarator declarator; private ICPPASTFunctionDeclarator declarator;
@ -65,7 +62,7 @@ public class ExtractFunctionInformation {
this.replaceDuplicates = replaceDuplicates; this.replaceDuplicates = replaceDuplicates;
} }
public ArrayList<NameInformation> getAllAfterUsedNames() { public List<NameInformation> getAllAfterUsedNames() {
if (allAfterUsedNames == null) { if (allAfterUsedNames == null) {
allAfterUsedNames = new ArrayList<NameInformation>(); allAfterUsedNames = new ArrayList<NameInformation>();
for (NameInformation name : getAllUsedNames()) { for (NameInformation name : getAllUsedNames()) {
@ -101,11 +98,11 @@ public class ExtractFunctionInformation {
this.inScopeDeclaredVariable = inScopeDeclaredVariable; this.inScopeDeclaredVariable = inScopeDeclaredVariable;
} }
public ArrayList<NameInformation> getAllUsedNames() { public List<NameInformation> getAllUsedNames() {
return allUsedNames; return allUsedNames;
} }
public void setAllUsedNames(ArrayList<NameInformation> allUsedNames) { public void setAllUsedNames(List<NameInformation> allUsedNames) {
this.allUsedNames = allUsedNames; this.allUsedNames = allUsedNames;
} }

View file

@ -201,7 +201,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
info.getInScopeDeclaredVariable().setUserSetIsReturnValue(true); info.getInScopeDeclaredVariable().setUserSetIsReturnValue(true);
} }
for (int i = 0; i < info.getAllUsedNames().size(); i++) { for (int i = 0; i < info.getAllUsedNames().size(); i++) {
if (!info.getAllUsedNames().get(i).isDeclarationInScope()) { if (!info.getAllUsedNames().get(i).isDeclarationExtracted()) {
NameInformation name = info.getAllUsedNames().get(i); NameInformation name = info.getAllUsedNames().get(i);
if (!name.isReturnValue()) { if (!name.isReturnValue()) {
name.setUserSetIsReference(name.isReference()); name.setUserSetIsReference(name.isReference());
@ -856,7 +856,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
private void addParameterIfPossible(List<IASTInitializerClause> args, private void addParameterIfPossible(List<IASTInitializerClause> args,
List<IASTName> declarations, NameInformation nameInfо) { List<IASTName> declarations, NameInformation nameInfо) {
if (!nameInfо.isDeclarationInScope()) { if (!nameInfо.isDeclarationExtracted()) {
IASTName declaration = nameInfо.getDeclaration(); IASTName declaration = nameInfо.getDeclaration();
if (!declarations.contains(declaration)) { if (!declarations.contains(declaration)) {
declarations.add(declaration); declarations.add(declaration);

View file

@ -36,13 +36,14 @@ public class ExtractStatement extends ExtractedFunctionConstructionHelper {
@Override @Override
public void constructMethodBody(IASTCompoundStatement compound, List<IASTNode> list, public void constructMethodBody(IASTCompoundStatement compound, List<IASTNode> list,
ASTRewrite rewrite, TextEditGroup group) { ASTRewrite rewrite, TextEditGroup group) {
for (IASTNode each : list) { for (IASTNode node : list) {
rewrite.insertBefore(compound, null, each, group); rewrite.insertBefore(compound, null, node, group);
} }
} }
@Override @Override
public IASTDeclSpecifier determineReturnType(IASTNode extractedNode, NameInformation returnVariable) { public IASTDeclSpecifier determineReturnType(IASTNode extractedNode,
NameInformation returnVariable) {
if (returnVariable != null) { if (returnVariable != null) {
IASTNode decl = ASTHelper.getDeclarationForNode(returnVariable.getDeclaration()); IASTNode decl = ASTHelper.getDeclarationForNode(returnVariable.getDeclaration());
return ASTHelper.getDeclarationSpecifier(decl).copy(CopyStyle.withLocations); return ASTHelper.getDeclarationSpecifier(decl).copy(CopyStyle.withLocations);

View file

@ -55,13 +55,14 @@ public abstract class ExtractedFunctionConstructionHelper {
public abstract IASTNode createReturnAssignment(IASTNode node, IASTExpressionStatement stmt, public abstract IASTNode createReturnAssignment(IASTNode node, IASTExpressionStatement stmt,
IASTExpression callExpression); IASTExpression callExpression);
protected boolean isReturnTypeAPointer(IASTNode node) { protected boolean hasPointerReturnType(IASTNode node) {
return false; return false;
} }
IASTStandardFunctionDeclarator createFunctionDeclarator(IASTName name, IASTStandardFunctionDeclarator createFunctionDeclarator(IASTName name,
IASTStandardFunctionDeclarator functionDeclarator, NameInformation returnVariable, IASTStandardFunctionDeclarator functionDeclarator, NameInformation returnVariable,
List<IASTNode> nodesToWrite, Collection<NameInformation> allUsedNames, INodeFactory nodeFactory) { List<IASTNode> nodesToWrite, Collection<NameInformation> allUsedNames,
INodeFactory nodeFactory) {
IASTStandardFunctionDeclarator declarator = nodeFactory.newFunctionDeclarator(name); IASTStandardFunctionDeclarator declarator = nodeFactory.newFunctionDeclarator(name);
if (functionDeclarator instanceof ICPPASTFunctionDeclarator && if (functionDeclarator instanceof ICPPASTFunctionDeclarator &&
@ -83,17 +84,18 @@ public abstract class ExtractedFunctionConstructionHelper {
declarator.addParameterDeclaration(param); declarator.addParameterDeclaration(param);
} }
if (isReturnTypeAPointer(nodesToWrite.get(0))) { if (hasPointerReturnType(nodesToWrite.get(0))) {
declarator.addPointerOperator(nodeFactory.newPointer()); declarator.addPointerOperator(nodeFactory.newPointer());
} }
return declarator; return declarator;
} }
public Collection<IASTParameterDeclaration> getParameterDeclarations(Collection<NameInformation> allUsedNames, INodeFactory nodeFactory) { public List<IASTParameterDeclaration> getParameterDeclarations(
Collection<IASTParameterDeclaration> result = new ArrayList<IASTParameterDeclaration>(); Collection<NameInformation> allUsedNames, INodeFactory nodeFactory) {
List<IASTParameterDeclaration> result = new ArrayList<IASTParameterDeclaration>();
for (NameInformation name : allUsedNames) { for (NameInformation name : allUsedNames) {
if (!name.isDeclarationInScope()) { if (!name.isDeclarationExtracted()) {
result.add(name.getParameterDeclaration(name.isUserSetIsReference(), nodeFactory)); result.add(name.getParameterDeclaration(name.isUserSetIsReference(), nodeFactory));
} }
} }