From 6a4bd1e7c17f3aa2d436c7c171bdfbc38f36dc7d Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 17 Nov 2010 01:40:04 +0000 Subject: [PATCH] Cosmetics. --- .../internal/ui/refactoring/CRefactoring.java | 12 +- .../ExtractConstantRefactoring.java | 101 +++++------ .../ExtractFunctionRefactoring.java | 159 +++++++----------- .../ExtractLocalVariableRefactoring.java | 6 +- .../GenerateGettersAndSettersRefactoring.java | 64 +++---- .../hidemethod/HideMethodRefactoring.java | 72 ++++---- .../ImplementMethodRefactoring.java | 49 +++--- 7 files changed, 192 insertions(+), 271 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java index b229d020e7b..2e4fc73eac2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java @@ -66,7 +66,7 @@ public abstract class CRefactoring extends Refactoring { protected IFile file; protected Region region; protected RefactoringStatus initStatus; - protected IASTTranslationUnit unit; + protected IASTTranslationUnit ast; protected ICProject project; private IIndex fIndex; @@ -227,8 +227,8 @@ public abstract class CRefactoring extends Refactoring { if (file != null) { try { subMonitor.subTask(Messages.Refactoring_PM_ParseTU); - unit = loadTranslationUnit(file); - if (unit == null) { + ast = loadTranslationUnit(file); + if (ast == null) { subMonitor.done(); return false; } @@ -264,7 +264,7 @@ public abstract class CRefactoring extends Refactoring { protected boolean translationUnitHasProblem() { ProblemFinder pf = new ProblemFinder(initStatus); - unit.accept(pf); + ast.accept(pf); return pf.hasProblem(); } @@ -293,13 +293,13 @@ public abstract class CRefactoring extends Refactoring { } public IASTTranslationUnit getUnit() { - return unit; + return ast; } protected ArrayList findAllMarkedNames() { final ArrayList namesVector = new ArrayList(); - unit.accept(new ASTVisitor() { + ast.accept(new ASTVisitor() { { shouldVisitNames = true; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java index 4407915ce15..d209894959f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoring.java @@ -31,6 +31,7 @@ import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.text.edits.TextEditGroup; import org.eclipse.cdt.core.dom.ast.ASTNodeFactoryFactory; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; @@ -45,7 +46,6 @@ import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.INodeFactory; import org.eclipse.cdt.core.dom.ast.IType; -import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory; @@ -74,10 +74,8 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper; * postconditions and collecting/creating the modifications to the AST. * * @author Mirko Stocker - * */ public class ExtractConstantRefactoring extends CRefactoring { - public static final String ID = "org.eclipse.cdt.ui.refactoring.extractconstant.ExtractConstantRefactoring"; //$NON-NLS-1$ private IASTLiteralExpression target = null; @@ -85,7 +83,7 @@ public class ExtractConstantRefactoring extends CRefactoring { private final ExtractConstantInfo info; - public ExtractConstantRefactoring(IFile file, ISelection selection, ExtractConstantInfo info, ICProject proj){ + public ExtractConstantRefactoring(IFile file, ISelection selection, ExtractConstantInfo info, ICProject proj) { super(file,selection, null, proj); this.info = info; this.project = proj; @@ -99,22 +97,22 @@ public class ExtractConstantRefactoring extends CRefactoring { lockIndex(); try { RefactoringStatus status = super.checkInitialConditions(sm.newChild(6)); - if(status.hasError()) { + if (status.hasError()) { return status; } Collection literalExpressionCollection = findAllLiterals(); - if(literalExpressionCollection.isEmpty()){ + if (literalExpressionCollection.isEmpty()) { initStatus.addFatalError(Messages.ExtractConstantRefactoring_LiteralMustBeSelected); return initStatus; } sm.worked(1); - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + if (isProgressMonitorCanceld(sm, initStatus)) return initStatus; boolean oneMarked = region != null && isOneMarked(literalExpressionCollection, region); - if(!oneMarked){ + if (!oneMarked) { //No or more than one marked - if(target == null){ + if (target == null) { //No Selection found; initStatus.addFatalError(Messages.ExtractConstantRefactoring_NoLiteralSelected); } else { @@ -125,12 +123,12 @@ public class ExtractConstantRefactoring extends CRefactoring { } sm.worked(1); - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + if (isProgressMonitorCanceld(sm, initStatus)) return initStatus; findAllNodesForReplacement(literalExpressionCollection); info.addNamesToUsedNames(findAllDeclaredNames()); - if(info.getName().length() == 0) { + if (info.getName().length() == 0) { info.setName(getDefaultName(target)); } info.setMContext(NodeHelper.findMethodContext(target, getIndex())); @@ -151,7 +149,7 @@ public class ExtractConstantRefactoring extends CRefactoring { case IASTLiteralExpression.lk_char_constant: case IASTLiteralExpression.lk_string_literal: int beginIndex = 1; - if(nameString.startsWith("L")) { //$NON-NLS-1$ + if (nameString.startsWith("L")) { //$NON-NLS-1$ beginIndex = 2; } final int len= nameString.length(); @@ -172,7 +170,7 @@ public class ExtractConstantRefactoring extends CRefactoring { ArrayListnames = new ArrayList(); IASTFunctionDefinition funcDef = NodeHelper.findFunctionDefinitionInAncestors(target); ICPPASTCompositeTypeSpecifier comTypeSpec = getCompositeTypeSpecifier(funcDef); - if(comTypeSpec != null) { + if (comTypeSpec != null) { for(IASTDeclaration dec : comTypeSpec.getMembers()) { if (dec instanceof IASTSimpleDeclaration) { IASTSimpleDeclaration simpDec = (IASTSimpleDeclaration) dec; @@ -186,17 +184,16 @@ public class ExtractConstantRefactoring extends CRefactoring { } private ICPPASTCompositeTypeSpecifier getCompositeTypeSpecifier(IASTFunctionDefinition funcDef) { - if(funcDef != null) { + if (funcDef != null) { IBinding binding = funcDef.getDeclarator().getName().resolveBinding(); if (binding instanceof CPPMethod) { - CPPMethod methode = (CPPMethod) binding; IASTNode[] declarations = methode.getDeclarations(); IASTNode decl; - if(declarations != null) { + if (declarations != null) { decl = declarations[0]; - }else { + } else { decl = methode.getDefinition(); } @@ -214,18 +211,18 @@ public class ExtractConstantRefactoring extends CRefactoring { if (target.getParent() instanceof IASTUnaryExpression) { IASTUnaryExpression unary = (IASTUnaryExpression) target.getParent(); for (IASTLiteralExpression expression : literalExpressionCollection) { - if( target.getKind() == expression.getKind() - && target.toString().equals( expression.toString() ) + if (target.getKind() == expression.getKind() + && target.toString().equals(expression.toString()) && expression.getParent() instanceof IASTUnaryExpression && unary.getOperator() == ((IASTUnaryExpression)expression.getParent()).getOperator()) { - literalsToReplace.add( ((IASTUnaryExpression)expression.getParent()) ); + literalsToReplace.add(((IASTUnaryExpression)expression.getParent())); } } } else { for (IASTLiteralExpression expression : literalExpressionCollection) { - if( target.getKind() == expression.getKind() - && target.toString().equals( expression.toString() ) ) { - literalsToReplace.add( expression ); + if (target.getKind() == expression.getKind() + && target.toString().equals(expression.toString())) { + literalsToReplace.add(expression); } } } @@ -235,8 +232,8 @@ public class ExtractConstantRefactoring extends CRefactoring { boolean oneMarked = false; for (IASTLiteralExpression expression : literalExpressionCollection) { boolean isInSameFileSelection = SelectionHelper.isInSameFileSelection(textSelection, expression, file); - if(isInSameFileSelection){ - if(target == null) { + if (isInSameFileSelection) { + if (target == null) { target = expression; oneMarked = true; } else { @@ -250,22 +247,21 @@ public class ExtractConstantRefactoring extends CRefactoring { private Collection findAllLiterals() { final Collection result = new ArrayList(); - unit.accept(new CPPASTVisitor(){ + ast.accept(new ASTVisitor() { { shouldVisitExpressions = true; } @Override public int visit(IASTExpression expression) { if (expression instanceof IASTLiteralExpression) { - if(!(expression.getNodeLocations().length == 1 - && expression.getNodeLocations()[0] instanceof IASTMacroExpansionLocation)){ + if (!(expression.getNodeLocations().length == 1 + && expression.getNodeLocations()[0] instanceof IASTMacroExpansionLocation)) { IASTLiteralExpression literal = (IASTLiteralExpression) expression; result.add(literal); } } return super.visit(expression); } - }); return result; @@ -273,57 +269,52 @@ public class ExtractConstantRefactoring extends CRefactoring { @Override protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) - throws CoreException, OperationCanceledException{ + throws CoreException, OperationCanceledException{ try { lockIndex(); try { MethodContext context = info.getMContext(); Collection locLiteralsToReplace = new ArrayList(); - if(context.getType() == MethodContext.ContextType.METHOD){ - + if (context.getType() == MethodContext.ContextType.METHOD) { for (IASTExpression expression : literalsToReplace) { MethodContext exprContext = NodeHelper.findMethodContext(expression, getIndex()); - if(exprContext.getType() == MethodContext.ContextType.METHOD){ - if(context.getMethodQName() != null) { - if( MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())){ + if (exprContext.getType() == MethodContext.ContextType.METHOD) { + if (context.getMethodQName() != null) { + if (MethodContext.isSameClass(exprContext.getMethodQName(), context.getMethodQName())) { locLiteralsToReplace.add(expression); } - }else { - if( MethodContext.isSameClass(exprContext.getMethodDeclarationName(), context.getMethodDeclarationName())){ + } else { + if (MethodContext.isSameClass(exprContext.getMethodDeclarationName(), context.getMethodDeclarationName())) { locLiteralsToReplace.add(expression); } } } } - } else { - for (IASTExpression expression : literalsToReplace) { IPath path = new Path(expression.getContainingFilename()); IFile expressionFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); //expressionFile may be null if the file is NOT in the workspace - if( expressionFile != null && expressionFile.equals(file) ){ + if (expressionFile != null && expressionFile.equals(file)) { locLiteralsToReplace.add(expression); } } - } //Create all Changes for literals String constName = info.getName(); createLiteralToConstantChanges(constName, locLiteralsToReplace, collector); - if(context.getType() == MethodContext.ContextType.METHOD) { + if (context.getType() == MethodContext.ContextType.METHOD) { ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) context.getMethodDeclaration().getParent(); AddDeclarationNodeToClassChange.createChange(classDefinition, info.getVisibility(), getConstNodesClass(constName), true, collector); } else { IASTDeclaration nodes = getConstNodesGlobal(constName); - ASTRewrite rewriter = collector.rewriterForTranslationUnit(unit); - rewriter.insertBefore(unit, TranslationUnitHelper.getFirstNode(unit), nodes, new TextEditGroup(Messages.ExtractConstantRefactoring_CreateConstant)); + ASTRewrite rewriter = collector.rewriterForTranslationUnit(ast); + rewriter.insertBefore(ast, TranslationUnitHelper.getFirstNode(ast), nodes, new TextEditGroup(Messages.ExtractConstantRefactoring_CreateConstant)); } - } - finally { + } finally { unlockIndex(); } } catch (InterruptedException e) { @@ -334,7 +325,8 @@ public class ExtractConstantRefactoring extends CRefactoring { @Override protected RefactoringDescriptor getRefactoringDescriptor() { Map arguments = getArgumentMap(); - RefactoringDescriptor desc = new ExtractConstantRefactoringDescription( project.getProject().getName(), "Extract Constant Refactoring", "Create constant for " + target.getRawSignature(), arguments); //$NON-NLS-1$//$NON-NLS-2$ + RefactoringDescriptor desc = new ExtractConstantRefactoringDescription(project.getProject().getName(), + "Extract Constant Refactoring", "Create constant for " + target.getRawSignature(), arguments); //$NON-NLS-1$//$NON-NLS-2$ return desc; } @@ -347,8 +339,8 @@ public class ExtractConstantRefactoring extends CRefactoring { return arguments; } - private void createLiteralToConstantChanges(String constName, Iterable literals, ModificationCollector collector) { - + private void createLiteralToConstantChanges(String constName, + Iterable literals, ModificationCollector collector) { for (IASTExpression each : literals) { ASTRewrite rewrite = collector.rewriterForTranslationUnit(each.getTranslationUnit()); CPPASTIdExpression idExpression = new CPPASTIdExpression(new CPPASTName(constName.toCharArray())); @@ -356,12 +348,10 @@ public class ExtractConstantRefactoring extends CRefactoring { } } - private IASTSimpleDeclaration getConstNodes(String newName){ - + private IASTSimpleDeclaration getConstNodes(String newName) { ICPPNodeFactory factory = ASTNodeFactoryFactory.getDefaultCPPNodeFactory(); DeclarationGenerator generator = DeclarationGenerator.create(factory); - IType type = target.getExpressionType(); IASTDeclSpecifier declSpec = generator.createDeclSpecFromType(type); @@ -386,10 +376,10 @@ public class ExtractConstantRefactoring extends CRefactoring { return simple; } - private IASTDeclaration getConstNodesGlobal(String newName){ + private IASTDeclaration getConstNodesGlobal(String newName) { IASTSimpleDeclaration simple = getConstNodes(newName); - INodeFactory factory= unit.getASTNodeFactory(); + INodeFactory factory= ast.getASTNodeFactory(); if (factory instanceof ICPPNodeFactory) { ICPPASTNamespaceDefinition namespace = ((ICPPNodeFactory) factory).newNamespaceDefinition(new CPPASTName()); namespace.addDeclaration(simple); @@ -400,10 +390,9 @@ public class ExtractConstantRefactoring extends CRefactoring { return simple; } - private IASTDeclaration getConstNodesClass(String newName){ + private IASTDeclaration getConstNodesClass(String newName) { IASTSimpleDeclaration simple = getConstNodes(newName); simple.getDeclSpecifier().setStorageClass(IASTDeclSpecifier.sc_static); return simple; } - } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java index af881266614..64bae50ac28 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java @@ -109,13 +109,11 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper; public class ExtractFunctionRefactoring extends CRefactoring { - public static final String ID = "org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring"; //$NON-NLS-1$ static final Integer NULL_INTEGER = Integer.valueOf(0); static final char[] ZERO= "0".toCharArray(); //$NON-NLS-1$ - NodeContainer container; final ExtractFunctionInformation info; @@ -151,7 +149,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { try { RefactoringStatus status = super.checkInitialConditions(sm.newChild(6)); - if(status.hasError()) { + if (status.hasError()) { return status; } @@ -189,8 +187,8 @@ public class ExtractFunctionRefactoring extends CRefactoring { info.setInScopeDeclaredVariable(container.getAllDeclaredInScope().get(0)); } - extractedFunctionConstructionHelper = ExtractedFunctionConstructionHelper - .createFor(container.getNodesToWrite()); + extractedFunctionConstructionHelper = + ExtractedFunctionConstructionHelper.createFor(container.getNodesToWrite()); boolean isExtractExpression = container.getNodesToWrite().get(0) instanceof IASTExpression; info.setExtractExpression(isExtractExpression); @@ -205,14 +203,13 @@ public class ExtractFunctionRefactoring extends CRefactoring { for (int i = 0; i < info.getAllUsedNames().size(); i++) { if (!info.getAllUsedNames().get(i).isDeclarationInScope()) { NameInformation name = info.getAllUsedNames().get(i); - if(!name.isReturnValue()) { + if (!name.isReturnValue()) { name.setUserSetIsReference(name.isReference()); } } } sm.done(); - } - finally { + } finally { unlockIndex(); } } catch (InterruptedException e) { @@ -225,18 +222,14 @@ public class ExtractFunctionRefactoring extends CRefactoring { ArrayList paras = container.getNames(); for (NameInformation name : paras) { - int flag = CPPVariableReadWriteFlags.getReadWriteFlags(name - .getName()); + int flag = CPPVariableReadWriteFlags.getReadWriteFlags(name.getName()); if ((flag & PDOMName.WRITE_ACCESS) != 0) { name.setWriteAccess(true); } } - } - private void checkForNonExtractableStatements(NodeContainer cont, - RefactoringStatus status) { - + private void checkForNonExtractableStatements(NodeContainer cont, RefactoringStatus status) { NonExtractableStmtFinder vis = new NonExtractableStmtFinder(); for (IASTNode node : cont.getNodesToWrite()) { node.accept(vis); @@ -257,17 +250,14 @@ public class ExtractFunctionRefactoring extends CRefactoring { break; } } - } private ICPPASTFunctionDeclarator getDeclaration(IASTNode node) { - while (node != null && !(node instanceof IASTFunctionDefinition)) { node = node.getParent(); } if (node != null) { - IASTFunctionDeclarator declarator = ((IASTFunctionDefinition) node) - .getDeclarator(); + IASTFunctionDeclarator declarator = ((IASTFunctionDefinition) node).getDeclarator(); if (declarator instanceof ICPPASTFunctionDeclarator) { return (ICPPASTFunctionDeclarator) declarator; } @@ -318,8 +308,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { try { lockIndex(); try { - final IASTName astMethodName = new CPPASTName(info.getMethodName() - .toCharArray()); + final IASTName astMethodName = new CPPASTName(info.getMethodName().toCharArray()); MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex()); @@ -330,27 +319,24 @@ public class ExtractFunctionRefactoring extends CRefactoring { // Create Method Definition IASTNode firstNode = container.getNodesToWrite().get(0); IPath implPath = new Path(firstNode.getContainingFilename()); - final IFile implementationFile = ResourcesPlugin.getWorkspace() - .getRoot().getFileForLocation(implPath); + final IFile implementationFile = + ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(implPath); - createMethodDefinition(astMethodName, context, firstNode, - implementationFile, collector); + createMethodDefinition(astMethodName, context, firstNode, implementationFile, + collector); createMethodCalls(astMethodName, implementationFile, context, collector); - } - finally { + } finally { unlockIndex(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - } private void createMethodCalls(final IASTName astMethodName, final IFile implementationFile, MethodContext context, ModificationCollector collector) throws CoreException { - String title; if (context.getType() == MethodContext.ContextType.METHOD) { title = Messages.ExtractFunctionRefactoring_CreateMethodCall; @@ -361,11 +347,10 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTNode methodCall = getMethodCall(astMethodName); IASTNode firstNodeToWrite = container.getNodesToWrite().get(0); - ASTRewrite rewriter = collector - .rewriterForTranslationUnit(firstNodeToWrite - .getTranslationUnit()); + ASTRewrite rewriter = collector.rewriterForTranslationUnit( + firstNodeToWrite.getTranslationUnit()); TextEditGroup editGroup = new TextEditGroup(title); - if(methodCall instanceof IASTDeclaration){ + if (methodCall instanceof IASTDeclaration){ CPPASTDeclarationStatement declarationStatement = new CPPASTDeclarationStatement((IASTDeclaration) methodCall); methodCall = declarationStatement; } @@ -385,7 +370,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { private void insertCallintoTree(IASTNode methodCall, List list, ASTRewrite rewriter, TextEditGroup editGroup) { IASTNode firstNode = list.get(0); - if(list.size() > 1 && firstNode.getParent() instanceof IASTBinaryExpression && + if (list.size() > 1 && firstNode.getParent() instanceof IASTBinaryExpression && firstNode.getParent().getParent() instanceof IASTBinaryExpression) { IASTBinaryExpression parent = (IASTBinaryExpression) firstNode.getParent(); IASTExpression leftSubTree = parent.getOperand1(); @@ -397,7 +382,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { newParentNode.setOperator(op); newParentNode.setOperand2((IASTExpression) methodCall); rewriter.replace(rootBinExp, newParentNode, editGroup); - }else { + } else { rewriter.replace(firstNode, methodCall, editGroup); } } @@ -412,7 +397,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { private void createMethodDefinition(final IASTName astMethodName, MethodContext context, IASTNode firstNode, final IFile implementationFile, ModificationCollector collector) { - IASTFunctionDefinition node = NodeHelper.findFunctionDefinitionInAncestors(firstNode); if (node != null) { String title; @@ -434,14 +418,12 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTSimpleDeclaration methodDeclaration = getDeclaration(collector, astMethodName); - AddDeclarationNodeToClassChange.createChange(classDeclaration, info - .getVisibility(), methodDeclaration, false, collector); - + AddDeclarationNodeToClassChange.createChange(classDeclaration, info.getVisibility(), + methodDeclaration, false, collector); } private void replaceSimilar(ModificationCollector collector, final IASTName astMethodName, - final IFile implementationFile, - final ContextType contextType) { + final IFile implementationFile, final ContextType contextType) { // Find similar code final List nodesToRewriteWithoutComments = new LinkedList(); @@ -460,14 +442,13 @@ public class ExtractFunctionRefactoring extends CRefactoring { } if (!hasNameResolvingForSimilarError) { - unit.accept(new SimilarFinderVisitor(this, collector, initTrail, implementationFile, + ast.accept(new SimilarFinderVisitor(this, collector, initTrail, implementationFile, astMethodName, nodesToRewriteWithoutComments, title)); } } protected Vector getTrail(List stmts) { final Vector trail = new Vector(); - nameTrail = new HashMap(); final Container trailCounter = new Container(NULL_INTEGER); @@ -475,7 +456,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { node.accept(new CPPASTAllVisitor() { @Override public int visitAll(IASTNode node) { - if (node instanceof IASTComment) { // Visit Comment, but don't add them to the trail return super.visitAll(node); @@ -489,7 +469,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { trail.add(node); return super.visitAll(node); } else { - // Save Name Sequenz Number + // Save Name Sequence Number IASTName name = (IASTName) node; TrailName trailName = new TrailName(name); int actCount = trailCounter.getObject().intValue(); @@ -530,7 +510,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { stmt.accept(new CPPASTAllVisitor() { @Override public int visitAll(IASTNode node) { - int pos = trailPos.getObject().intValue(); if (trail.size() <= 0 || pos >= trail.size()) { @@ -552,7 +531,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { } else { return super.visitAll(node); } - } else { same.setObject(new Boolean(false)); return PROCESS_ABORT; @@ -563,15 +541,14 @@ public class ExtractFunctionRefactoring extends CRefactoring { return same.getObject().booleanValue(); } - private boolean isMethodAllreadyDefined( - IASTSimpleDeclaration methodDeclaration, + private boolean isMethodAllreadyDefined(IASTSimpleDeclaration methodDeclaration, ICPPASTCompositeTypeSpecifier classDeclaration, IIndex index) { TrailNodeEqualityChecker equalityChecker = new TrailNodeEqualityChecker( names, namesCounter, index); IBinding bind = classDeclaration.getName().resolveBinding(); - IASTStandardFunctionDeclarator declarator = (IASTStandardFunctionDeclarator) methodDeclaration - .getDeclarators()[0]; + IASTStandardFunctionDeclarator declarator = + (IASTStandardFunctionDeclarator) methodDeclaration.getDeclarators()[0]; String name = new String(declarator.getName().toCharArray()); if (bind instanceof ICPPClassType) { ICPPClassType classBind = (ICPPClassType) bind; @@ -587,23 +564,16 @@ public class ExtractFunctionRefactoring extends CRefactoring { IParameter[] parameters = method.getParameters(); if (parameters.length == declarator.getParameters().length) { for (int i = 0; i < parameters.length; i++) { - IASTName[] origParameterName = unit - .getDeclarationsInAST(parameters[i]); - - IASTParameterDeclaration origParameter = (IASTParameterDeclaration) origParameterName[0] - .getParent().getParent(); - IASTParameterDeclaration newParameter = declarator - .getParameters()[i]; + IASTName[] origParameterName = ast.getDeclarationsInAST(parameters[i]); + IASTParameterDeclaration origParameter = + (IASTParameterDeclaration) origParameterName[0].getParent().getParent(); + IASTParameterDeclaration newParameter = declarator.getParameters()[i]; // if not the same break; - if (!(equalityChecker.isEquals(origParameter - .getDeclSpecifier(), newParameter - .getDeclSpecifier()) && ASTHelper - .samePointers(origParameter - .getDeclarator() - .getPointerOperators(), - newParameter.getDeclarator() - .getPointerOperators(), + if (!(equalityChecker.isEquals(origParameter.getDeclSpecifier(), + newParameter.getDeclSpecifier()) && + ASTHelper.samePointers(origParameter.getDeclarator().getPointerOperators(), + newParameter.getDeclarator().getPointerOperators(), equalityChecker))) { break; } @@ -613,7 +583,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { } } } - } } return false; @@ -623,10 +592,9 @@ public class ExtractFunctionRefactoring extends CRefactoring { private void addMethod(IASTName astMethodName, MethodContext context, ASTRewrite rewriter, IASTNode insertpoint, TextEditGroup group) { - ICPPASTQualifiedName qname = new CPPASTQualifiedName(); if (context.getType() == ContextType.METHOD) { - if(context.getMethodQName() != null) { + if (context.getMethodQName() != null) { for (int i = 0; i < (context.getMethodQName().getNames().length - 1); i++) { qname.addName(new CPPASTName(context.getMethodQName().getNames()[i].toCharArray())); } @@ -635,7 +603,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { qname.addName(astMethodName); IASTFunctionDefinition func = new CPPASTFunctionDefinition(); - func.setParent(unit); + func.setParent(ast); IASTDeclSpecifier returnType = getReturnType(); func.setDeclSpecifier(returnType); @@ -643,26 +611,24 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTStandardFunctionDeclarator createdFunctionDeclarator = extractedFunctionConstructionHelper .createFunctionDeclarator(qname, info.getDeclarator(), info .getReturnVariable(), container.getNodesToWrite(), info - .getAllUsedNames(), unit.getASTNodeFactory()); + .getAllUsedNames(), ast.getASTNodeFactory()); func.setDeclarator(createdFunctionDeclarator); IASTCompoundStatement compound = new CPPASTCompoundStatement(); func.setBody(compound); ASTRewrite subRW; - if(insertpoint.getParent() instanceof ICPPASTTemplateDeclaration) { - + if (insertpoint.getParent() instanceof ICPPASTTemplateDeclaration) { CPPASTTemplateDeclaration templateDeclaration = new CPPASTTemplateDeclaration(); - templateDeclaration.setParent(unit); + templateDeclaration.setParent(ast); - for(ICPPASTTemplateParameter templateParameter : ((ICPPASTTemplateDeclaration) insertpoint.getParent()).getTemplateParameters()) { + for (ICPPASTTemplateParameter templateParameter : ((ICPPASTTemplateDeclaration) insertpoint.getParent()).getTemplateParameters()) { templateDeclaration.addTemplateParameter(templateParameter.copy()); } templateDeclaration.setDeclaration(func); - - subRW = rewriter.insertBefore(insertpoint.getParent().getParent(), insertpoint.getParent(), templateDeclaration, group); - + subRW = rewriter.insertBefore(insertpoint.getParent().getParent(), insertpoint.getParent(), + templateDeclaration, group); } else { subRW = rewriter.insertBefore(insertpoint.getParent(), insertpoint, func, group); @@ -690,7 +656,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { } subRW.insertBefore(compound, null, returnStmt, group); } - } private IASTName newName(IASTName declaration) { @@ -698,12 +663,10 @@ public class ExtractFunctionRefactoring extends CRefactoring { } private IASTDeclSpecifier getReturnType() { - IASTNode firstNodeToWrite = container.getNodesToWrite().get(0); NameInformation returnVariable = info.getReturnVariable(); - return extractedFunctionConstructionHelper.determineReturnType( - firstNodeToWrite, returnVariable); + return extractedFunctionConstructionHelper.determineReturnType(firstNodeToWrite, returnVariable); } protected IASTNode getMethodCall(IASTName astMethodName, @@ -721,8 +684,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { boolean theRetName = false; for (NameInformation nameInfo : myContainer.getNames()) { - Integer trailSeqNumber = trailNameTable.get(nameInfo - .getDeclaration().getRawSignature()); + Integer trailSeqNumber = trailNameTable.get(nameInfo.getDeclaration().getRawSignature()); String orgName = null; for (Entry entry : similarNameTable.entrySet()) { if (entry.getValue().equals(trailSeqNumber)) { @@ -736,8 +698,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { if (orgName != null) { boolean found = false; - for (NameInformation simNameInfo : mySimilarContainer - .getNames()) { + for (NameInformation simNameInfo : mySimilarContainer.getNames()) { if (orgName.equals(simNameInfo.getDeclaration() .getRawSignature())) { addAParameterIfPossible(args, declarations, @@ -746,9 +707,8 @@ public class ExtractFunctionRefactoring extends CRefactoring { if (theRetName) { theRetName = false; - retName = new CPPASTName(simNameInfo - .getDeclaration().getRawSignature() - .toCharArray()); + retName = new CPPASTName( + simNameInfo.getDeclaration().getRawSignature().toCharArray()); } } } @@ -792,7 +752,6 @@ public class ExtractFunctionRefactoring extends CRefactoring { } IASTName retname = newName(info.getReturnVariable().getName()); return getReturnAssignment(stmt, callExpression, retname); - } private IASTNode getReturnAssignment(IASTExpressionStatement stmt, @@ -808,8 +767,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { declarator.setName(retname); - for (IASTPointerOperator pointer : orgDecl.getDeclarators()[0] - .getPointerOperators()) { + for (IASTPointerOperator pointer : orgDecl.getDeclarators()[0].getPointerOperators()) { declarator.addPointerOperator(pointer.copy()); } @@ -834,19 +792,17 @@ public class ExtractFunctionRefactoring extends CRefactoring { private IASTNode getReturnAssignment(IASTExpressionStatement stmt, IASTExpression callExpression) { - IASTNode node = container.getNodesToWrite().get(0); return extractedFunctionConstructionHelper.createReturnAssignment(node, stmt, callExpression); - } private IASTSimpleDeclaration getDeclaration(IASTName name) { IASTSimpleDeclaration simpleDecl = new CPPASTSimpleDeclaration(); - IASTStandardFunctionDeclarator declarator = extractedFunctionConstructionHelper - .createFunctionDeclarator(name, info.getDeclarator(), info - .getReturnVariable(), container.getNodesToWrite(), info - .getAllUsedNames(), unit.getASTNodeFactory()); + IASTStandardFunctionDeclarator declarator = + extractedFunctionConstructionHelper.createFunctionDeclarator(name, + info.getDeclarator(), info.getReturnVariable(), + container.getNodesToWrite(), info.getAllUsedNames(), ast.getASTNodeFactory()); simpleDecl.addDeclarator(declarator); return simpleDecl; } @@ -854,21 +810,21 @@ public class ExtractFunctionRefactoring extends CRefactoring { private IASTSimpleDeclaration getDeclaration(ModificationCollector collector,IASTName name) { IASTDeclSpecifier declSpec = getReturnType(); IASTSimpleDeclaration simpleDecl = factory.newSimpleDeclaration(declSpec); - if(info.isVirtual() && declSpec instanceof ICPPASTDeclSpecifier) { + if (info.isVirtual() && declSpec instanceof ICPPASTDeclSpecifier) { ((ICPPASTDeclSpecifier)declSpec).setVirtual(true); } - simpleDecl.setParent(unit); + simpleDecl.setParent(ast); IASTStandardFunctionDeclarator declarator = extractedFunctionConstructionHelper .createFunctionDeclarator(name, info.getDeclarator(), info .getReturnVariable(), container.getNodesToWrite(), info - .getAllUsedNames(), unit.getASTNodeFactory()); + .getAllUsedNames(), ast.getASTNodeFactory()); simpleDecl.addDeclarator(declarator); return simpleDecl; } private NodeContainer findExtractableNodes() { final NodeContainer container = new NodeContainer(); - unit.accept(new ASTVisitor() { + ast.accept(new ASTVisitor() { { shouldVisitStatements = true; shouldVisitExpressions = true; @@ -933,5 +889,4 @@ public class ExtractFunctionRefactoring extends CRefactoring { arguments.put(ExtractFunctionRefactoringDescription.REPLACE_DUBLICATES, Boolean.toString(info.isReplaceDuplicates())); return arguments; } - } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java index 5680c55627d..dbc0537e502 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java @@ -224,7 +224,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { private NodeContainer findAllExpressions() { final NodeContainer container = new NodeContainer(); - unit.accept(new ASTVisitor() { + ast.accept(new ASTVisitor() { { shouldVisitExpressions = true; } @@ -255,7 +255,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { IASTStatement declInsertPoint = getParentStatement(target); IASTDeclarationStatement declaration = getVariableNodes(variableName); declaration.setParent(declInsertPoint.getParent()); - ASTRewrite rewriter = collector.rewriterForTranslationUnit(unit); + ASTRewrite rewriter = collector.rewriterForTranslationUnit(ast); rewriter.insertBefore(declInsertPoint.getParent(), declInsertPoint, declaration, editGroup); // Replace target with reference to temporary variable @@ -280,7 +280,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { } private IASTDeclarationStatement getVariableNodes(String newName) { - INodeFactory factory = this.unit.getASTNodeFactory(); + INodeFactory factory = this.ast.getASTNodeFactory(); IASTSimpleDeclaration simple = factory.newSimpleDeclaration(null); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java index 56979eea61a..a2f8dfd6e96 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersRefactoring.java @@ -55,7 +55,6 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; /** * @author Thomas Corbat - * */ public class GenerateGettersAndSettersRefactoring extends CRefactoring { @@ -73,10 +72,9 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { @Override public int visit(IASTDeclSpecifier declSpec) { - if (declSpec instanceof IASTCompositeTypeSpecifier) { IASTFileLocation loc = declSpec.getFileLocation(); - if(start > loc.getNodeOffset() && start < loc.getNodeOffset()+ loc.getNodeLength()) { + if (start > loc.getNodeOffset() && start < loc.getNodeOffset()+ loc.getNodeLength()) { container.setObject((IASTCompositeTypeSpecifier) declSpec); return ASTVisitor.PROCESS_ABORT; } @@ -99,22 +97,18 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { SubMonitor sm = SubMonitor.convert(pm, 10); RefactoringStatus status = super.checkInitialConditions(sm.newChild(6)); - if(status.hasError()) { + if (status.hasError()) { return status; } - if(!initStatus.hasFatalError()) { - + if (!initStatus.hasFatalError()) { initRefactoring(pm); - - if(context.existingFields.size() == 0) { + if (context.existingFields.size() == 0) { initStatus.addFatalError(Messages.GenerateGettersAndSettersRefactoring_NoFields); } } return initStatus; } - - @Override public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, @@ -123,14 +117,14 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { try { lockIndex(); finalStatus = super.checkFinalConditions(pm); - if(!context.isImplementationInHeader()) { + if (!context.isImplementationInHeader()) { definitionInsertLocation = findInsertLocation(); - if(file.equals(definitionInsertLocation.getInsertFile())) { + if (file.equals(definitionInsertLocation.getInsertFile())) { finalStatus.addInfo(Messages.GenerateGettersAndSettersRefactoring_NoImplFile); } } - } catch (InterruptedException e) {} - finally { + } catch (InterruptedException e) { + } finally { unlockIndex(); } return finalStatus; @@ -140,14 +134,14 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { loadTranslationUnit(initStatus, pm); context.selectedName = getSelectedName(); IASTCompositeTypeSpecifier compositeTypeSpecifier = null; - if(context.selectedName != null) { + if (context.selectedName != null) { compositeTypeSpecifier = getCompositeTypeSpecifier(context.selectedName); - }else { + } else { compositeTypeSpecifier = findCurrentCompositeTypeSpecifier(); } - if(compositeTypeSpecifier != null) { + if (compositeTypeSpecifier != null) { findDeclarations(compositeTypeSpecifier); - }else { + } else { initStatus.addFatalError(Messages.GenerateGettersAndSettersRefactoring_NoCassDefFound); } } @@ -155,7 +149,7 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { private IASTCompositeTypeSpecifier findCurrentCompositeTypeSpecifier() { final int start = region.getOffset(); Container container = new Container(); - unit.accept(new CompositeTypeSpecFinder(start, container)); + ast.accept(new CompositeTypeSpecFinder(start, container)); return container.getObject(); } @@ -176,9 +170,7 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { } protected void findDeclarations(IASTCompositeTypeSpecifier compositeTypeSpecifier) { - compositeTypeSpecifier.accept(new ASTVisitor() { - { shouldVisitDeclarations = true; } @@ -198,7 +190,7 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { if ((innermostDeclarator instanceof IASTFunctionDeclarator)) { context.existingFunctionDeclarations.add(fieldDeclaration); } else { - if(SelectionHelper.isInSameFile(fieldDeclaration, file)){ + if (SelectionHelper.isInSameFile(fieldDeclaration, file)) { context.existingFields.add(fieldDeclaration); } } @@ -218,31 +210,32 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { } @Override - protected void collectModifications(IProgressMonitor pm,ModificationCollector collector) throws CoreException, OperationCanceledException { + protected void collectModifications(IProgressMonitor pm,ModificationCollector collector) + throws CoreException, OperationCanceledException { try { lockIndex(); ArrayList getterAndSetters = new ArrayList(); ArrayList definitions = new ArrayList(); - for(GetterSetterInsertEditProvider currentProvider : context.selectedFunctions){ - if(context.isImplementationInHeader()) { + for (GetterSetterInsertEditProvider currentProvider : context.selectedFunctions) { + if (context.isImplementationInHeader()) { getterAndSetters.add(currentProvider.getFunctionDefinition(false)); - }else { + } else { getterAndSetters.add(currentProvider.getFunctionDeclaration()); definitions.add(currentProvider.getFunctionDefinition(true)); } } - if(!context.isImplementationInHeader()) { + if (!context.isImplementationInHeader()) { addDefinition(collector, definitions); } - ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) context.existingFields.get(context.existingFields.size()-1).getParent(); + ICPPASTCompositeTypeSpecifier classDefinition = + (ICPPASTCompositeTypeSpecifier) context.existingFields.get(context.existingFields.size() - 1).getParent(); - AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_public, getterAndSetters, false, collector); - } catch (InterruptedException e) {} - finally { + AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_public, + getterAndSetters, false, collector); + } catch (InterruptedException e) { + } finally { unlockIndex(); } - - } private void addDefinition(ModificationCollector collector, ArrayList definitions) @@ -269,8 +262,8 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { private InsertLocation findInsertLocation() throws CoreException { IASTSimpleDeclaration decl = context.existingFields.get(0); - - InsertLocation insertLocation = MethodDefinitionInsertLocationFinder.find(decl.getFileLocation(), decl.getParent(), file); + InsertLocation insertLocation = MethodDefinitionInsertLocationFinder.find(decl.getFileLocation(), + decl.getParent(), file); if (!insertLocation.hasFile() || NodeHelper.isContainedInTemplateDeclaration(decl)) { insertLocation.setInsertFile(file); @@ -278,7 +271,6 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { } return insertLocation; - } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java index 29bb93700e3..c9de2a7b291 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/HideMethodRefactoring.java @@ -59,10 +59,8 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; /** * @author Guido Zgraggen IFS - * */ public class HideMethodRefactoring extends CRefactoring { - public static final String ID = "org.eclipse.cdt.internal.ui.refactoring.hidemethod.HideMethodRefactoring"; //$NON-NLS-1$ private IASTName methodToHide; @@ -82,11 +80,11 @@ public class HideMethodRefactoring extends CRefactoring { try { super.checkInitialConditions(sm.newChild(6)); - if(initStatus.hasFatalError()){ + if (initStatus.hasFatalError()) { return initStatus; } - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + if (isProgressMonitorCanceld(sm, initStatus)) return initStatus; IASTName name; ArrayList names = findAllMarkedNames(); @@ -96,11 +94,11 @@ public class HideMethodRefactoring extends CRefactoring { } name = names.get(names.size()-1); sm.worked(1); - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + if (isProgressMonitorCanceld(sm, initStatus)) return initStatus; declData = DeclarationFinder.getDeclaration(name, getIndex()); - if(declData == null || declData.name == null) { + if (declData == null || declData.name == null) { initStatus.addFatalError(Messages.HideMethodRefactoring_NoMethodNameSelected); return initStatus; } @@ -108,34 +106,34 @@ public class HideMethodRefactoring extends CRefactoring { methodToHide = declData.name; sm.worked(1); methodToHideDecl = NodeHelper.findSimpleDeclarationInParents(methodToHide); - if(methodToHideDecl == null) { + if (methodToHideDecl == null) { initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); return initStatus; } - if(!(methodToHideDecl.getParent() instanceof ICPPASTCompositeTypeSpecifier)) { + if (!(methodToHideDecl.getParent() instanceof ICPPASTCompositeTypeSpecifier)) { methodToHideDecl = NodeHelper.findFunctionDefinitionInAncestors(methodToHide); } - if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; + if (isProgressMonitorCanceld(sm, initStatus)) return initStatus; sm.worked(1); - if(methodToHideDecl instanceof IASTFunctionDefinition) { + if (methodToHideDecl instanceof IASTFunctionDefinition) { IASTDeclarator declarator = ((IASTFunctionDefinition)methodToHideDecl).getDeclarator(); - if(CPPVisitor.findInnermostDeclarator(declarator).getName().getRawSignature().equals(name.getRawSignature())) { + if (CPPVisitor.findInnermostDeclarator(declarator).getName().getRawSignature().equals(name.getRawSignature())) { if (!(declarator instanceof IASTFunctionDeclarator)) { initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); return initStatus; } } - }else if (methodToHideDecl instanceof IASTSimpleDeclaration) { + } else if (methodToHideDecl instanceof IASTSimpleDeclaration) { for(IASTDeclarator declarator : ((IASTSimpleDeclaration) methodToHideDecl).getDeclarators()) { - if(declarator.getName().getRawSignature().equals(name.getRawSignature())) { + if (declarator.getName().getRawSignature().equals(name.getRawSignature())) { if (!(declarator instanceof IASTFunctionDeclarator)) { initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); return initStatus; } } } - }else { + } else { initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); return initStatus; } @@ -143,16 +141,15 @@ public class HideMethodRefactoring extends CRefactoring { sm.worked(1); IASTCompositeTypeSpecifier classNode = NodeHelper.findClassInAncestors(methodToHide); - if(classNode == null) { + if (classNode == null) { initStatus.addError(Messages.HideMethodRefactoring_EnclosingClassNotFound); } - if(checkIfPrivate(classNode, methodToHideDecl)) { + if (checkIfPrivate(classNode, methodToHideDecl)) { initStatus.addError(Messages.HideMethodRefactoring_IsAlreadyPrivate); } sm.done(); - } - finally { + } finally { unlockIndex(); } } catch (InterruptedException e) { @@ -163,23 +160,22 @@ public class HideMethodRefactoring extends CRefactoring { private boolean checkIfPrivate(IASTCompositeTypeSpecifier classNode, IASTDeclaration decl) { IASTDeclaration[] members = classNode.getMembers(); - int currentVisibility = ICPPASTVisibilityLabel.v_private; - if(IASTCompositeTypeSpecifier.k_struct == classNode.getKey()) { + if (IASTCompositeTypeSpecifier.k_struct == classNode.getKey()) { currentVisibility = ICPPASTVisibilityLabel.v_public; } for (IASTDeclaration declaration : members) { - if(declaration instanceof ICPPASTVisibilityLabel){ + if (declaration instanceof ICPPASTVisibilityLabel) { currentVisibility =((ICPPASTVisibilityLabel) declaration).getVisibility(); } if (declaration != null) { - if(decl == declaration) { + if (decl == declaration) { break; } } } - if(ICPPASTVisibilityLabel.v_private == currentVisibility) { + if (ICPPASTVisibilityLabel.v_private == currentVisibility) { return true; } return false; @@ -196,12 +192,12 @@ public class HideMethodRefactoring extends CRefactoring { for(IIndexName pdomref : declData.allNamesPDom) { declData.filename = pdomref.getFileLocation().getFileName(); - if(pdomref instanceof PDOMName) { + if (pdomref instanceof PDOMName) { PDOMName pdomName = (PDOMName)pdomref; - if(pdomName.isDeclaration()) { + if (pdomName.isDeclaration()) { continue; } - if(pdomName.isDefinition()) { + if (pdomName.isDefinition()) { continue; } } @@ -211,7 +207,7 @@ public class HideMethodRefactoring extends CRefactoring { IASTFunctionDeclarator funcDec = findEnclosingFunction(expName); IASTCompositeTypeSpecifier encClass2; - if(funcDec == null) { + if (funcDec == null) { encClass2 = NodeHelper.findClassInAncestors(expName); } else { @@ -220,15 +216,14 @@ public class HideMethodRefactoring extends CRefactoring { IASTCompositeTypeSpecifier encClass = NodeHelper.findClassInAncestors(methodToHide); - if(!NodeHelper.isSameNode(encClass, encClass2)) { + if (!NodeHelper.isSameNode(encClass, encClass2)) { finalConditions.addWarning(Messages.HideMethodRefactoring_HasExternalReferences); break; } } return finalConditions; - } - finally { + } finally { unlockIndex(); } } catch (InterruptedException e) { @@ -239,26 +234,26 @@ public class HideMethodRefactoring extends CRefactoring { private IASTFunctionDeclarator findEnclosingFunction(IASTNode node) throws CoreException { IASTCompoundStatement compStat = NodeHelper.findCompoundStatementInAncestors(node); - if(compStat == null) { + if (compStat == null) { return null; } IASTNode parent = compStat.getParent(); - if(parent instanceof IASTFunctionDefinition) { + if (parent instanceof IASTFunctionDefinition) { IASTDeclarator declarator = ((IASTFunctionDefinition)parent).getDeclarator(); IASTName declaratorName = getLastName(CPPVisitor.findInnermostDeclarator(declarator)); DeclarationFinderDO data = DeclarationFinder.getDeclaration(declaratorName, getIndex()); - if(data == null || data.name == null) { + if (data == null || data.name == null) { return null; } - if(data.name.getParent() instanceof IASTFunctionDeclarator) { + if (data.name.getParent() instanceof IASTFunctionDeclarator) { return (IASTFunctionDeclarator) data.name.getParent(); } return null; - }else if(parent instanceof IASTTranslationUnit) { + } else if (parent instanceof IASTTranslationUnit) { return null; } return findEnclosingFunction(parent); @@ -266,8 +261,8 @@ public class HideMethodRefactoring extends CRefactoring { private IASTName getLastName(IASTDeclarator declarator) { IASTName declaratorName = declarator.getName(); - if(declaratorName instanceof ICPPASTQualifiedName) { - IASTName[] declaratorNames = ((ICPPASTQualifiedName)declaratorName).getNames(); + if (declaratorName instanceof ICPPASTQualifiedName) { + IASTName[] declaratorNames = ((ICPPASTQualifiedName) declaratorName).getNames(); declaratorName = declaratorNames[declaratorNames.length-1]; } return declaratorName; @@ -285,8 +280,7 @@ public class HideMethodRefactoring extends CRefactoring { AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_private, methodToHideDecl, false, collector); rewriter.remove(methodToHideDecl, editGroup); - } - finally { + } finally { unlockIndex(); } } catch (InterruptedException e) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java index 86b42871a6a..558658e18ea 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/implementmethod/ImplementMethodRefactoring.java @@ -60,10 +60,8 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper; * Checks conditions, finds insert location and generates the ImplementationNode. * * @author Mirko Stocker, Lukas Felber, Emanuel Graf - * */ public class ImplementMethodRefactoring extends CRefactoring { - private InsertLocation insertLocation; private CPPASTFunctionDeclarator createdMethodDeclarator; private ImplementMethodData data; @@ -78,15 +76,14 @@ public class ImplementMethodRefactoring extends CRefactoring { SubMonitor sm = SubMonitor.convert(pm, 10); super.checkInitialConditions(sm.newChild(6)); - if(!initStatus.hasFatalError()) { + if (!initStatus.hasFatalError()) { + data.setMethodDeclarations(findUnimplementedMethodDeclarations(ast)); - data.setMethodDeclarations(findUnimplementedMethodDeclarations(unit)); - - if(region.getLength()>0) { - IASTSimpleDeclaration methodDeclaration = SelectionHelper.findFirstSelectedDeclaration(region, unit); + if (region.getLength() > 0) { + IASTSimpleDeclaration methodDeclaration = SelectionHelper.findFirstSelectedDeclaration(region, ast); if (NodeHelper.isMethodDeclaration(methodDeclaration)) { for (MethodToImplementConfig config : data.getMethodDeclarations()) { - if(config.getDeclaration() == methodDeclaration) { + if (config.getDeclaration() == methodDeclaration) { config.setChecked(true); } } @@ -97,7 +94,6 @@ public class ImplementMethodRefactoring extends CRefactoring { return initStatus; } - private List findUnimplementedMethodDeclarations( IASTTranslationUnit unit) { final List list = new ArrayList(); @@ -111,26 +107,23 @@ public class ImplementMethodRefactoring extends CRefactoring { if (declaration instanceof IASTSimpleDeclaration) { IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) declaration; try { - if(NodeHelper.isMethodDeclaration(simpleDeclaration) && DefinitionFinder.getDefinition(simpleDeclaration, file) == null) { + if (NodeHelper.isMethodDeclaration(simpleDeclaration) && DefinitionFinder.getDefinition(simpleDeclaration, file) == null) { list.add(simpleDeclaration); } } catch (CoreException e) {} } return ASTVisitor.PROCESS_CONTINUE; } - - - }); return list; } @Override - protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException { - + protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) + throws CoreException, OperationCanceledException { List methodsToImplement = data.getMethodsToImplement(); SubMonitor sm = SubMonitor.convert(pm, 4*methodsToImplement.size()); - for(MethodToImplementConfig config : methodsToImplement) { + for (MethodToImplementConfig config : methodsToImplement) { createDefinition(collector, config, sm.newChild(4)); } } @@ -154,22 +147,21 @@ public class ImplementMethodRefactoring extends CRefactoring { } private void createParameterModifications(ASTRewrite methodRewrite, ParameterHandler handler) { - for(ParameterInfo actParameterInfo : handler.getParameterInfos()) { + for (ParameterInfo actParameterInfo : handler.getParameterInfos()) { ASTRewrite parameterRewrite = methodRewrite.insertBefore(createdMethodDeclarator, null, actParameterInfo.getParameter(), null); createNewNameInsertModification(actParameterInfo, parameterRewrite); createRemoveDefaultValueModification(actParameterInfo, parameterRewrite); } - } private void createRemoveDefaultValueModification(ParameterInfo parameterInfo, ASTRewrite parameterRewrite) { - if(parameterInfo.hasDefaultValue()) { + if (parameterInfo.hasDefaultValue()) { parameterRewrite.remove(parameterInfo.getDefaultValueNode(), null); } } private void createNewNameInsertModification(ParameterInfo parameterInfo, ASTRewrite parameterRewrite) { - if(parameterInfo.hasNewName()) { + if (parameterInfo.hasNewName()) { IASTNode insertNode = parameterInfo.getNewNameNode(); IASTName replaceNode = parameterInfo.getNameNode(); parameterRewrite.replace(replaceNode, insertNode, null); @@ -196,20 +188,19 @@ public class ImplementMethodRefactoring extends CRefactoring { private IASTDeclaration createFunctionDefinition(IASTDeclSpecifier declSpecifier, ICPPASTFunctionDeclarator functionDeclarator, IASTNode declarationParent, IASTTranslationUnit unit) throws CoreException { - IASTFunctionDefinition func = new CPPASTFunctionDefinition(); func.setParent(unit); - if(declSpecifier instanceof ICPPASTDeclSpecifier) { + if (declSpecifier instanceof ICPPASTDeclSpecifier) { ((ICPPASTDeclSpecifier) declSpecifier).setVirtual(false); } String currentFileName = declarationParent.getNodeLocations()[0].asFileLocation().getFileName(); - if(Path.fromOSString(currentFileName).equals(insertLocation.getInsertFile().getLocation())) { + if (Path.fromOSString(currentFileName).equals(insertLocation.getInsertFile().getLocation())) { declSpecifier.setInline(true); } - if(declSpecifier.getStorageClass() == IASTDeclSpecifier.sc_static) { + if (declSpecifier.getStorageClass() == IASTDeclSpecifier.sc_static) { declSpecifier.setStorageClass(IASTDeclSpecifier.sc_unspecified); } @@ -220,18 +211,18 @@ public class ImplementMethodRefactoring extends CRefactoring { createdMethodDeclarator = new CPPASTFunctionDeclarator(); createdMethodDeclarator.setName(qname); createdMethodDeclarator.setConst(functionDeclarator.isConst()); - for(IASTPointerOperator pop : functionDeclarator.getPointerOperators()) { + for (IASTPointerOperator pop : functionDeclarator.getPointerOperators()) { createdMethodDeclarator.addPointerOperator(pop.copy()); } func.setDeclarator(createdMethodDeclarator); func.setBody(new CPPASTCompoundStatement()); - if(NodeHelper.isContainedInTemplateDeclaration(declarationParent)) { + if (NodeHelper.isContainedInTemplateDeclaration(declarationParent)) { CPPASTTemplateDeclaration templateDeclaration = new CPPASTTemplateDeclaration(); templateDeclaration.setParent(unit); - for(ICPPASTTemplateParameter templateParameter : ((ICPPASTTemplateDeclaration) declarationParent.getParent().getParent() ).getTemplateParameters()) { + for (ICPPASTTemplateParameter templateParameter : ((ICPPASTTemplateDeclaration) declarationParent.getParent().getParent() ).getTemplateParameters()) { templateDeclaration.addTemplateParameter(templateParameter.copy()); } @@ -241,8 +232,8 @@ public class ImplementMethodRefactoring extends CRefactoring { return func; } - private ICPPASTQualifiedName createQualifiedNameFor(IASTFunctionDeclarator functionDeclarator, IASTNode declarationParent) - throws CoreException { + private ICPPASTQualifiedName createQualifiedNameFor(IASTFunctionDeclarator functionDeclarator, + IASTNode declarationParent) throws CoreException { int insertOffset = insertLocation.getInsertPosition(); return NameHelper.createQualifiedNameFor(functionDeclarator.getName(), file, functionDeclarator.getFileLocation().getNodeOffset(), insertLocation.getInsertFile(), insertOffset); }