From 6905e11d5d7a5874b62972620ff711f795f624ef Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 15 May 2008 14:39:55 +0000 Subject: [PATCH] Improvements for Hide Method, by Emanuel Graf, bug 232291. --- .../resources/refactoring/HideMethod.rts | 674 +++++++++++++++++- .../hidemethod/HideMethodRefactoringTest.java | 33 +- .../hidemethod/HideMethodRefactoring.java | 212 ++++-- .../ui/refactoring/hidemethod/Messages.java | 3 + .../hidemethod/messages.properties | 5 +- 5 files changed, 829 insertions(+), 98 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts index 686eb645339..62690cb439b 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/HideMethod.rts @@ -1,34 +1,3 @@ -//!HideMethodChangeToDefaultVisibility -//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest -//@.config -filename=A.h -//@A.h -#ifndef A_H_ -#define A_H_ - -#include - -class A{ -public: - //$void method2();$// -}; - -#endif /*A_H_*/ - -//= -#ifndef A_H_ -#define A_H_ - -#include - -class A{ -public: -private: - void method2(); -}; - -#endif /*A_H_*/ - //!HideMethodSimple //#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest //@.config @@ -595,6 +564,7 @@ private: //#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest //@.config filename=A.h +errors=1 //@A.h #ifndef A_H_ #define A_H_ @@ -843,3 +813,645 @@ void A:://$method2$//() { } +//!HideMethod with References 1 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=HideMethod.h +//@HideMethod.h +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void //$methode2$//(); + void methode3() + { + methode2(); + } + +}; + +#endif /* HIDEMETHOD_H_ */ + +//= +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void methode3() + { + methode2(); + } + +private: + void methode2(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//!HideMethod with References 2 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=HideMethod.h +//@HideMethod.h +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void //$methode2$//(); + void methode3(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//= +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void methode3(); +private: + void methode2(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//@HideMethod.cpp +#include "HideMethod.h" + +HideMethod::HideMethod() { + // TODO Auto-generated constructor stub +} + +HideMethod::~HideMethod() { + // TODO Auto-generated destructor stub +} + +void HideMethod::methode2() { + //do nothing +} + +void HideMethod::methode3() +{ + methode2(); +} + +//= +#include "HideMethod.h" + +HideMethod::HideMethod() { + // TODO Auto-generated constructor stub +} + +HideMethod::~HideMethod() { + // TODO Auto-generated destructor stub +} + +void HideMethod::methode2() { + //do nothing +} + +void HideMethod::methode3() +{ + methode2(); +} + +//!HideMethod with References 3 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=HideMethod.h +warnings=1 +//@HideMethod.h +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void //$methode2$//(); + void methode3(); +}; + +class test{ +public: + void call(){ + HideMethod hm; + hm.methode2(); + } +}; + +#endif /* HIDEMETHOD_H_ */ + +//= +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void methode3(); +private: + void methode2(); +}; + +class test{ +public: + void call(){ + HideMethod hm; + hm.methode2(); + } +}; + +#endif /* HIDEMETHOD_H_ */ + +//!HideMethod with References 4 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=HideMethod.h +warnings=1 +//@HideMethod.h +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void //$methode2$//(); + void methode3(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//= +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void methode3(); +private: + void methode2(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//@HideMethod.cpp +#include "HideMethod.h" + +HideMethod::HideMethod() { + // TODO Auto-generated constructor stub + +} + +HideMethod::~HideMethod() { + // TODO Auto-generated destructor stub +} + +void HideMethod::methode2() { + //do nothing +} + +void HideMethod::methode3() { + //do nothing +} + +int main(){ + HideMethod hm; + hm.methode2(); +} + +//= +//@HideMethod.cpp +#include "HideMethod.h" + +HideMethod::HideMethod() { + // TODO Auto-generated constructor stub + +} + +HideMethod::~HideMethod() { + // TODO Auto-generated destructor stub +} + +void HideMethod::methode2() { + //do nothing +} + +void HideMethod::methode3() { + //do nothing +} + +int main(){ + HideMethod hm; + hm.methode2(); +} + +//!HideMethod with CPP File selection +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=HideMethod.h +//@HideMethod.h +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void methode2(); + void methode3(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//= +#ifndef HIDEMETHOD_H_ +#define HIDEMETHOD_H_ + +class HideMethod { +public: + HideMethod(); + virtual ~HideMethod(); + void methode3(); +private: + void methode2(); +}; + +#endif /* HIDEMETHOD_H_ */ + +//@HideMethod.cpp +#include "HideMethod.h" + +HideMethod::HideMethod() { + // TODO Auto-generated constructor stub +} + +HideMethod::~HideMethod() { + // TODO Auto-generated destructor stub +} + +void HideMethod:://$methode2$//() { + //do nothing +} + +void HideMethod::methode3() +{ + methode2(); +} + +//= +#include "HideMethod.h" + +HideMethod::HideMethod() { + // TODO Auto-generated constructor stub +} + +HideMethod::~HideMethod() { + // TODO Auto-generated destructor stub +} + +void HideMethod::methode2() { + //do nothing +} + +void HideMethod::methode3() +{ + methode2(); +} + +//!HideMethodChangeToDefaultVisibility Class1 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include + +class A{ +public: + //$void method2();$// +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include + +class A{ +public: +private: + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodChangeToDefaultVisibility Class2 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +errors=1 +//@A.h +#ifndef A_H_ +#define A_H_ + +#include + +class A{ + //$void method2();$// +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include + +class A{ + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethodChangeToDefaultVisibility Struct +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +#ifndef A_H_ +#define A_H_ + +#include + +struct A{ + //$void method2();$// +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include + +struct A{ +private: + void method2(); +}; + +#endif /*A_H_*/ + +//!HideMethod CheckIfPrivateBug 1 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.cpp +//@A.cpp +struct other +{ + bool value() {return true;} +}; + +class Klass +{ +public: + void //$set$//(bool b){} + void test() + { + other o; + this->set(o.value()); + } +}; + +//= +struct other +{ + bool value() {return true;} +}; + +class Klass +{ +public: + void test() + { + other o; + this->set(o.value()); + } + +private: + void set(bool b) + { + } +}; + +//!HideMethod CheckIfPrivateBug 2 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.cpp +//@A.cpp +struct other +{ + bool value() {return true;} +}; + +class Klass +{ +public: + void set(bool b){} + void test() + { + other o; + this->//$set$//(o.value()); + } +}; + +//= +struct other +{ + bool value() {return true;} +}; + +class Klass +{ +public: + void test() + { + other o; + this->set(o.value()); + } + +private: + void set(bool b) + { + } +}; + +//!HideMethod CheckIfPrivateBug 3 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.cpp +warnings=1 +//@A.cpp +struct other +{ + bool //$value$//() {return true;} +}; + +class Klass +{ +public: + void set(bool b){} + void test() + { + other o; + this->set(o.value()); + } +}; + +//= +struct other +{ +private: + bool value() {return true;} +}; + +class Klass +{ +public: + void set(bool b){} + void test() + { + other o; + this->set(o.value()); + } +}; + +//!HideMethod CheckIfPrivateBug 4 +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.cpp +//@A.cpp +struct other +{ + bool value() {return true;} +}; + +class Klass +{ +public: + void set(bool b){} + void //$test$//() + { + other o; + this->set(o.value()); + } +}; + +//= +struct other +{ + bool value() {return true;} +}; + +class Klass +{ +public: + void set(bool b){} + +private: + void test() + { + other o; + this->set(o.value()); + } +}; + +//!HideMethod Main +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.cpp +fatalerrors=1 +//@A.cpp +int //$main$//(){ + int i = 2; + i++; + return 0; +} + +//= +int main(){ + int i = 2; + i++; + return 0; +} + +//!HideMethod empty public sections +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +class Klass +{ +public: + void //$to_move()$//; + +private: + void just_private(); +}; + +//= +class Klass +{ +public: +private: + void just_private(); + void to_move(); +}; + +//!HideMethod several private sections +//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest +//@.config +filename=A.h +//@A.h +class Klass +{ +public: + void //$to_move()$//; + +private: + void just_private(); +private: +}; + +//= +class Klass +{ +public: +private: + void just_private(); + void to_move(); +private: +}; + diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java index 6cd48da8d25..06a4238a0f2 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/hidemethod/HideMethodRefactoringTest.java @@ -21,12 +21,16 @@ import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest; import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile; +import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; import org.eclipse.cdt.internal.ui.refactoring.hidemethod.HideMethodRefactoring; /** * @author Guido Zgraggen IFS */ public class HideMethodRefactoringTest extends RefactoringTest { + private int warnings; + private int errors; + private int fatalerrors; public HideMethodRefactoringTest(String name, Vector files) { super(name, files); @@ -36,21 +40,40 @@ public class HideMethodRefactoringTest extends RefactoringTest { protected void runTest() throws Throwable { IFile refFile = project.getFile(fileWithSelection); - - HideMethodRefactoring refactoring = new HideMethodRefactoring(refFile,selection, null); + CRefactoring refactoring = new HideMethodRefactoring(refFile,selection, null); + try { + refactoring.lockIndex(); RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR); - assertConditionsOk(checkInitialConditions); + if(errors > 0) { + assertConditionsError(checkInitialConditions, errors); + }else if(fatalerrors > 0) { + assertConditionsError(checkInitialConditions, errors); + return; + }else { + assertConditionsOk(checkInitialConditions); + } Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR); RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR); - assertConditionsOk(finalConditions); + if(warnings > 0){ + assertConditionsWarning(finalConditions, warnings); + }else{ + assertConditionsOk(finalConditions); + } createChange.perform(NULL_PROGRESS_MONITOR); compareFiles(fileMap); - + } + finally { + refactoring.unlockIndex(); + } } @Override protected void configureRefactoring(Properties refactoringProperties) { + warnings = new Integer(refactoringProperties.getProperty("warnings", "0")).intValue(); //$NON-NLS-1$//$NON-NLS-2$ + errors = new Integer(refactoringProperties.getProperty("errors", "0")).intValue(); //$NON-NLS-1$//$NON-NLS-2$ + fatalerrors = new Integer(refactoringProperties.getProperty("fatalerrors", "0")).intValue(); //$NON-NLS-1$//$NON-NLS-2$ + } } 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 f8c77aa95d1..163df5dc203 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 @@ -22,21 +22,33 @@ import org.eclipse.jface.viewers.ISelection; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.text.edits.TextEditGroup; -import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; -import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.cdt.core.index.IIndexName; import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMName; + import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector; +import org.eclipse.cdt.internal.ui.refactoring.utils.DeclarationFinder; +import org.eclipse.cdt.internal.ui.refactoring.utils.DeclarationFinderDO; +import org.eclipse.cdt.internal.ui.refactoring.utils.ExpressionFinder; +import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper; +import org.eclipse.cdt.internal.ui.refactoring.utils.TranslationUnitHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; /** @@ -45,8 +57,9 @@ import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; */ public class HideMethodRefactoring extends CRefactoring { - private IASTName methodToMove; - private IASTSimpleDeclaration fieldToMoveDecl; + private IASTName methodToHide; + private IASTDeclaration methodToHideDecl; + private DeclarationFinderDO declData; public HideMethodRefactoring(IFile file, ISelection selection, ICElement element) { super(file, selection, element); @@ -77,92 +90,169 @@ public class HideMethodRefactoring extends CRefactoring { sm.worked(1); if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; - this.methodToMove = getDeclaration(name); - - if(this.methodToMove == null) { + declData = DeclarationFinder.getDeclaration(name, getIndex()); + + if(declData == null || declData.name == null) { initStatus.addFatalError(Messages.HideMethodRefactoring_NoMethodNameSelected); return initStatus; } + + methodToHide = declData.name; sm.worked(1); - fieldToMoveDecl = findSimpleDeclaration(this.methodToMove); + methodToHideDecl = NodeHelper.findSimpleDeclarationInParents(methodToHide); + if(methodToHideDecl == null) { + initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); + return initStatus; + } + if(!(methodToHideDecl.getParent() instanceof ICPPASTCompositeTypeSpecifier)) { + methodToHideDecl = NodeHelper.findFunctionDefinition(methodToHide); + } if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; sm.worked(1); - for(IASTDeclarator declarator : fieldToMoveDecl.getDeclarators()) { + if(methodToHideDecl instanceof IASTFunctionDefinition) { + IASTDeclarator declarator = ((IASTFunctionDefinition)methodToHideDecl).getDeclarator(); if(declarator.getName().getRawSignature().equals(name.getRawSignature())) { if (!(declarator instanceof IASTFunctionDeclarator)) { initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); return initStatus; } } + }else if (methodToHideDecl instanceof IASTSimpleDeclaration) { + for(IASTDeclarator declarator : ((IASTSimpleDeclaration) methodToHideDecl).getDeclarators()) { + if(declarator.getName().getRawSignature().equals(name.getRawSignature())) { + if (!(declarator instanceof IASTFunctionDeclarator)) { + initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); + return initStatus; + } + } + } + }else { + initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); + return initStatus; + } + + sm.worked(1); + + IASTCompositeTypeSpecifier classNode = NodeHelper.findEnclosingClass(methodToHide); + if(classNode == null) { + initStatus.addError(Messages.HideMethodRefactoring_EnclosingClassNotFound); + } + + if(checkIfPrivate(classNode, methodToHideDecl)) { + initStatus.addError(Messages.HideMethodRefactoring_IsAlreadyPrivate); } sm.done(); return initStatus; } - protected IASTSimpleDeclaration findSimpleDeclaration(IASTNode fieldToFoundDecl) { - while(fieldToFoundDecl != null){ - if (fieldToFoundDecl instanceof IASTSimpleDeclaration) { - return (IASTSimpleDeclaration) fieldToFoundDecl; + private boolean checkIfPrivate(IASTCompositeTypeSpecifier classNode, IASTDeclaration decl) { + IASTDeclaration[] members = classNode.getMembers(); + + int currentVisibility = ICPPASTVisibilityLabel.v_private; + if(IASTCompositeTypeSpecifier.k_struct == classNode.getKey()) { + currentVisibility = ICPPASTVisibilityLabel.v_public; + } + for (IASTDeclaration declaration : members) { + if(declaration instanceof ICPPASTVisibilityLabel){ + currentVisibility =((ICPPASTVisibilityLabel) declaration).getVisibility(); } - fieldToFoundDecl = fieldToFoundDecl.getParent(); - } - return null; - } - - private IASTName getDeclaration(IASTName name) { - DeclarationFinder df = new DeclarationFinder(name); - unit.accept(df); - return df.getDeclaration(); - } - - private class DeclarationFinder extends CPPASTVisitor{ - - IASTName astName; - IASTName declaration = null; - - { - shouldVisitDeclarators = true; - } - - public DeclarationFinder(IASTName name) { - this.astName = name; - } - - @Override - public int visit(IASTDeclarator declarator) { - if(declarator instanceof ICPPASTFunctionDeclarator) { - ICPPASTFunctionDeclarator funcDec = (ICPPASTFunctionDeclarator)declarator; - if(funcDec.getName().getRawSignature().equals(astName.getRawSignature())){ - declaration = funcDec.getName(); - return PROCESS_ABORT; + + if (declaration != null) { + if(decl == declaration) { + break; } } - return ASTVisitor.PROCESS_CONTINUE; } - - public IASTName getDeclaration() { - return declaration; + if(ICPPASTVisibilityLabel.v_private == currentVisibility) { + return true; } + return false; } - + @Override public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { - return super.checkFinalConditions(pm); + RefactoringStatus finalConditions = super.checkFinalConditions(pm); + + for(IIndexName pdomref : declData.allNamesPDom) { + declData.filename = pdomref.getFileLocation().getFileName(); + + if(pdomref instanceof PDOMName) { + PDOMName pdomName = (PDOMName)pdomref; + if(pdomName.isDeclaration()) { + continue; + } + if(pdomName.isDefinition()) { + continue; + } + } + + IASTTranslationUnit transUtmp = TranslationUnitHelper.loadTranslationUnit(declData.filename); + IASTName expName = ExpressionFinder.findExpressionInTranslationUnit(transUtmp, pdomref); + + IASTFunctionDeclarator funcDec = findEnclosingFunction(expName); + IASTCompositeTypeSpecifier encClass2; + if(funcDec == null) { + encClass2 = NodeHelper.findEnclosingClass(expName); + } + else { + encClass2 = NodeHelper.findEnclosingClass(funcDec); + } + + IASTCompositeTypeSpecifier encClass = NodeHelper.findEnclosingClass(methodToHide); + + if(!NodeHelper.isSameNode(encClass, encClass2)) { + finalConditions.addWarning(Messages.HideMethodRefactoring_HasExternalReferences); + break; + } + } + return finalConditions; } - + + private IASTFunctionDeclarator findEnclosingFunction(IASTNode node) throws CoreException { + IASTCompoundStatement compStat = NodeHelper.findCompoundStatementInParent(node); + if(compStat == null) { + return null; + } + + IASTNode parent = compStat.getParent(); + if(parent instanceof IASTFunctionDefinition) { + IASTDeclarator declarator = ((IASTFunctionDefinition)parent).getDeclarator(); + IASTName declaratorName = getLastName(declarator); + + DeclarationFinderDO data = DeclarationFinder.getDeclaration(declaratorName, getIndex()); + + if(data == null || data.name == null) { + return null; + } + + if(data.name.getParent() instanceof IASTFunctionDeclarator) { + return (IASTFunctionDeclarator) data.name.getParent(); + } + return null; + }else if(parent instanceof IASTTranslationUnit) { + return null; + } + return findEnclosingFunction(parent); + } + + private IASTName getLastName(IASTDeclarator declarator) { + IASTName declaratorName = declarator.getName(); + if(declaratorName instanceof ICPPASTQualifiedName) { + IASTName[] declaratorNames = ((ICPPASTQualifiedName)declaratorName).getNames(); + declaratorName = declaratorNames[declaratorNames.length-1]; + } + return declaratorName; + } + @Override protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException { + ASTRewrite rewriter = collector.rewriterForTranslationUnit(declData.transUnit); + TextEditGroup editGroup = new TextEditGroup(Messages.HideMethodRefactoring_FILE_CHANGE_TEXT+ methodToHide.getRawSignature()); - ASTRewrite rewriter = collector.rewriterForTranslationUnit(unit); - TextEditGroup editGroup = new TextEditGroup(Messages.HideMethodRefactoring_FILE_CHANGE_TEXT+ methodToMove.getRawSignature()); - - // egtodo -// IASTNode parent = fieldToMoveDecl.getParent(); - - ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) fieldToMoveDecl.getParent(); - AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_private, fieldToMoveDecl, false, collector); + ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) methodToHideDecl.getParent(); + AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_private, methodToHideDecl, false, collector); - rewriter.remove(fieldToMoveDecl, editGroup); + rewriter.remove(methodToHideDecl, editGroup); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java index 070f3a66e99..4b898db73db 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/Messages.java @@ -21,6 +21,7 @@ public final class Messages extends NLS { // Do not instantiate } + public static String HideMethodRefactoring_HasExternalReferences; public static String HideMethodRefactoring_HIDE_METHOD; public static String HideMethodRefactoring_NoNameSelected; public static String HideMethodRefactoring_NoMethodNameSelected; @@ -32,6 +33,8 @@ public final class Messages extends NLS { public static String HideMethodRefactoring_NO_FILE; public static String HideMethodRefactoring_NO_PROPER_SELECTION; public static String HideMethodRefactoring_EDITOR_NOT_SAVE; + public static String HideMethodRefactoring_EnclosingClassNotFound; + public static String HideMethodRefactoring_IsAlreadyPrivate; public static String HideMethodRefactoring_NO_NODES; static { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties index fe4272506b8..98be99bd063 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/hidemethod/messages.properties @@ -9,10 +9,11 @@ # Contributors: # Institute for Software - initial API and implementation ############################################################################### +HideMethodRefactoring_HasExternalReferences=This method might be referenced from outside of the class and therefor compilation error can occure. HideMethodRefactoring_HIDE_METHOD=Hide Method HideMethodRefactoring_NoNameSelected=No names selected. HideMethodRefactoring_NoMethodNameSelected=No method name selected. -HideMethodRefactoring_CanOnlyHideMethods=Hide Method can only hide Methods. +HideMethodRefactoring_CanOnlyHideMethods=Hide Method can only hide Member Functions. HideMethodRefactoring_FILE_CHANGE_TEXT=Hide HideMethodRefactoring_BAD_TYPE=Bad selection-type HideMethodRefactoring_NO_SELECTION=No Selection available. @@ -21,5 +22,7 @@ HideMethodRefactoring_BAD_DIALECT=Not Supported Dialect HideMethodRefactoring_NO_FILE=There is no file marked. HideMethodRefactoring_NO_PROPER_SELECTION=No proper Selection\! HideMethodRefactoring_EDITOR_NOT_SAVE=Editor is not saved +HideMethodRefactoring_EnclosingClassNotFound=Enclosing class not found. +HideMethodRefactoring_IsAlreadyPrivate=Method is already private. HideMethodRefactoring_NO_NODES=No nodes found.