diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java index cd582f53974..c4555e77a57 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java @@ -7,7 +7,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Institute for Software - initial API and implementation + * Institute for Software - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.extractfunction; @@ -56,59 +56,54 @@ import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; * Handles the extraction of expression nodes, like return type determination. * * @author Mirko Stocker - * */ public class ExtractExpression extends ExtractedFunctionConstructionHelper { - - final static char[] ZERO= {'0'}; + final static char[] ZERO= { '0' }; @Override - public void constructMethodBody(IASTCompoundStatement compound, - List list, ASTRewrite rewrite, TextEditGroup group) { - + public void constructMethodBody(IASTCompoundStatement compound, List list, + ASTRewrite rewrite, TextEditGroup group) { CPPASTReturnStatement statement = new CPPASTReturnStatement(); IASTExpression nullReturnExp = new CPPASTLiteralExpression(IASTLiteralExpression.lk_integer_constant, ZERO); statement.setReturnValue(nullReturnExp); ASTRewrite nestedRewrite = rewrite.insertBefore(compound, null, statement, group); nestedRewrite.replace(nullReturnExp, getExpression(list), group); - } private IASTExpression getExpression(List list) { - if(list.size()> 1 ) { + if (list.size()> 1) { CPPASTBinaryExpression bExp = new CPPASTBinaryExpression(); bExp.setParent(list.get(0).getParent()); bExp.setOperand1((IASTExpression) list.get(0).copy(CopyStyle.withLocations)); bExp.setOperator(((IASTBinaryExpression)list.get(1).getParent()).getOperator()); bExp.setOperand2(getExpression(list.subList(1, list.size()))); return bExp; - }else { + } else { return (IASTExpression) list.get(0).copy(CopyStyle.withLocations); } - } @Override public IASTDeclSpecifier determineReturnType(IASTNode extractedNode, NameInformation _) { - List typdefs = getTypdefs(extractedNode); + List typedefs = getTypedefs(extractedNode); if (extractedNode instanceof IASTExpression) { IASTExpression exp = (IASTExpression) extractedNode; INodeFactory factory = extractedNode.getTranslationUnit().getASTNodeFactory(); DeclarationGenerator generator = DeclarationGenerator.create(factory); IType expressionType = exp.getExpressionType(); - for (ITypedef typedef : typdefs) { + for (ITypedef typedef : typedefs) { if (typedef.getType().isSameType(expressionType)) { return generator.createDeclSpecFromType(typedef); } } return generator.createDeclSpecFromType(expressionType); - } else {// Fallback + } else { // Fallback return createSimpleDeclSpecifier(Kind.eVoid); } } - private List getTypdefs(IASTNode extractedNode) { + private List getTypedefs(IASTNode extractedNode) { final ArrayList typeDefs = new ArrayList(); extractedNode.accept(new ASTVisitor() { { @@ -148,10 +143,10 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper { IASTExpression functionNameExpression = callExpression.getFunctionNameExpression(); IASTName functionName = null; - if(functionNameExpression instanceof CPPASTIdExpression) { + if (functionNameExpression instanceof CPPASTIdExpression) { CPPASTIdExpression idExpression = (CPPASTIdExpression) functionNameExpression; functionName = idExpression.getName(); - } else if(functionNameExpression instanceof CPPASTFieldReference) { + } else if (functionNameExpression instanceof CPPASTFieldReference) { CPPASTFieldReference fieldReference = (CPPASTFieldReference) functionNameExpression; functionName = fieldReference.getFieldName(); } @@ -160,28 +155,29 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper { @Override protected boolean isReturnTypeAPointer(IASTNode node) { - if(node instanceof ICPPASTNewExpression) { + if (node instanceof ICPPASTNewExpression) { return true; - } else if(!(node instanceof IASTFunctionCallExpression)) { + } else if (!(node instanceof IASTFunctionCallExpression)) { return false; } IASTName functionName = findCalledFunctionName((IASTFunctionCallExpression) node); - if(functionName != null) { + if (functionName != null) { IBinding binding = functionName.resolveBinding(); if (binding instanceof CPPFunction) { CPPFunction function = (CPPFunction) binding; - if(function.getDefinition() != null) { + if (function.getDefinition() != null) { IASTNode parent = function.getDefinition().getParent(); - if(parent instanceof CPPASTFunctionDefinition) { + if (parent instanceof CPPASTFunctionDefinition) { CPPASTFunctionDefinition definition = (CPPASTFunctionDefinition) parent; return definition.getDeclarator().getPointerOperators().length > 0; } - } else if(hasDeclaration(function)) { + } else if (hasDeclaration(function)) { IASTNode parent = function.getDeclarations()[0].getParent(); if (parent instanceof CPPASTSimpleDeclaration) { CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent; - return declaration.getDeclarators().length > 0 && declaration.getDeclarators()[0].getPointerOperators().length > 0; + return declaration.getDeclarators().length > 0 && + declaration.getDeclarators()[0].getPointerOperators().length > 0; } } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/SimilarFinderVisitor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/SimilarFinderVisitor.java index 3d6d179c76e..9f254c04f1d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/SimilarFinderVisitor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/SimilarFinderVisitor.java @@ -7,7 +7,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Institute for Software - initial API and implementation + * Institute for Software - initial API and implementation *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.extractfunction; @@ -30,14 +30,13 @@ import org.eclipse.cdt.internal.ui.refactoring.NodeContainer; import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; final class SimilarFinderVisitor extends ASTVisitor { + private final ExtractFunctionRefactoring refactoring; - private final ExtractFunctionRefactoring refactoring; - - private final Vector trail; - private final IASTName name; - private final List stmts; - private int i = 0; - private NodeContainer similarContainer; + private final Vector trail; + private final IASTName name; + private final List stmts; + private int i; + private NodeContainer similarContainer; private final List stmtToReplace = new ArrayList(); private final ModificationCollector collector; @@ -51,95 +50,88 @@ final class SimilarFinderVisitor extends ASTVisitor { this.stmts = stmts; this.collector = collector; this.similarContainer = new NodeContainer(); + shouldVisitStatements = true; } - { - shouldVisitStatements = true; - } + @Override + public int visit(IASTStatement stmt) { + boolean isAllreadyInMainRefactoring = isInSelection(stmt); - @Override - public int visit(IASTStatement stmt) { - - boolean isAllreadyInMainRefactoring = isInSelection(stmt); - - if( (!isAllreadyInMainRefactoring) - && this.refactoring.isStatementInTrail(stmt, trail, this.refactoring.getIndex())){ - stmtToReplace.add(stmt); - similarContainer.add(stmt); - ++i; - - if(i==stmts.size()){ - //found similar code - - boolean similarOnReturnWays = true; - for (NameInformation nameInfo : similarContainer.getAllAfterUsedNames()) { - if(this.refactoring.names.containsKey(nameInfo.getDeclaration().getRawSignature())){ - Integer nameOrderNumber = this.refactoring.names.get(nameInfo.getDeclaration().getRawSignature()); - if(this.refactoring.nameTrail.containsValue(nameOrderNumber)){ - String orgName = null; - boolean found = false; - for (Entry entry : this.refactoring.nameTrail.entrySet()) { - if(entry.getValue().equals(nameOrderNumber)){ - orgName = entry.getKey(); + if ((!isAllreadyInMainRefactoring) + && this.refactoring.isStatementInTrail(stmt, trail, this.refactoring.getIndex())) { + stmtToReplace.add(stmt); + similarContainer.add(stmt); + ++i; + + if (i == stmts.size()) { + // Found similar code + + boolean similarOnReturnWays = true; + for (NameInformation nameInfo : similarContainer.getAllAfterUsedNames()) { + if (this.refactoring.names.containsKey(nameInfo.getDeclaration().getRawSignature())) { + Integer nameOrderNumber = this.refactoring.names.get(nameInfo.getDeclaration().getRawSignature()); + if (this.refactoring.nameTrail.containsValue(nameOrderNumber)) { + String orgName = null; + boolean found = false; + for (Entry entry : this.refactoring.nameTrail.entrySet()) { + if (entry.getValue().equals(nameOrderNumber)) { + orgName = entry.getKey(); + } + } + if (orgName != null) { + for (NameInformation orgNameInfo : this.refactoring.container.getAllAfterUsedNamesChoosenByUser()) { + if (orgName.equals(orgNameInfo.getDeclaration().getRawSignature())) { + found = true; } } - if(orgName != null){ - for (NameInformation orgNameInfo : this.refactoring.container.getAllAfterUsedNamesChoosenByUser()) { - if( orgName.equals(orgNameInfo.getDeclaration().getRawSignature()) ){ - found = true; - } - } - } - - if(!found){ - similarOnReturnWays = false; - } + } + + if (!found) { + similarOnReturnWays = false; } } } - - if(similarOnReturnWays){ - IASTNode call = refactoring.getMethodCall(name, - this.refactoring.nameTrail, this.refactoring.names, - this.refactoring.container, similarContainer); - ASTRewrite rewrite = collector.rewriterForTranslationUnit(stmtToReplace.get(0) - .getTranslationUnit()); - TextEditGroup editGroup = new TextEditGroup(Messages.SimilarFinderVisitor_replaceDuplicateCode); - rewrite.replace(stmtToReplace.get(0), call, editGroup); - if (stmtToReplace.size() > 1) { - for (int i = 1; i < stmtToReplace.size(); ++i) { - rewrite.remove(stmtToReplace.get(i), editGroup); - } - } - } - - clear(); } - return PROCESS_SKIP; - } else { + if (similarOnReturnWays) { + IASTNode call = refactoring.getMethodCall(name, + this.refactoring.nameTrail, this.refactoring.names, + this.refactoring.container, similarContainer); + ASTRewrite rewrite = + collector.rewriterForTranslationUnit(stmtToReplace.get(0).getTranslationUnit()); + TextEditGroup editGroup = new TextEditGroup(Messages.SimilarFinderVisitor_replaceDuplicateCode); + rewrite.replace(stmtToReplace.get(0), call, editGroup); + if (stmtToReplace.size() > 1) { + for (int i = 1; i < stmtToReplace.size(); ++i) { + rewrite.remove(stmtToReplace.get(i), editGroup); + } + } + } clear(); - return super.visit(stmt); } - + return PROCESS_SKIP; + } else { + clear(); + return super.visit(stmt); } + } - private boolean isInSelection(IASTStatement stmt) { - Listnodes = this.refactoring.container.getNodesToWrite(); - for (IASTNode node : nodes) { - if(node.equals(stmt)) { - return true; - } + private boolean isInSelection(IASTStatement stmt) { + Listnodes = this.refactoring.container.getNodesToWrite(); + for (IASTNode node : nodes) { + if (node.equals(stmt)) { + return true; } - return false; } + return false; + } - private void clear() { - i = 0; - this.refactoring.names.clear(); - similarContainer = new NodeContainer(); - this.refactoring.namesCounter.setObject(ExtractFunctionRefactoring.NULL_INTEGER); - this.refactoring.trailPos.setObject(ExtractFunctionRefactoring.NULL_INTEGER); + private void clear() { + i = 0; + this.refactoring.names.clear(); + similarContainer = new NodeContainer(); + this.refactoring.namesCounter.setObject(ExtractFunctionRefactoring.NULL_INTEGER); + this.refactoring.trailPos.setObject(ExtractFunctionRefactoring.NULL_INTEGER); stmtToReplace.clear(); - } - } \ No newline at end of file + } +} \ No newline at end of file