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

Improvements for Hide Method, by Emanuel Graf, bug 232291.

This commit is contained in:
Markus Schorn 2008-05-15 14:39:55 +00:00
parent 401d6e1845
commit 6905e11d5d
5 changed files with 829 additions and 98 deletions

View file

@ -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 <iostream>
class A{
public:
//$void method2();$//
};
#endif /*A_H_*/
//=
#ifndef A_H_
#define A_H_
#include <iostream>
class A{
public:
private:
void method2();
};
#endif /*A_H_*/
//!HideMethodSimple //!HideMethodSimple
//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest //#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest
//@.config //@.config
@ -595,6 +564,7 @@ private:
//#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest //#org.eclipse.cdt.ui.tests.refactoring.hidemethod.HideMethodRefactoringTest
//@.config //@.config
filename=A.h filename=A.h
errors=1
//@A.h //@A.h
#ifndef A_H_ #ifndef A_H_
#define 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 <iostream>
class A{
public:
//$void method2();$//
};
#endif /*A_H_*/
//=
#ifndef A_H_
#define A_H_
#include <iostream>
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 <iostream>
class A{
//$void method2();$//
};
#endif /*A_H_*/
//=
#ifndef A_H_
#define A_H_
#include <iostream>
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 <iostream>
struct A{
//$void method2();$//
};
#endif /*A_H_*/
//=
#ifndef A_H_
#define A_H_
#include <iostream>
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:
};

View file

@ -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.RefactoringTest;
import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile; 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; import org.eclipse.cdt.internal.ui.refactoring.hidemethod.HideMethodRefactoring;
/** /**
* @author Guido Zgraggen IFS * @author Guido Zgraggen IFS
*/ */
public class HideMethodRefactoringTest extends RefactoringTest { public class HideMethodRefactoringTest extends RefactoringTest {
private int warnings;
private int errors;
private int fatalerrors;
public HideMethodRefactoringTest(String name, Vector<TestSourceFile> files) { public HideMethodRefactoringTest(String name, Vector<TestSourceFile> files) {
super(name, files); super(name, files);
@ -36,21 +40,40 @@ public class HideMethodRefactoringTest extends RefactoringTest {
protected void runTest() throws Throwable { protected void runTest() throws Throwable {
IFile refFile = project.getFile(fileWithSelection); IFile refFile = project.getFile(fileWithSelection);
CRefactoring refactoring = new HideMethodRefactoring(refFile,selection, null);
HideMethodRefactoring refactoring = new HideMethodRefactoring(refFile,selection, null); try {
refactoring.lockIndex();
RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR); 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); Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR);
RefactoringStatus finalConditions = refactoring.checkFinalConditions(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); createChange.perform(NULL_PROGRESS_MONITOR);
compareFiles(fileMap); compareFiles(fileMap);
}
finally {
refactoring.unlockIndex();
}
} }
@Override @Override
protected void configureRefactoring(Properties refactoringProperties) { 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$
} }
} }

View file

