1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

FIXED - bug 285375: Allow to decide where to place getters and setters

https://bugs.eclipse.org/bugs/show_bug.cgi?id=285375
This commit is contained in:
Emanuel Graf 2009-09-03 09:09:02 +00:00
parent 0df87d142e
commit 49fee1ebef
11 changed files with 865 additions and 63 deletions

View file

@ -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_ */

View file

@ -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());
}

View file

@ -39,6 +39,8 @@ public class GenerateGettersAndSettersTest extends RefactoringTest {
private ArrayList<String> selectedGetters;
private ArrayList<String> 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<String>();
for(String getterName : getters.split(",")){ //$NON-NLS-1$

View file

@ -60,6 +60,7 @@ public class ModificationCollector {
result.addAll(changes.toArray(new Change[changes.size()]));
for (ASTRewrite each : rewriters.values()) {
result.add(each.rewriteAST());
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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<IASTNode> getterAndSetters = new ArrayList<IASTNode>();
ArrayList<IASTFunctionDefinition> definitions = new ArrayList<IASTFunctionDefinition>();
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<IASTFunctionDefinition> 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() {

View file

@ -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<IASTFunctionDefinition> existingFunctionDefinitions = new ArrayList<IASTFunctionDefinition>();
public ArrayList<IASTSimpleDeclaration> existingFunctionDeclarations = new ArrayList<IASTSimpleDeclaration>();
public SortedSet<GetterSetterInsertEditProvider> selectedFunctions = new TreeSet<GetterSetterInsertEditProvider>();
private IASTTranslationUnit unit;
public IASTName selectedName;
private ArrayList<FieldWrapper> 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<FieldWrapper> getWrappedFields() {
if(wrappedFields == null) {
wrappedFields = new ArrayList<FieldWrapper>();

View file

@ -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<GetterSetterInsertEditProvider>{
@ -22,22 +28,67 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
private IASTFunctionDefinition function;
private IASTSimpleDeclaration functionDeclaration;
private Type type;
private String name;
private IASTSimpleDeclaration fieldDeclaration;
public GetterSetterInsertEditProvider(IASTFunctionDefinition function, Type type){
this.function = function;
public GetterSetterInsertEditProvider(String name, IASTSimpleDeclaration fieldDeclaration, Type type){
switch(type) {
case getter:
this.functionDeclaration = FunctionFactory.createGetterDeclaration(name, fieldDeclaration);
break;
case setter:
this.functionDeclaration = FunctionFactory.createSetterDeclaration(name, fieldDeclaration);
break;
}
this.type = type;
this.name = name;
this.fieldDeclaration = fieldDeclaration;
}
@Override
public String toString(){
return function.getDeclarator().getName().toString();
return functionDeclaration.getDeclarators()[0].getName().toString();
}
public IASTFunctionDefinition getFunction() {
return function;
public IASTFunctionDefinition getFunctionDefinition(boolean qualifedName) {
IASTFunctionDefinition definition = null;
ICPPASTQualifiedName qname;
if(qualifedName) {
qname = getClassname();
}else {
qname = null;
}
switch(type) {
case getter:
definition = FunctionFactory.createGetterDefinition(name, fieldDeclaration, qname);
break;
case setter:
definition = FunctionFactory.createSetterDefinition(name, fieldDeclaration, qname);
break;
}
return definition;
}
private ICPPASTQualifiedName getClassname() {
IASTNode n = fieldDeclaration.getParent();
while (!(n instanceof IASTCompositeTypeSpecifier)) {
n = n.getParent();
}
IASTCompositeTypeSpecifier comp = (IASTCompositeTypeSpecifier) n;
CPPASTQualifiedName qname = new CPPASTQualifiedName();
qname.addName(comp.getName().copy());
return qname;
}
public IASTSimpleDeclaration getFunctionDeclaration() {
return functionDeclaration;
}
public Type getType() {

View file

@ -23,11 +23,13 @@ public final class Messages extends NLS {
public static String GenerateGettersAndSettersInputPage_DeselectAll;
public static String GenerateGettersAndSettersInputPage_header;
public static String GenerateGettersAndSettersInputPage_PlaceImplHeader;
public static String GenerateGettersAndSettersInputPage_SelectAll;
public static String GenerateGettersAndSettersInputPage_SelectGetters;
public static String GenerateGettersAndSettersInputPage_SelectSetters;
public static String GenerateGettersAndSettersRefactoring_NoCassDefFound;
public static String GenerateGettersAndSettersRefactoring_NoFields;
public static String GenerateGettersAndSettersRefactoring_NoImplFile;
public static String GettersAndSetters_Name;
static {

View file

@ -12,8 +12,10 @@
GettersAndSetters_Name=Generate getters and setters
GenerateGettersAndSettersInputPage_DeselectAll=Deselect All
GenerateGettersAndSettersInputPage_header=Select getters and setters to create:
GenerateGettersAndSettersInputPage_PlaceImplHeader=Place implementation in header file
GenerateGettersAndSettersInputPage_SelectAll=Select All
GenerateGettersAndSettersInputPage_SelectGetters=Select Getters
GenerateGettersAndSettersInputPage_SelectSetters=Select Setters
GenerateGettersAndSettersRefactoring_NoCassDefFound=No class definition found
GenerateGettersAndSettersRefactoring_NoFields=The class does not contain any fields.
GenerateGettersAndSettersRefactoring_NoImplFile=No implementation file found. Insert definition into the header file.