From 49fee1ebefc033e764b13142473cdb0c7cb4eac1 Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Thu, 3 Sep 2009 09:09:02 +0000 Subject: [PATCH] FIXED - bug 285375: Allow to decide where to place getters and setters https://bugs.eclipse.org/bugs/show_bug.cgi?id=285375 --- .../refactoring/GenerateGettersAndSetters.rts | 624 ++++++++++++++++++ .../ui/tests/refactoring/RefactoringTest.java | 2 +- .../GenerateGettersAndSettersTest.java | 11 +- .../ui/refactoring/ModificationCollector.java | 1 + .../gettersandsetters/FunctionFactory.java | 114 +++- .../GenerateGettersAndSettersInputPage.java | 14 + .../GenerateGettersAndSettersRefactoring.java | 69 +- .../GetterAndSetterContext.java | 26 +- .../GetterSetterInsertEditProvider.java | 63 +- .../gettersandsetters/Messages.java | 2 + .../gettersandsetters/messages.properties | 2 + 11 files changed, 865 insertions(+), 63 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts index 05b04619d77..667a6ee77ad 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/GenerateGettersAndSetters.rts @@ -3,6 +3,7 @@ //@.config filename=C.h getters=name +inHeader=true //@C.cpp #include "C.h" @@ -116,6 +117,7 @@ int gooo = 1; //@.config filename=C.h getters=name +inHeader=true //@C.cpp #include "C.h" @@ -235,6 +237,7 @@ int gooo = 1; //@.config filename=C.h setters=name +inHeader=true //@C.cpp #include "C.h" @@ -351,6 +354,7 @@ int gooo = 1; filename=C.h setters=name getters=name +inHeader=true //@C.cpp #include "C.h" @@ -472,6 +476,7 @@ int gooo = 1; filename=C.h getters=name,systemId setters=name,systemId +inHeader=true //@C.cpp #include "C.h" @@ -588,6 +593,7 @@ int gooo = 1; filename=GaS.h getters=i,isOk setters=i,isOk +inHeader=true //@GaS.cpp #include "Getters.h" @@ -656,6 +662,7 @@ private: filename=C.h getters=id setters=id +inHeader=true //@C.h #ifndef C_H_ #define C_H_ @@ -695,6 +702,7 @@ public: filename=C.h getters=i setters=i +inHeader=true //@C.h /* * test.h @@ -781,4 +789,620 @@ class test #endif /* TEST_H_ */ +//!Generate Getters and Setters One Getter Selection Implementation in CPP +//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest +//@.config +filename=C.h +getters=name +//@C.cpp +#include "C.h" + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + +//= +#include "C.h" + +char *Person::getName() const +{ + return name; +} + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + +//@C.h +#ifndef C_H_ +#define C_H_ + +class Person { + +private: + + int systemId; + +protected: + + char *name; + +public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int /*$*/SocSecNo/*$$*/(); + + int GetUniqueId(); + + int getSystemId(); + + void setSystemId(int systemId); +}; + +int gooo = 1; + +#endif /*C_H_*/ +//= +#ifndef C_H_ +#define C_H_ + +class Person { + +private: + + int systemId; + +protected: + + char *name; + +public: + + const int socSecNo; + + Person myFriend; + char *getName() const; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int SocSecNo(); + + int GetUniqueId(); + + int getSystemId(); + + void setSystemId(int systemId); +}; + +int gooo = 1; + +#endif /*C_H_*/ +//!Generate Getters and Setters One Getter Selection with Namespace Implementation in CPP +//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest +//@.config +filename=C.h +getters=name +//@C.cpp +#include "C.h" + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + +//= +#include "C.h" + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + +char *Person::getName() const +{ + return name; +} + + + +//@C.h +#ifndef C_H_ +#define C_H_ + +namespace Personal { + class Person { + + private: + + int systemId; + + protected: + + char *name; + + public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int /*$*/SocSecNo/*$$*/(); + + int GetUniqueId(); + + int getSystemId(){ + return systemId; + } + + void setSystemId(int systemId){ + this->systemId = systemId; + } + }; +} + +int gooo = 1; + +#endif /*C_H_*/ + +//= +#ifndef C_H_ +#define C_H_ + +namespace Personal { + class Person { + + private: + + int systemId; + + protected: + + char *name; + + public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int SocSecNo(); + + int GetUniqueId(); + char *getName() const; + + int getSystemId(){ + return systemId; + } + + void setSystemId(int systemId){ + this->systemId = systemId; + } + }; +} + +int gooo = 1; + +#endif /*C_H_*/ + +//!Generate Getters and Setters One Setter Selection Implementation in CPP +//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest +//@.config +filename=C.h +setters=name +//@C.cpp +#include "C.h" + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + + +//= +#include "C.h" + +void Person::setName(char *name) +{ + this->name = name; +} + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + + +//@C.h +#ifndef C_H_ +#define C_H_ + +class Person { + +private: + + int systemId; + +protected: + + char *name; + +public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int /*$*/SocSecNo/*$$*/(); + + int GetUniqueId(); + + int getSystemId(){ + return systemId; + } + + void setSystemId(int systemId){ + this->systemId = systemId; + } +}; + + +int gooo = 1; + +#endif /*C_H_*/ +//= +#ifndef C_H_ +#define C_H_ + +class Person { + +private: + + int systemId; + +protected: + + char *name; + +public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int SocSecNo(); + + int GetUniqueId(); + void setName(char *name); + + int getSystemId(){ + return systemId; + } + + void setSystemId(int systemId){ + this->systemId = systemId; + } +}; + + +int gooo = 1; + +#endif /*C_H_*/ +//!Getter and Setter Selection Implementation in CPP +//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest +//@.config +filename=C.h +setters=name +getters=name +//@C.cpp +#include "C.h" + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + +//= +#include "C.h" + +char *Person::getName() const +{ + return name; +} + +void Person::setName(char *name) +{ + this->name = name; +} + +int Person::SocSecNo(){ + return socSecNo; +} + +int main(int argc, char **argv) { + +} + +//@C.h +#ifndef C_H_ +#define C_H_ + +class Person { + +private: + + int systemId; + +protected: + + char *name; + +public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int /*$*/SocSecNo/*$$*/(); + + int GetUniqueId(); + + int getSystemId(){ + return systemId; + } + + void setSystemId(int systemId){ + this->systemId = systemId; + } +}; + + +int gooo = 1; + +#endif /*C_H_*/ + +//= +#ifndef C_H_ +#define C_H_ + +class Person { + +private: + + int systemId; + +protected: + + char *name; + +public: + + const int socSecNo; + + Person myFriend; + + + Person(int socSecNo); // contructor + + ~Person(); // destructor + + + char* Name(); + + void Print(); + + int SocSecNo(); + + int GetUniqueId(); + char *getName() const; + void setName(char *name); + + int getSystemId(){ + return systemId; + } + + void setSystemId(int systemId){ + this->systemId = systemId; + } +}; + + +int gooo = 1; + +#endif /*C_H_*/ + +//!Generate Getters and Setters no Methods Implementation in CPP +//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest +//@.config +filename=C.h +getters=id +setters=id +infos=1 +//@C.h +#ifndef C_H_ +#define C_H_ + +class Person { +private: + int /*$*/id/*$$*/; +}; + +#endif /*C_H_*/ + +//= +#ifndef C_H_ +#define C_H_ + +class Person { +private: + int id; +public: + int getId() const; + void setId(int id); +}; + +int Person::getId() const +{ + return id; +} + +void Person::setId(int id) +{ + this->id = id; +} + + + +#endif /*C_H_*/ + +//!No Methods Implementation in CPP +//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest +//@.config +filename=C.h +getters=i +setters=i +infos=1 +//@C.h +/* + * test.h + */ + +#ifndef TEST_H_ +#define TEST_H_ + +//comment1 +class test +{ + int /*$*/i/*$$*/; //comment2 + char* b; + //comment3 +}; + +#endif /* TEST_H_ */ + +//= +/* + * test.h + */ + +#ifndef TEST_H_ +#define TEST_H_ + +//comment1 +class test +{ + int i; //comment2 + char* b; +public: + int getI() const; + void setI(int i); + //comment3 +}; + +int test::getI() const +{ + return i; +} + +void test::setI(int i) +{ + this->i = i; +} + + + +#endif /* TEST_H_ */ diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTest.java index 7523d81fc19..cde4741799f 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTest.java @@ -84,7 +84,7 @@ public abstract class RefactoringTest extends RefactoringBaseTest { } protected void assertConditionsOk(RefactoringStatus conditions) { - assertTrue(conditions.isOK() ? "OK" : "Error or Warning in initial Conditions: " + conditions.getEntries()[0].getMessage() //$NON-NLS-1$ //$NON-NLS-2$ + assertTrue(conditions.isOK() ? "OK" : "Error or Warning in Conditions: " + conditions.getEntries()[0].getMessage() //$NON-NLS-1$ //$NON-NLS-2$ , conditions.isOK()); } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java index 4cd19b043e3..561229a3e4d 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/gettersandsetters/GenerateGettersAndSettersTest.java @@ -39,6 +39,8 @@ public class GenerateGettersAndSettersTest extends RefactoringTest { private ArrayList selectedGetters; private ArrayList selectedSetters; private GenerateGettersAndSettersRefactoring refactoring; + private boolean keepInHeader; + private int infos; /** @@ -70,13 +72,14 @@ public class GenerateGettersAndSettersTest extends RefactoringTest { private void executeRefactoring() throws CoreException, Exception { - RefactoringStatus initialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR); - assertConditionsOk(initialConditions); selectFields(); - Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR); + refactoring.getContext().setImplementationInHeader(keepInHeader); RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR); + Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR); if(warnings > 0){ assertConditionsWarning(finalConditions, warnings); + }else if(infos >0) { + assertConditionsInfo(finalConditions, infos); } else{ assertConditionsOk(finalConditions); @@ -109,8 +112,10 @@ public class GenerateGettersAndSettersTest extends RefactoringTest { protected void configureRefactoring(Properties refactoringProperties) { fatalError = Boolean.valueOf(refactoringProperties.getProperty("fatalerror", "false")).booleanValue(); //$NON-NLS-1$//$NON-NLS-2$ warnings = new Integer(refactoringProperties.getProperty("warnings", "0")).intValue(); //$NON-NLS-1$//$NON-NLS-2$ + infos = new Integer(refactoringProperties.getProperty("infos", "0")); String getters = refactoringProperties.getProperty("getters", ""); //$NON-NLS-1$ //$NON-NLS-2$ String setters = refactoringProperties.getProperty("setters", ""); //$NON-NLS-1$ //$NON-NLS-2$ + keepInHeader = Boolean.valueOf(refactoringProperties.getProperty("inHeader", "false")); selectedGetters = new ArrayList(); for(String getterName : getters.split(",")){ //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ModificationCollector.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ModificationCollector.java index 691cd24e136..e5ec2b820d0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ModificationCollector.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ModificationCollector.java @@ -60,6 +60,7 @@ public class ModificationCollector { result.addAll(changes.toArray(new Change[changes.size()])); for (ASTRewrite each : rewriters.values()) { + result.add(each.rewriteAST()); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java index e3bec0915cb..e9cdba169de 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/FunctionFactory.java @@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.parser.Keywords; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression; @@ -30,28 +31,25 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration; import org.eclipse.cdt.internal.ui.refactoring.utils.NameHelper; public class FunctionFactory { - public static IASTFunctionDefinition createGetter(String varName, IASTSimpleDeclaration fieldDeclaration) { + public static IASTFunctionDefinition createGetterDefinition(String varName, IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) { IASTFunctionDefinition getter = new CPPASTFunctionDefinition(); - getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy()); - - CPPASTName getterName = new CPPASTName(); - String varPartOfGetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName)); - getterName.setName("get".concat(varPartOfGetterName).toCharArray()); //$NON-NLS-1$ - CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator(); - declarator.setConst(true); - declarator.setName(getterName); - for(IASTPointerOperator pointer : fieldDeclaration.getDeclarators()[0].getPointerOperators()){ - declarator.addPointerOperator(pointer.copy()); - } - getter.setDeclarator(declarator); + getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy()); + getter.setDeclarator(getGetterDeclarator(varName, fieldDeclaration, name)); + getter.setBody(getGetterBody(varName)); + + return getter; + } + + private static CPPASTCompoundStatement getGetterBody(String varName) { CPPASTCompoundStatement compound = new CPPASTCompoundStatement(); CPPASTReturnStatement returnStatement = new CPPASTReturnStatement(); CPPASTIdExpression idExpr = new CPPASTIdExpression(); @@ -60,31 +58,40 @@ public class FunctionFactory { idExpr.setName(returnVal); returnStatement.setReturnValue(idExpr); compound.addStatement(returnStatement); - - getter.setBody(compound); - - return getter; + return compound; + } + + private static CPPASTFunctionDeclarator getGetterDeclarator(String varName, + IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) { + CPPASTName getterName = new CPPASTName(); + String varPartOfGetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName)); + getterName.setName("get".concat(varPartOfGetterName).toCharArray()); //$NON-NLS-1$ + CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator(); + declarator.setConst(true); + if(name != null) { + name.addName(getterName); + declarator.setName(name); + }else { + declarator.setName(getterName); + } + for(IASTPointerOperator pointer : fieldDeclaration.getDeclarators()[0].getPointerOperators()){ + declarator.addPointerOperator(pointer.copy()); + } + return declarator; } - public static IASTFunctionDefinition createSetter(String varName, IASTSimpleDeclaration fieldDeclaration) { + public static IASTFunctionDefinition createSetterDefinition(String varName, IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) { IASTFunctionDefinition setter = new CPPASTFunctionDefinition(); - CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier(); - declSpecifier.setType(IASTSimpleDeclSpecifier.t_void); - setter.setDeclSpecifier(declSpecifier); - - CPPASTName setterName = new CPPASTName(); - String varPartOfSetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName)); - setterName.setName("set".concat(varPartOfSetterName).toCharArray()); //$NON-NLS-1$ - CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator(); - declarator.setName(setterName); - setter.setDeclarator(declarator); - CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration(); - parameterDeclaration.setDeclarator(fieldDeclaration.getDeclarators()[0].copy()); - parameterDeclaration.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy()); - declarator.addParameterDeclaration(parameterDeclaration.copy()); + setter.setDeclSpecifier(getVoidDeclSpec()); + setter.setDeclarator(getSetterDeclarator(varName, fieldDeclaration, name)); + setter.setBody(getSetterBody(fieldDeclaration)); + return setter; + } + + private static CPPASTCompoundStatement getSetterBody(IASTSimpleDeclaration fieldDeclaration) { CPPASTCompoundStatement compound = new CPPASTCompoundStatement(); CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement(); CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression(); @@ -101,9 +108,48 @@ public class FunctionFactory { binExpr.setOperand2(idExpr); exprStmt.setExpression(binExpr); compound.addStatement(exprStmt); + return compound; + } + + private static CPPASTFunctionDeclarator getSetterDeclarator(String varName, + IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) { + CPPASTName setterName = new CPPASTName(); + String varPartOfSetterName = NameHelper.makeFirstCharUpper(NameHelper.trimFieldName(varName)); + setterName.setName("set".concat(varPartOfSetterName).toCharArray()); //$NON-NLS-1$ + CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator(); + if(name != null) { + name.addName(setterName); + declarator.setName(name); + }else { + declarator.setName(setterName); + } + CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration(); + parameterDeclaration.setDeclarator(fieldDeclaration.getDeclarators()[0].copy()); + parameterDeclaration.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy()); + declarator.addParameterDeclaration(parameterDeclaration.copy()); + return declarator; + } + + private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() { + CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier(); + declSpecifier.setType(IASTSimpleDeclSpecifier.t_void); + return declSpecifier; + } + + public static IASTSimpleDeclaration createGetterDeclaration(String name, + IASTSimpleDeclaration fieldDeclaration) { + IASTSimpleDeclaration getter = new CPPASTSimpleDeclaration(); + getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy()); + getter.addDeclarator(getGetterDeclarator(name, fieldDeclaration, null)); - setter.setBody(compound); - + return getter; + } + + public static IASTSimpleDeclaration createSetterDeclaration(String name, + IASTSimpleDeclaration fieldDeclaration) { + IASTSimpleDeclaration setter = new CPPASTSimpleDeclaration(); + setter.setDeclSpecifier(getVoidDeclSpec()); + setter.addDeclarator(getSetterDeclarator(name, fieldDeclaration, null)); return setter; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java index d821a6c587e..978b30c46f3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GenerateGettersAndSettersInputPage.java @@ -56,6 +56,20 @@ public class GenerateGettersAndSettersInputPage extends UserInputWizardPage { gd = new GridData(); gd.verticalAlignment = SWT.TOP; btComp.setLayoutData(gd); + + final Button placeImplemetation = new Button(comp, SWT.CHECK); + placeImplemetation.setText(Messages.GenerateGettersAndSettersInputPage_PlaceImplHeader); + gd = new GridData(); + placeImplemetation.setLayoutData(gd); + placeImplemetation.setSelection(context.isImplementationInHeader()); + placeImplemetation.addSelectionListener(new SelectionAdapter() { + + @Override + public void widgetSelected(SelectionEvent e) { + context.setImplementationInHeader(placeImplemetation.getSelection()); + } + + }); setControl(comp); } 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 44ec2477c6a..ff5074b4642 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 @@ -35,15 +35,22 @@ 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.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ContainerNode; + import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange; import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; import org.eclipse.cdt.internal.ui.refactoring.Container; import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector; +import org.eclipse.cdt.internal.ui.refactoring.implementmethod.InsertLocation; +import org.eclipse.cdt.internal.ui.refactoring.implementmethod.MethodDefinitionInsertLocationFinder; +import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper; import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; @@ -81,7 +88,8 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { } private static final String MEMBER_DECLARATION = "MEMBER_DECLARATION"; //$NON-NLS-1$ - private final GetterAndSetterContext context = new GetterAndSetterContext(); + private final GetterAndSetterContext context = new GetterAndSetterContext(); + private InsertLocation definitionInsertLocation; public GenerateGettersAndSettersRefactoring(IFile file, ISelection selection, ICElement element, ICProject project) { super(file, selection, element, project); @@ -103,10 +111,24 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { } return initStatus; } + + + + @Override + public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, + OperationCanceledException { + RefactoringStatus finalStatus = super.checkFinalConditions(pm); + if(!context.isImplementationInHeader()) { + definitionInsertLocation = findInsertLocation(); + if(file.equals(definitionInsertLocation.getInsertFile())) { + finalStatus.addInfo(Messages.GenerateGettersAndSettersRefactoring_NoImplFile); + } + } + return finalStatus; + } private void initRefactoring(IProgressMonitor pm) { loadTranslationUnit(initStatus, pm); - context.setUnit(unit); context.selectedName = getSelectedName(); IASTCompositeTypeSpecifier compositeTypeSpecifier = null; if(context.selectedName != null) { @@ -185,12 +207,37 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { @Override protected void collectModifications(IProgressMonitor pm,ModificationCollector collector) throws CoreException, OperationCanceledException { ArrayList getterAndSetters = new ArrayList(); + ArrayList definitions = new ArrayList(); for(GetterSetterInsertEditProvider currentProvider : context.selectedFunctions){ - getterAndSetters.add(currentProvider.getFunction()); - } + if(context.isImplementationInHeader()) { + getterAndSetters.add(currentProvider.getFunctionDefinition(false)); + }else { + getterAndSetters.add(currentProvider.getFunctionDeclaration()); + definitions.add(currentProvider.getFunctionDefinition(true)); + } + } + if(!context.isImplementationInHeader()) { + addDefinition(collector, definitions); + } ICPPASTCompositeTypeSpecifier classDefinition = (ICPPASTCompositeTypeSpecifier) context.existingFields.get(context.existingFields.size()-1).getParent(); AddDeclarationNodeToClassChange.createChange(classDefinition, VisibilityEnum.v_public, getterAndSetters, false, collector); + + + } + + private void addDefinition(ModificationCollector collector, ArrayList definitions) + throws CoreException { + InsertLocation location = findInsertLocation(); + IASTTranslationUnit targetUnit = location.getTargetTranslationUnit(); + IASTNode parent = location.getPartenOfNodeToInsertBefore(); + ASTRewrite rewrite = collector.rewriterForTranslationUnit(targetUnit); + IASTNode nodeToInsertBefore = location.getNodeToInsertBefore(); + ContainerNode cont = new ContainerNode(); + for (IASTFunctionDefinition functionDefinition : definitions) { + cont.addNode(functionDefinition); + } + rewrite = rewrite.insertBefore(parent, nodeToInsertBefore, cont , null); } public GetterAndSetterContext getContext() { @@ -200,6 +247,20 @@ public class GenerateGettersAndSettersRefactoring extends CRefactoring { public Region getRegion() { return region; } + + private InsertLocation findInsertLocation() throws CoreException { + IASTSimpleDeclaration decl = context.existingFields.get(0); + + InsertLocation insertLocation = MethodDefinitionInsertLocationFinder.find(decl.getFileLocation(), decl.getParent(), file); + + if (!insertLocation.hasFile() || NodeHelper.isContainedInTemplateDeclaration(decl)) { + insertLocation.setInsertFile(file); + insertLocation.setNodeToInsertAfter(NodeHelper.findTopLevelParent(decl)); + } + + return insertLocation; + + } @Override protected RefactoringDescriptor getRefactoringDescriptor() { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterAndSetterContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterAndSetterContext.java index de70972b4d2..60fe79a63d4 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterAndSetterContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterAndSetterContext.java @@ -21,7 +21,6 @@ import org.eclipse.jface.viewers.Viewer; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; -import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.internal.ui.refactoring.gettersandsetters.GetterSetterInsertEditProvider.Type; import org.eclipse.cdt.internal.ui.refactoring.utils.NameHelper; @@ -32,9 +31,9 @@ public class GetterAndSetterContext implements ITreeContentProvider{ public ArrayList existingFunctionDefinitions = new ArrayList(); public ArrayList existingFunctionDeclarations = new ArrayList(); public SortedSet selectedFunctions = new TreeSet(); - private IASTTranslationUnit unit; public IASTName selectedName; private ArrayList wrappedFields; + private boolean implementationInHeader = false; public Object[] getChildren(Object parentElement) { @@ -57,20 +56,15 @@ public class GetterAndSetterContext implements ITreeContentProvider{ public GetterSetterInsertEditProvider createGetterInserter(IASTSimpleDeclaration simpleDeclaration) { String varName = simpleDeclaration.getDeclarators()[0].getName().toString(); - IASTFunctionDefinition getter = FunctionFactory.createGetter(varName, simpleDeclaration); - getter.setParent(unit); - return new GetterSetterInsertEditProvider(getter, Type.getter); + return new GetterSetterInsertEditProvider(varName, simpleDeclaration, Type.getter); } public GetterSetterInsertEditProvider createSetterInserter(IASTSimpleDeclaration simpleDeclaration) { String varName = simpleDeclaration.getDeclarators()[0].getName().toString(); - IASTFunctionDefinition setter = FunctionFactory.createSetter(varName, simpleDeclaration); - setter.setParent(unit); - return new GetterSetterInsertEditProvider(setter, Type.setter); + return new GetterSetterInsertEditProvider(varName, simpleDeclaration, Type.setter); } public Object getParent(Object element) { - // TODO Auto-generated method stub return null; } @@ -89,17 +83,19 @@ public class GetterAndSetterContext implements ITreeContentProvider{ } public void dispose() { - // TODO Auto-generated method stub } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - // TODO Auto-generated method stub - } - - public void setUnit(IASTTranslationUnit unit) { - this.unit = unit; } + public boolean isImplementationInHeader() { + return implementationInHeader; + } + + public void setImplementationInHeader(boolean implementationInHeader) { + this.implementationInHeader = implementationInHeader; + } + private ArrayList getWrappedFields() { if(wrappedFields == null) { wrappedFields = new ArrayList(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java index 6a0c29dfe73..5fb651c9ace 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/gettersandsetters/GetterSetterInsertEditProvider.java @@ -11,7 +11,13 @@ *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters; +import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; + +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName; public class GetterSetterInsertEditProvider implements Comparable{ @@ -22,22 +28,67 @@ public class GetterSetterInsertEditProvider implements Comparable