@ -22,21 +22,33 @@ import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.text.edits.TextEditGroup; 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.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator; 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.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; 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.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.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.ICElement; 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.AddDeclarationNodeToClassChange;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector; 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; 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 { public class HideMethodRefactoring extends CRefactoring {
private IASTName methodToMove; private IASTName methodToHide;
private IASTSimpleDeclaration fieldToMoveDecl; private IASTDeclaration methodToHideDecl;
private DeclarationFinderDO declData;
public HideMethodRefactoring(IFile file, ISelection selection, ICElement element) { public HideMethodRefactoring(IFile file, ISelection selection, ICElement element) {
super(file, selection, element); super(file, selection, element);
@ -77,92 +90,169 @@ public class HideMethodRefactoring extends CRefactoring {
sm.worked(1); sm.worked(1);
if(isProgressMonitorCanceld(sm, initStatus)) return initStatus; if(isProgressMonitorCanceld(sm, initStatus)) return initStatus;
this.methodToMove = getDeclaration(name); declData = DeclarationFinder.getDeclaration(name, getIndex());
if(this.methodToMove == null) { if(declData == null || declData.name == null) {
initStatus.addFatalError(Messages.HideMethodRefactoring_NoMethodNameSelected); initStatus.addFatalError(Messages.HideMethodRefactoring_NoMethodNameSelected);
return initStatus; return initStatus;
} }
methodToHide = declData.name;
sm.worked(1); 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; if(isProgressMonitorCanceld(sm, initStatus)) return initStatus;
sm.worked(1); 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.getName().getRawSignature().equals(name.getRawSignature())) {
if (!(declarator instanceof IASTFunctionDeclarator)) { if (!(declarator instanceof IASTFunctionDeclarator)) {
initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods); initStatus.addFatalError(Messages.HideMethodRefactoring_CanOnlyHideMethods);
return initStatus; 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(); sm.done();
return initStatus; return initStatus;
} }
protected IASTSimpleDeclaration findSimpleDeclaration(IASTNode fieldToFoundDecl) { private boolean checkIfPrivate(IASTCompositeTypeSpecifier classNode, IASTDeclaration decl) {
while(fieldToFoundDecl != null){ IASTDeclaration[] members = classNode.getMembers();
if (fieldToFoundDecl instanceof IASTSimpleDeclaration) {
return (IASTSimpleDeclaration) fieldToFoundDecl; 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) { if (declaration != null) {
DeclarationFinder df = new DeclarationFinder(name); if(decl == declaration) {
unit.accept(df); break;
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;
} }
} }
return ASTVisitor.PROCESS_CONTINUE;
} }
if(ICPPASTVisibilityLabel.v_private == currentVisibility) {
public IASTName getDeclaration() { return true;
return declaration;
} }
return false;
} }
@Override @Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { 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 @Override
protected void collectModifications(IProgressMonitor pm, ModificationCollector collector) throws CoreException, OperationCanceledException { 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); ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) methodToHideDecl.getParent();
TextEditGroup editGroup = new TextEditGroup(Messages.HideMethodRefactoring_FILE_CHANGE_TEXT+ methodToMove.getRawSignature()); AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_private, methodToHideDecl, false, collector);
// egtodo rewriter.remove(methodToHideDecl, editGroup);
// IASTNode parent = fieldToMoveDecl.getParent();
ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) fieldToMoveDecl.getParent();
AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_private, fieldToMoveDecl, false, collector);
rewriter.remove(fieldToMoveDecl, editGroup);
} }
} }

View file

@ -21,6 +21,7 @@ public final class Messages extends NLS {
// Do not instantiate // Do not instantiate
} }
public static String HideMethodRefactoring_HasExternalReferences;
public static String HideMethodRefactoring_HIDE_METHOD; public static String HideMethodRefactoring_HIDE_METHOD;
public static String HideMethodRefactoring_NoNameSelected; public static String HideMethodRefactoring_NoNameSelected;
public static String HideMethodRefactoring_NoMethodNameSelected; 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_FILE;
public static String HideMethodRefactoring_NO_PROPER_SELECTION; public static String HideMethodRefactoring_NO_PROPER_SELECTION;
public static String HideMethodRefactoring_EDITOR_NOT_SAVE; public static String HideMethodRefactoring_EDITOR_NOT_SAVE;
public static String HideMethodRefactoring_EnclosingClassNotFound;
public static String HideMethodRefactoring_IsAlreadyPrivate;
public static String HideMethodRefactoring_NO_NODES; public static String HideMethodRefactoring_NO_NODES;
static { static {

View file

@ -9,10 +9,11 @@
# Contributors: # Contributors:
# Institute for Software - initial API and implementation # 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_HIDE_METHOD=Hide Method
HideMethodRefactoring_NoNameSelected=No names selected. HideMethodRefactoring_NoNameSelected=No names selected.
HideMethodRefactoring_NoMethodNameSelected=No method name 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_FILE_CHANGE_TEXT=Hide
HideMethodRefactoring_BAD_TYPE=Bad selection-type HideMethodRefactoring_BAD_TYPE=Bad selection-type
HideMethodRefactoring_NO_SELECTION=No Selection available. 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_FILE=There is no file marked.
HideMethodRefactoring_NO_PROPER_SELECTION=No proper Selection\! HideMethodRefactoring_NO_PROPER_SELECTION=No proper Selection\!
HideMethodRefactoring_EDITOR_NOT_SAVE=Editor is not saved 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. HideMethodRefactoring_NO_NODES=No nodes found.